Better interaction

This commit is contained in:
2021-09-19 04:02:50 -07:00
parent 2fad7347d2
commit 66ea8b5de0
+65 -32
View File
@@ -1,52 +1,85 @@
import { Message, Permissions, CommandInteraction } from "discord.js"; import { Message, Permissions, Interaction, CommandInteraction } from "discord.js";
import { getEmotes, makeSuccessEmbed, makeProcessingEmbed, sendMessage, sendReply, makeErrorEmbed } from "../utils/DiscordMessage"; import { getEmotes, makeSuccessEmbed, makeProcessingEmbed, sendMessageOrInteractionResponse, sendReply, makeErrorEmbed, makeInfoEmbed } from "../utils/DiscordMessage";
import DiscordProvider from "../providers/Discord"; import DiscordProvider from "../providers/Discord";
const EMBEDS = {
SAY_INFO: (data: Message | Interaction) => {
return makeInfoEmbed ({
title: 'Say',
description: `Make me say something!`,
fields: [
{
name: 'Available arguments',
value: '``Your message``'
}
],
user: (data instanceof Interaction) ? data.user : data.author
});
},
NO_PERMISSION: (data: Message | Interaction) => {
return makeErrorEmbed ({
title: 'You need ``VIEW_CHANNEL, SEND_MESSAGES and MANAGE_CHANNELS`` permission on this guild!',
user: (data instanceof Interaction) ? data.user : data.author
});
},
SUCCESSFULLY_SAID: (data: Message | Interaction) => {
return makeSuccessEmbed ({
title: 'Successfully said',
user: (data instanceof Interaction) ? data.user : data.author
});
}
}
export default class Say { export default class Say {
async onCommand(command: string, args: any, message: Message) { async onCommand(command: string, args: any, message: Message) {
if(command.toLowerCase() !== 'say') return; if(command.toLowerCase() !== 'say') return;
await this.process(message, args);
if(!message.guildId) return;
if(message.member === null) return;
if(message.guild === null) return;
await this.sendSayMessage(false, message, args);
} }
async interactionCreate(interaction: CommandInteraction) { async interactionCreate(interaction: CommandInteraction) {
if(typeof interaction.commandName === 'undefined') return; if(interaction.isCommand()) {
if((interaction.commandName).toLowerCase() !== 'say') return; if(typeof interaction.commandName === 'undefined') return;
await this.sendSayMessage(true, interaction, interaction.options.getString('message')); if((interaction.commandName).toLowerCase() !== 'say') return;
await this.process(interaction, interaction.options);
}
} }
async sendSayMessage(isSlashCommand: boolean, data: any, args: any) { async process(data: Interaction | Message, args: any) {
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
const isMessage = data instanceof Message;
if (!data.member.permissions.has([Permissions.FLAGS.VIEW_CHANNEL, Permissions.FLAGS.SEND_MESSAGES, Permissions.FLAGS.MANAGE_CHANNELS])) if(!isSlashCommand && !isMessage) return;
return await sendReply(data, {
embeds: [makeErrorEmbed ({ let query;
title: 'You need ``VIEW_CHANNEL, SEND_MESSAGES and MANAGE_CHANNELS`` permission on this guild!',
user: isSlashCommand ? data.user : data.author
})]
});
if(isMessage) {
if(data === null || !data.guildId || data.member === null || data.guild === null) return;
if (!data.member.permissions.has([Permissions.FLAGS.VIEW_CHANNEL, Permissions.FLAGS.SEND_MESSAGES, Permissions.FLAGS.MANAGE_CHANNELS]))
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.NO_PERMISSION(data)] });
if(args.length === 0) if(args.length === 0) {
return await sendReply(data, { return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.SAY_INFO(data)] });
embeds: [makeErrorEmbed({ }
title: `No message defined`, query = args.join(" ");
user: isSlashCommand ? data.user : data.author
})] }
}); else if(isSlashCommand) {
if(!data.guild!.members.cache.get(data.user.id)?.permissions.has([Permissions.FLAGS.ADMINISTRATOR]))
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.NO_PERMISSION(data)] });
query = data.options.getString('message');
}
if(isSlashCommand) { if(isSlashCommand) {
await data.channel.send({ content: args }); await data.channel!.send({ content: query });
await data.reply({ ephemeral: true, embeds: [makeSuccessEmbed({ await data.reply({ ephemeral: true, embeds: [EMBEDS.SUCCESSFULLY_SAID(data)] });
title: `Successfully said`,
user: isSlashCommand ? data.user : data.author
})] });
} }
else { else {
await data.channel.send({ content: args.join(" ") }); if(data.deletable)
await data.delete();
await data.channel.send({ content: query });
} }
} }