This snippet requires you to register your slash command here: https://autocode.com/tools/discord/command-builder. Make a command called `colourrole` with an option called `hex-code`. Make sure to specifiy in the description of the option that people should not input a #; This snippet allows your donators and staff to set their own custom coloured role! They can run the command themselves and choose their own color.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const color = context.params.event.data.options[0].value;
let donatorRole = `${process.env.Donator}`;
let staffRole = `${process.env.Staff}`;
let boosterRole = `${process.env.Booster}`;
// check if input is valid hex code by checking the lenght
if (color.length === 6) {
// Do not run if user is not donator, booster or staff member
if (
context.params.event.member.roles.includes(donatorRole) ||
context.params.event.member.roles.includes(staffRole) ||
context.params.event.member.roles.includes(boosterRole)
) {
try {
// Create the role with the chosen color and name it the User's ID
let newRole = await lib.discord.guilds['@0.1.0'].roles.create({
guild_id: context.params.event.guild_id,
name: context.params.event.member.user.id,
color: Number.parseInt(`0x${color}`, 16),
permissions: `1`,
hoist: false,
mentionable: false,
});
// give user the newly created role
let result = await lib.discord.guilds['@0.1.0'].members.roles.update({
role_id: newRole.id,
user_id: context.params.event.member.user.id,
guild_id: context.params.event.guild_id,
});
// send a success message
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content:
'Congrats, you now have a unique role created for you and you only! If you want to change it, please send a DM to an admin.',
});
// if there is an error in the above, it is most likely because the hex code is invalid. This is mentioned in the message.
} catch {
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `That is not a valid color code. Please check out for valid hex colors. Also make sure not to include the \`#\`.`,
});
}
} //Fail message if user does not have the donator, booster or staff role
else {
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `You need to be a donator or booster befohre you can create a role. If you want to become a donator, check it out on: ${process.env.Donation_link}`,
});
}
} //shout at user when they dont put in a valid hex code
else {
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `${color} is not a valid code. Please check out for valid hex colors. Also make sure not to include the \`#\`.`,
});
}
// Feel free to ping Lars.#0018 (<@119473151913623552>) in the Autocode Discord server for help!