ui updates and bot updates. new commands and command handler
This commit is contained in:
@@ -1,63 +1,116 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { SlashCommandBuilder, ActionRowBuilder, ChannelSelectMenuBuilder, StringSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ComponentType } = require('discord.js');
|
||||
const { readDb, writeDb } = require('../../backend/db.js');
|
||||
|
||||
const defaultWelcomeMessages = ["Welcome to the server, {user}!", "Hey {user}, welcome!", "{user} has joined the party!"];
|
||||
|
||||
module.exports = {
|
||||
name: 'config-welcome',
|
||||
description: 'Configure the welcome message for this server.',
|
||||
name: 'setup-welcome',
|
||||
description: 'Interactively set up the welcome message for this server.',
|
||||
enabled: true,
|
||||
builder: new SlashCommandBuilder()
|
||||
.setName('config-welcome')
|
||||
.setDescription('Configure the welcome message for this server.')
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('set-channel')
|
||||
.setDescription('Set the channel for welcome messages.')
|
||||
.addChannelOption(option =>
|
||||
option.setName('channel')
|
||||
.setDescription('The channel to send welcome messages to.')
|
||||
.setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('set-message')
|
||||
.setDescription('Set the welcome message.')
|
||||
.addStringOption(option =>
|
||||
option.setName('message')
|
||||
.setDescription('The welcome message. Use {user} for username mention and {server} for server name.')
|
||||
.setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('disable')
|
||||
.setDescription('Disable welcome messages.')
|
||||
),
|
||||
.setName('setup-welcome')
|
||||
.setDescription('Interactively set up the welcome message for this server.'),
|
||||
async execute(interaction) {
|
||||
const db = readDb();
|
||||
const guildId = interaction.guildId;
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
|
||||
if (!db[guildId]) {
|
||||
db[guildId] = {};
|
||||
}
|
||||
|
||||
if (subcommand === 'set-channel') {
|
||||
const channel = interaction.options.getChannel('channel');
|
||||
db[guildId].welcomeChannel = channel.id;
|
||||
const channelSelect = new ChannelSelectMenuBuilder()
|
||||
.setCustomId('welcome_channel_select')
|
||||
.setPlaceholder('Select the channel for welcome messages.')
|
||||
.setChannelTypes([0]); // Text channels
|
||||
|
||||
const row1 = new ActionRowBuilder().addComponents(channelSelect);
|
||||
|
||||
const channelReply = await interaction.reply({
|
||||
content: 'Please select the channel where you want welcome messages to be sent.',
|
||||
components: [row1],
|
||||
flags: 64,
|
||||
});
|
||||
|
||||
try {
|
||||
const channelConfirmation = await channelReply.awaitMessageComponent({
|
||||
componentType: ComponentType.ChannelSelect,
|
||||
time: 60000,
|
||||
});
|
||||
|
||||
const channelId = channelConfirmation.values[0];
|
||||
db[guildId].welcomeChannel = channelId;
|
||||
db[guildId].welcomeEnabled = true;
|
||||
writeDb(db);
|
||||
await interaction.reply(`Welcome channel set to ${channel}.`);
|
||||
} else if (subcommand === 'set-message') {
|
||||
const message = interaction.options.getString('message');
|
||||
db[guildId].welcomeMessage = message;
|
||||
db[guildId].welcomeEnabled = true;
|
||||
writeDb(db);
|
||||
await interaction.reply(`Welcome message set to: "${message}"`);
|
||||
} else if (subcommand === 'disable') {
|
||||
db[guildId].welcomeEnabled = false;
|
||||
writeDb(db);
|
||||
await interaction.reply('Welcome messages disabled.');
|
||||
|
||||
const messageOptions = defaultWelcomeMessages.map(msg => ({
|
||||
label: msg.length > 100 ? msg.substring(0, 97) + '...' : msg,
|
||||
value: msg,
|
||||
}));
|
||||
|
||||
messageOptions.push({
|
||||
label: 'Custom Message',
|
||||
value: 'custom',
|
||||
});
|
||||
|
||||
const messageSelect = new StringSelectMenuBuilder()
|
||||
.setCustomId('welcome_message_select')
|
||||
.setPlaceholder('Select a welcome message or create your own.')
|
||||
.addOptions(messageOptions);
|
||||
|
||||
const row2 = new ActionRowBuilder().addComponents(messageSelect);
|
||||
|
||||
await channelConfirmation.update({
|
||||
content: `Channel <#${channelId}> selected. Now, please select a welcome message.`,
|
||||
components: [row2],
|
||||
});
|
||||
|
||||
const messageConfirmation = await channelReply.awaitMessageComponent({
|
||||
componentType: ComponentType.StringSelect,
|
||||
time: 60000,
|
||||
});
|
||||
|
||||
const selectedMessage = messageConfirmation.values[0];
|
||||
|
||||
if (selectedMessage === 'custom') {
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId('welcome_custom_message_modal')
|
||||
.setTitle('Custom Welcome Message');
|
||||
|
||||
const messageInput = new TextInputBuilder()
|
||||
.setCustomId('custom_message_input')
|
||||
.setLabel("Your custom message")
|
||||
.setPlaceholder('Use {user} for username and {server} for server name.')
|
||||
.setStyle(TextInputStyle.Paragraph)
|
||||
.setRequired(true);
|
||||
|
||||
const firstActionRow = new ActionRowBuilder().addComponents(messageInput);
|
||||
modal.addComponents(firstActionRow);
|
||||
|
||||
await messageConfirmation.showModal(modal);
|
||||
|
||||
const modalSubmit = await interaction.awaitModalSubmit({
|
||||
time: 300000,
|
||||
});
|
||||
|
||||
const customMessage = modalSubmit.fields.getTextInputValue('custom_message_input');
|
||||
db[guildId].welcomeMessage = customMessage;
|
||||
writeDb(db);
|
||||
|
||||
await modalSubmit.reply({
|
||||
content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${customMessage}"`,
|
||||
flags: 64,
|
||||
});
|
||||
|
||||
} else {
|
||||
db[guildId].welcomeMessage = selectedMessage;
|
||||
writeDb(db);
|
||||
await messageConfirmation.update({
|
||||
content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${selectedMessage}"`,
|
||||
components: [],
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error during welcome setup:', error);
|
||||
await interaction.editReply({ content: 'Configuration timed out or an error occurred.', components: [] });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user