This snippet is an add on to this snippet: https://autocode.com/snippet/ErNrCo/cachsnpt_YFKprcBq6EDdMBWZ3BYgjRpDfnPCHtcJ8nRv/ which allows the users to change their role colour with the `!color <hex code>` command.
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 ('!color')) {
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 === ('Available')) {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! You havent made your role with \`!role \` yet!`,
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 === ('!color')) {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `ERROR! Please use the command like this: \`!color \` , ex. \`!color #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: \`!color \` , ex. \`!color #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, update the role with the new color.
if (color === true) {
const roleColor = parseInt(hex[1], 16);
const role = await lib.utils.kv['@0.1.16'].get({
key: `${event.author.id}_${event.guild_id}_ROLE_ID`
});
// Then send a message saying their role color is updated.
await lib.discord.guilds['@0.1.2'].roles.update({
role_id: role,
guild_id: event.guild_id,
name: event.author.username,
color: roleColor
});
// Send a message saying their role color has been changed.
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `Request completed! New role color: **${input[1]}**.`,
message_reference: {
message_id: context.params.event.id,
channel_id: context.params.event.channel_id,
guild_id: context.params.event.guild_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
}
});
}}}}