From c3e8ea2152ba31c360d92ff8d7a2521764e84dfd Mon Sep 17 00:00:00 2001 From: Supositware Date: Tue, 30 Aug 2022 04:14:14 +0200 Subject: [PATCH] Leave and join command (Untested) --- commands/admin/bye.js | 67 ++++++++++++++++++++++++++++++++++++++ commands/admin/welcome.js | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 commands/admin/bye.js create mode 100644 commands/admin/welcome.js diff --git a/commands/admin/bye.js b/commands/admin/bye.js new file mode 100644 index 0000000..553156b --- /dev/null +++ b/commands/admin/bye.js @@ -0,0 +1,67 @@ +import { SlashCommandBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionFlagsBits } from 'discord.js'; +import db from '../../models/index.js'; + +export default { + data: new SlashCommandBuilder() + .setName('bye') + .setDescription('Set a leave message') + .addStringOption(option => + option.setName('message') + .setDescription('The message you want the bot to say when someone leave in the current channel.')), + category: 'admin', + userPermissions: [PermissionFlagsBits.ManageChannels], + async execute(interaction, args, client) { + const leave = await db.leaveChannel.findOne({ where: { guildID: interaction.guild.id } }); + + if (!leave && !args[0]) { + return interaction.reply({ content: 'You need a message for me to say anything!', ephemeral: true }); + } + else if (!leave) { + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + await db.leaveChannel.create(body); + return interaction.reply({ content: `The leave message have been set with ${args.message}`, ephemeral: true }); + } + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId('edit') + .setLabel('Edit') + .setStyle(ButtonStyle.Primary), + ) + .addComponents( + new ButtonBuilder() + .setCustomId('remove') + .setLabel('Remove') + .setStyle(ButtonStyle.Danger), + ) + .addComponents( + new ButtonBuilder() + .setCustomId('nothing') + .setLabel('Do nothing') + .setStyle(ButtonStyle.Secondary), + ); + + await interaction.reply({ content: 'The server already has a message set, do you want to edit it or remove it?', components: [row], ephemeral: true }); + + client.once('interactionCreate', async (interactionMenu) => { + if (!interactionMenu.isButton) return; + interactionMenu.update({ components: [] }); + if (interactionMenu.customId === 'edit') { + if (!args[0]) { + return interaction.reply({ content: 'You need to input a message for me to edit!', ephemeral: true }); + } + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + await db.leaveChannel.update(body, { where: { guildID: interaction.guild.id } }); + return interaction.editReply({ content: `The leave message has been set to ${args[0]}`, ephemeral: true }); + } + else if (interactionMenu.customId === 'remove') { + db.leaveChannel.destroy({ where: { guildID: interaction.guild.id, channelID: interaction.channel.id } }); + return interaction.editReply({ content: 'The leave message has been deleted.', ephemeral: true }); + } + else { + return interaction.editReply({ content: 'Nothing has been changed.', ephemeral: true }); + } + }); + }, +}; diff --git a/commands/admin/welcome.js b/commands/admin/welcome.js new file mode 100644 index 0000000..ac4044a --- /dev/null +++ b/commands/admin/welcome.js @@ -0,0 +1,68 @@ +import { SlashCommandBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionFlagsBits } from 'discord.js'; +import db from '../../models/index.js'; + +export default { + data: new SlashCommandBuilder() + .setName('welcome') + .setDescription('Set a join message') + .addStringOption(option => + option.setName('message') + .setDescription('The message you want the bot to say when someone join in the current channel.')), + category: 'admin', + userPermissions: [PermissionFlagsBits.ManageChannels], + async execute(interaction, args, client) { + const join = await db.joinChannel.findOne({ where: { guildID: interaction.guild.id } }); + + if (!join && !args[0]) { + return interaction.reply({ content: 'You need a message for me to say anything!', ephemeral: true }); + } + else if (!join) { + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + await db.joinChannel.create(body); + return interaction.reply({ content: `The join message have been set with ${args[0]}`, ephemeral: true }); + } + + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId('edit') + .setLabel('Edit') + .setStyle(ButtonStyle.Primary), + ) + .addComponents( + new ButtonBuilder() + .setCustomId('remove') + .setLabel('Remove') + .setStyle(ButtonStyle.Danger), + ) + .addComponents( + new ButtonBuilder() + .setCustomId('nothing') + .setLabel('Do nothing') + .setStyle(ButtonStyle.Secondary), + ); + + await interaction.reply({ content: 'The server already has a message set, do you want to edit it or remove it?', components: [row], ephemeral: true }); + + client.once('interactionCreate', async (interactionMenu) => { + if (!interactionMenu.isButton) return; + interactionMenu.update({ components: [] }); + if (interactionMenu.customId === 'edit') { + if (!args[0]) { + return interaction.reply({ content: 'You need to input a message for me to edit!', ephemeral: true }); + } + const body = { guildID: interaction.guild.id, channelID: interaction.channel.id, message: args[0] }; + await db.joinChannel.update(body, { where: { guildID: interaction.guild.id } }); + return interaction.editReply({ content: `The join message has been set to ${args[0]}`, ephemeral: true }); + } + else if (interactionMenu.customId === 'remove') { + db.joinChannel.destroy({ where: { guildID: interaction.guild.id, channelID: interaction.channel.id } }); + return interaction.editReply({ content: 'The join message has been deleted.', ephemeral: true }); + } + else { + return interaction.editReply({ content: 'Nothing has been changed.', ephemeral: true }); + } + }); + }, +};