Creates a prefix command `!addrole <@User> <Role Tag>` which adds roles via custom tags. E.g. `!addrole @kanga mute` will add the defined 'mute' role. Define the role tags and ID in the code.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const prefix = require('prefix-parser');
const {guild_id, channel_id, content} = context.params.event;
// ** DEV ** Add the name of the role and the role ID here
// Example: !addrole @kang verified
// Result: This will add the role with ID 12345 to @kang
const roles = {
'verified': '12345',
'muted': '54321',
}
const [args, infoOrError] = prefix('!addrole', 'Add a role to a user')
.user('User')
.text('Role Tag')
.parse(content);
const message = (content) => lib.discord.channels['@0.1.2'].messages.create({ channel_id, content})
if (infoOrError) {
return message(infoOrError)
} else if (args) {
const [user_id, roleName] = args;
const role_id = roles[roleName]
if (role_id) {
try {
await lib.discord.guilds['@0.1.2'].members.roles.update({
guild_id, role_id, user_id,
});
return message(`Role added!`)
} catch (e) {
return message(`Could not add role with ID \`${role_id}\` to user`)
}
} else {
return message(`No role for \`${roleName}\` found`)
}
return;
}