Feature/81 slash command support (#192)

* Update discord.js

* Migrate to slash commands

* Clean up imports

* Update permissions

* Fix guild-specific commands not showing up

* Fix changes requested
This commit is contained in:
Vylpes 2022-09-18 11:57:22 +01:00 committed by GitHub
parent 0465697b87
commit ed8f5927c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 1469 additions and 1850 deletions

View file

@ -1,7 +1,4 @@
import { CommandResponse } from "../constants/CommandResponse";
import { ICommandContext } from "../contracts/ICommandContext";
import ErrorEmbed from "../helpers/embeds/ErrorEmbed";
import PublicEmbed from "../helpers/embeds/PublicEmbed";
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import SettingsHelper from "../helpers/SettingsHelper";
import StringTools from "../helpers/StringTools";
import { Command } from "../type/command";
@ -10,85 +7,58 @@ export default class Code extends Command {
constructor() {
super();
super.Category = "Moderation";
super.Roles = [
"moderator"
];
super.CommandBuilder = new SlashCommandBuilder()
.setName('code')
.setDescription('Manage the verification code of the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
.addSubcommand(subcommand =>
subcommand
.setName('randomise')
.setDescription('Regenerates the verification code for this server'))
.addSubcommand(subcommand =>
subcommand
.setName('embed')
.setDescription('Sends the embed with the current code to the current channel'));
}
public override async precheckAsync(context: ICommandContext): Promise<CommandResponse> {
if (!context.message.guild){
return CommandResponse.NotInServer;
}
public override async execute(interaction: CommandInteraction) {
if (!interaction.isChatInputCommand()) return;
const isEnabled = await SettingsHelper.GetSetting("verification.enabled", context.message.guild?.id);
if (!isEnabled) {
return CommandResponse.FeatureDisabled;
}
if (isEnabled.toLocaleLowerCase() != 'true') {
return CommandResponse.FeatureDisabled;
}
return CommandResponse.Ok;
}
public override async execute(context: ICommandContext) {
const action = context.args[0];
switch (action) {
switch (interaction.options.getSubcommand()) {
case "randomise":
await this.Randomise(context);
await this.Randomise(interaction);
break;
case "embed":
await this.SendEmbed(context);
await this.SendEmbed(interaction);
break;
default:
await this.SendUsage(context);
}
}
private async SendUsage(context: ICommandContext) {
const description = [
"USAGE: <randomise|embed>",
"",
"randomise: Sets the server's entry code to a random code",
"embed: Sends an embed with the server's entry code"
].join("\n");
const embed = new PublicEmbed(context, "", description);
await embed.SendToCurrentChannel();
}
private async Randomise(context: ICommandContext) {
if (!context.message.guild) {
return;
}
private async Randomise(interaction: CommandInteraction) {
if (!interaction.guildId) return;
const randomCode = StringTools.RandomString(5);
await SettingsHelper.SetSetting("verification.code", context.message.guild.id, randomCode);
await SettingsHelper.SetSetting("verification.code", interaction.guildId, randomCode);
const embed = new PublicEmbed(context, "Code", `Entry code has been set to \`${randomCode}\``);
await embed.SendToCurrentChannel();
await interaction.reply(`Entry code has been set to \`${randomCode}\``);
}
private async SendEmbed(context: ICommandContext) {
if (!context.message.guild) {
return;
}
private async SendEmbed(interaction: CommandInteraction) {
if (!interaction.guildId) return;
if (!interaction.channel) return;
const code = await SettingsHelper.GetSetting("verification.code", context.message.guild.id);
const code = await SettingsHelper.GetSetting("verification.code", interaction.guildId);
if (!code || code == "") {
const errorEmbed = new ErrorEmbed(context, "There is no code for this server setup.");
errorEmbed.SendToCurrentChannel();
await interaction.reply("There is no code for this server setup.");
return;
}
const embed = new PublicEmbed(context, "Entry Code", code!);
await embed.SendToCurrentChannel();
const embed = new EmbedBuilder()
.setTitle("Entry Code")
.setDescription(code);
await interaction.channel.send({ embeds: [ embed ]});
}
}