2023-02-20 01:42:14 +00:00
|
|
|
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
|
|
|
|
const { GuildInfo } = require('../modules/CustomClasses.js');
|
|
|
|
const dbfn = require('../modules/dbfn.js');
|
|
|
|
const fn = require('../modules/functions.js');
|
|
|
|
const strings = require('../data/strings.json');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
2023-02-20 19:07:38 +00:00
|
|
|
.setName('relay')
|
2023-02-20 01:42:14 +00:00
|
|
|
.setDescription('A notification relay for improved water and fruit notifications')
|
|
|
|
.addSubcommand(sc =>
|
|
|
|
sc.setName('set')
|
|
|
|
.setDescription('Set up the notification relay for the first time')
|
|
|
|
.addChannelOption(o =>
|
|
|
|
o.setName('watchchannel')
|
|
|
|
.setDescription('The channel Grow A Tree sends your notifications in')
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
.addStringOption(o =>
|
|
|
|
o.setName('watermessage')
|
|
|
|
.setDescription('Message to send for water reminders')
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
.addChannelOption(o =>
|
|
|
|
o.setName('pingchannel')
|
|
|
|
.setDescription('The channel to send the water reminder in')
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
.addStringOption(o =>
|
|
|
|
o.setName('fruitmessage')
|
|
|
|
.setDescription("Message to send for fruit reminders")
|
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.addSubcommand(sc =>
|
|
|
|
sc.setName('update')
|
|
|
|
.setDescription('Update an already setup notification relay')
|
|
|
|
.addChannelOption(o =>
|
|
|
|
o.setName('watchchannel')
|
|
|
|
.setDescription('The channel Grow A Tree sends your notifications in')
|
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
.addStringOption(o =>
|
|
|
|
o.setName('watermessage')
|
|
|
|
.setDescription('Message to send for water reminders')
|
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
.addChannelOption(o =>
|
|
|
|
o.setName('pingchannel')
|
|
|
|
.setDescription('The channel to send the water reminder in')
|
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
.addStringOption(o =>
|
|
|
|
o.setName('fruitmessage')
|
|
|
|
.setDescription("Message to send for fruit reminders")
|
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.addSubcommand(sc =>
|
|
|
|
sc.setName('disable')
|
|
|
|
.setDescription('Disable the notification relay')
|
|
|
|
)
|
|
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles),
|
|
|
|
async execute(interaction) {
|
|
|
|
try {
|
|
|
|
await interaction.deferReply({ ephemeral: true });
|
|
|
|
const subcommand = interaction.options.getSubcommand();
|
|
|
|
// if (process.env.DEBUG) console.log(`${typeof subcommand}: ${subcommand}`);
|
|
|
|
switch (subcommand) {
|
2023-02-20 19:07:38 +00:00
|
|
|
// Set all components for the first time
|
2023-02-20 01:42:14 +00:00
|
|
|
case "set":
|
2023-02-20 19:07:38 +00:00
|
|
|
// If there is already a guildInfo object for this server
|
2023-02-20 01:42:14 +00:00
|
|
|
if (interaction.client.guildInfos.has(interaction.guildId)) {
|
2023-02-20 19:07:38 +00:00
|
|
|
let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
|
|
|
|
// Get options from the interaction
|
2023-02-20 01:42:14 +00:00
|
|
|
const watchChannel = interaction.options.getChannel('watchchannel');
|
|
|
|
const waterMessage = interaction.options.getString('watermessage');
|
2023-02-20 19:07:38 +00:00
|
|
|
// If the fruit message is set, use it, otherwise default to the water message.
|
2023-02-20 01:42:14 +00:00
|
|
|
const fruitMessage = interaction.options.getString('fruitmessage') ? interaction.options.getString('fruitmessage') : interaction.options.getString('watermessage');
|
|
|
|
const reminderChannel = interaction.options.getChannel('pingchannel');
|
2023-02-20 19:07:38 +00:00
|
|
|
// Set the reminder configuration in the GuildInfo object
|
2023-02-20 01:42:14 +00:00
|
|
|
guildInfo.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id, true);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Update the guildInfos Collection
|
|
|
|
interaction.client.guildInfos.set(interaction.guildId, guildInfo);
|
|
|
|
// Build a query to update the database
|
2023-02-20 01:42:14 +00:00
|
|
|
let query = guildInfo.queryBuilder("setReminders");
|
2023-02-20 19:07:38 +00:00
|
|
|
// Run the query
|
2023-02-20 01:42:14 +00:00
|
|
|
await dbfn.setGuildInfo(query);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Set up a collector on the watch channel
|
|
|
|
fn.collectors.create(interaction.client, guildInfo);
|
|
|
|
// Compose a reply
|
|
|
|
const reply = [
|
2023-02-20 01:42:14 +00:00
|
|
|
`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`,
|
2023-02-20 19:07:38 +00:00
|
|
|
`Water Message: ${waterMessage}`,
|
|
|
|
`Fruit Message: ${fruitMessage}`
|
|
|
|
].join("\n");
|
|
|
|
// Send the reply
|
|
|
|
await interaction.editReply(fn.builders.embed(reply)).catch(e => console.error(e));
|
2023-02-20 01:42:14 +00:00
|
|
|
} else {
|
2023-02-20 19:07:38 +00:00
|
|
|
// Get options from the interaction
|
2023-02-20 01:42:14 +00:00
|
|
|
const watchChannel = interaction.options.getChannel('watchchannel');
|
|
|
|
const waterMessage = interaction.options.getString('watermessage');
|
2023-02-20 19:07:38 +00:00
|
|
|
// If the fruit message is set, use it. Otherwise default to the water message
|
2023-02-20 01:42:14 +00:00
|
|
|
const fruitMessage = interaction.options.getString('fruitmessage') ? interaction.options.getString('fruitmessage') : interaction.options.getString('watermessage');
|
|
|
|
const reminderChannel = interaction.options.getChannel('pingchannel');
|
2023-02-20 19:07:38 +00:00
|
|
|
// Create a new GuildInfo object
|
2023-02-20 01:42:14 +00:00
|
|
|
let guildInfo = new GuildInfo()
|
|
|
|
.setId(interaction.guildId)
|
2023-02-20 19:07:38 +00:00
|
|
|
// Set the reminder configuration
|
2023-02-20 01:42:14 +00:00
|
|
|
.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id, true);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Update the guildInfos Collection
|
|
|
|
interaction.client.guildInfos.set(interaction.guildId, guildInfo);
|
|
|
|
// Build a query to update the database
|
2023-02-20 01:42:14 +00:00
|
|
|
let query = guildInfo.queryBuilder("setReminders");
|
2023-02-20 19:07:38 +00:00
|
|
|
// Run the query
|
2023-02-20 01:42:14 +00:00
|
|
|
await dbfn.setGuildInfo(query);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Create a messageCollector on the watch channel
|
|
|
|
fn.collectors.create(interaction.client, guildInfo);
|
|
|
|
// Compose a reply
|
|
|
|
const reply = [
|
2023-02-20 01:42:14 +00:00
|
|
|
`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`,
|
2023-02-20 19:07:38 +00:00
|
|
|
`Water Message: ${waterMessage}`,
|
|
|
|
`Fruit Message: ${fruitMessage}`
|
|
|
|
].join("\n");
|
|
|
|
// Send the reply
|
|
|
|
await interaction.editReply(reply).catch(e => console.error(e));
|
2023-02-20 01:42:14 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-02-20 19:07:38 +00:00
|
|
|
case "update": // Update the relay configuration piecemeal
|
2023-02-20 01:42:14 +00:00
|
|
|
if (interaction.client.guildInfos.has(interaction.guildId)) {
|
|
|
|
let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
|
2023-02-20 19:07:38 +00:00
|
|
|
|
|
|
|
// Get all possible options from the interaction
|
2023-02-20 01:42:14 +00:00
|
|
|
const inWatchChannel = interaction.options.getChannel('watchchannel');
|
|
|
|
const inWaterMessage = interaction.options.getString('watermessage');
|
|
|
|
const inFruitMessage = interaction.options.getString('fruitmessage');
|
|
|
|
const inReminderChannel = interaction.options.getChannel('pingchannel');
|
|
|
|
|
2023-02-20 19:07:38 +00:00
|
|
|
// Check if each option is set, if it is, use it. Otherwise use what was already set
|
2023-02-20 01:42:14 +00:00
|
|
|
const outWatchChannelId = inWatchChannel ? inWatchChannel.id : guildInfo.watchChannelId;
|
|
|
|
const outWaterMessage = inWaterMessage ? inWaterMessage : guildInfo.waterMessage;
|
|
|
|
const outFruitMessage = inFruitMessage ? inFruitMessage : guildInfo.fruitMessage;
|
|
|
|
const outReminderChannelId = inReminderChannel ? inReminderChannel.id : guildInfo.reminderChannelId;
|
2023-02-20 19:07:38 +00:00
|
|
|
|
|
|
|
// Update the relay configuration
|
2023-02-20 01:42:14 +00:00
|
|
|
guildInfo.setReminders(outWaterMessage, outFruitMessage, outReminderChannelId, outWatchChannelId, true);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Update the guildInfos Collection
|
|
|
|
interaction.client.guildInfos.set(interaction.guildId, guildInfo);
|
|
|
|
// Build a query to update the database
|
2023-02-20 01:42:14 +00:00
|
|
|
let query = guildInfo.queryBuilder("setReminders");
|
2023-02-20 19:07:38 +00:00
|
|
|
// Run the query
|
2023-02-20 01:42:14 +00:00
|
|
|
await dbfn.setGuildInfo(query);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Create a messageCollector on the watch channel
|
|
|
|
fn.collectors.create(interaction.client, guildInfo);
|
|
|
|
// Compose a reply
|
|
|
|
const reply = [
|
2023-02-20 01:42:14 +00:00
|
|
|
`I'll watch <#${outWatchChannelId}> for Grow A Tree Notifications and relay them to <#${outReminderChannelId}>.`,
|
2023-02-20 19:07:38 +00:00
|
|
|
`Water Message: ${outWaterMessage}`,
|
|
|
|
`Fruit Message: ${outFruitMessage}`
|
|
|
|
].join("\n");
|
|
|
|
// Send the reply
|
|
|
|
await interaction.editReply(reply).catch(e => console.error(e));
|
2023-02-20 01:42:14 +00:00
|
|
|
} else {
|
|
|
|
await interaction.editReply(fn.builders.errorEmbed("There is no existing notification relay to update!")).catch(e => console.error(e));
|
|
|
|
}
|
|
|
|
break;
|
2023-02-20 19:07:38 +00:00
|
|
|
case 'disable': // Disable the relay
|
2023-02-20 01:42:14 +00:00
|
|
|
if (interaction.client.guildInfos.has(interaction.guildId)) {
|
|
|
|
let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Update the relay config with all undefined except for `enabled` which is false
|
2023-02-20 01:42:14 +00:00
|
|
|
guildInfo.setReminders(undefined, undefined, undefined, undefined, false);
|
2023-02-20 19:07:38 +00:00
|
|
|
// Update the guildInfos Collection
|
|
|
|
interaction.client.guildInfos.set(interaction.guildId, guildInfo);
|
|
|
|
// Update the database
|
2023-02-20 01:42:14 +00:00
|
|
|
await dbfn.setGuildInfo(guildInfo.queryBuilder("setReminders")).catch(e => console.error(e));
|
2023-02-20 19:07:38 +00:00
|
|
|
// Close the collector
|
|
|
|
await fn.collectors.end(interaction.client, guildInfo).catch(e => console.error(e));
|
|
|
|
// Reply confirming disabling of relay
|
2023-02-20 01:42:14 +00:00
|
|
|
await interaction.editReply(fn.builders.embed(strings.status.optout)).catch(e => console.error(e));
|
|
|
|
} else {
|
|
|
|
await interaction.editReply(fn.builders.errorEmbed("A notification relay has not been set up yet!")).catch(e => console.error(e));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
await interaction.editReply(fn.builders.errorEmbed("Invalid subcommand detected.")).catch(e => console.error(e));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error occurred while setting up a notification relay: " + err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|