Creates a `!nick <@user> <nickname>` prefix command that changes a users nickname. This provides proper command validation and error reporting.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const prefix = require('prefix-parser');
// Create the command
const [args, infoOrError] = prefix('!nick', "Change someone's nick name")
.user('User')
.rest('nick')
.parse(context.params.event.content);
if (infoOrError) {
return lib.discord.channels['@0.1.2'].messages.create({
channel_id: context.params.event.channel_id,
content: infoOrError,
});
} else if (args) {
const [user_id, nick] = args;
try {
await lib.discord.guilds['@0.1.0'].members.update({
guild_id: context.params.event.guild_id,
user_id: user_id,
nick: nick,
});
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `Nickname changed!`,
});
} catch (e) {
// We can't change the nickname for owners or the bot for example
console.error(e);
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `You can't change the nickname for this user`,
});
}
return;
}