This is a one time use only prefix command that people can use to create their own roles + colour.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const event = context.params.event;
const msg = event.content;
// Prefix command, This makes sure only messages that start with '!role' will trigger this function.
if (msg.startsWith ('!role')) {
const input = msg.split(' ');
// Retrieve the availability of this command for the user.
const used = await lib.utils.kv['@0.1.16'].get({
key: `${event.author.id}_${event.guild_id}_ROLE`,
defaultValue: `Available`
});
console.log(used);
// Check if user has used this command before.
if (used === ('USED')) {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! You've already used this command! This command was made one time use only to prevent spam`,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_id
}
});
} else {
// Check if there was a hex code provided.
if (msg === ('!role')) {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! Please use the command like this: \`!role \` , ex. \`!role #ff0000\``,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_id
}
});
}
// Check if the hex code starts with a '#'.
if (!input[1].startsWith('#')) {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! Please use the command like this: \`!role \` , ex. \`!role #ff0000\``,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_id
}
});
} else {
const hex = input[1].split('#');
// Hex code validator function.
function isHexColor (hex) {
return typeof hex === 'string'
&& hex.length === 6
&& !isNaN(Number('0x' + hex))
}
// Run the given hex code through the function to check validation. (Also console log the output :).
const color = (isHexColor(hex[1]))
console.log(isHexColor(hex[1]))
// If the given hex code is a valid one, make the role with the color and the authors username.
if (color === true) {
const roleColor = parseInt(hex[1], 16);
const role = await lib.discord.guilds['@0.1.2'].roles.create({
guild_id: event.guild_id,
name: event.author.username,
color: roleColor,
hoist: false,
mentionable: false
});
// Then send a message saying their role is made.
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `Request completed! Here is your role: <@&${role.id}>!`,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_id
}
});
// And give them the role.
await lib.discord.guilds['@0.1.2'].members.roles.update({
role_id: role.id,
user_id: event.author.id,
guild_id: event.guild_id
});
// Now tell the system that this user has used this command in this guild.
await lib.utils.kv['@0.1.16'].set({
key: `${event.author.id}_${event.guild_id}_ROLE`,
value: 'USED'
});
// Set the role ID in utils for optional extra snippet.
await lib.utils.kv['@0.1.16'].set({
key: `${event.author.id}_${event.guild_id}_ROLE_ID`,
value: role.id
});
// If the given hex code isn't a valid one, return a message saying the hex code was invalid and they have to try again.
} else {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! **${input[1]}** is an invalid hex code, please try again.`,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_id
}
});
}}}}