jillo-bot/src/commands/roles.ts

79 lines
2.6 KiB
TypeScript

import { GuildMember, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { Command } from '../types/index';
import { RoleSeperator, RoleSeperatorType, db } from '../lib/db';
function extendOption(t: string) {
return {name: t, value: t};
}
export default {
data: new SlashCommandBuilder()
.setName('roles')
.setDescription('[ADMIN] Manage self-assignable roles')
.addSubcommand(sub =>
sub
.setName('seperator')
.setDescription('[ADMIN] Determine where new roles will be created')
.addStringOption(opt =>
opt
.setName('type')
.setDescription('The type of self-assignable role')
.setChoices(...['color', 'pronoun'].map(extendOption))
.setRequired(true)
)
.addRoleOption(opt =>
opt
.setName('seperator')
.setDescription('Roles will be put *below* this role; omit to put them at the bottom of the wole list')
)
)
.setDefaultMemberPermissions(0)
.setDMPermission(false),
execute: async (interaction: CommandInteraction) => {
if (!interaction.isChatInputCommand()) return;
const member = interaction.member! as GuildMember;
await interaction.deferReply({
ephemeral: true
});
const subcommand = interaction.options.getSubcommand();
if (subcommand === 'seperator') {
const type = interaction.options.getString('type', true) as RoleSeperatorType;
const seperator = interaction.options.getRole('seperator');
if (seperator) {
const highestRole = member.guild.members.me!.roles.highest;
if (seperator.position > highestRole.position)
return interaction.followUp(`This role is above my highest role (${highestRole.toString()}), so I can\`t put roles up there!`);
await db<RoleSeperator>('roleSeperators')
.insert({
guild: member.guild.id,
type,
role: seperator.id,
});
await interaction.followUp(`New ${type} roles will be placed below ${seperator.toString()}.`);
} else {
const role = await db<RoleSeperator>('roleSeperators')
.where('guild', member.guild.id)
.where('type', type)
.returning('*')
.delete();
if (role === 0) {
await interaction.followUp(`\`${type}\` never had a seperator role in this server!`);
} else {
await interaction.followUp(`Unset seperator role for \`${type}\`. Roles will now be placed at the bottom of the role list.`);
}
}
} else {
// ...
}
}
} satisfies Command;