Implement updating of notifications piecemeal

This commit is contained in:
Skylar Grant 2023-02-19 18:37:13 -05:00
parent c3d3be33d7
commit 4846a36e55
3 changed files with 130 additions and 38 deletions

View File

@ -102,11 +102,16 @@ module.exports = {
break; break;
case "setReminders": case "setReminders":
queryParts = [ queryParts = [
`UPDATE guild_info SET water_message = ${db.escape(this.waterMessage)}, `, `INSERT INTO guild_info (guild_id, water_message, fruit_message, reminder_channel_id, watch_channel_id) VALUES (`,
`${db.escape(this.guildId)},`,
`${db.escape(this.waterMessage)},`,
`${db.escape(this.fruitMessage)},`,
`${db.escape(this.reminderChannelId)},`,
`${db.escape(this.watchChannelId)}`,
`) ON DUPLICATE KEY UPDATE water_message = ${db.escape(this.waterMessage)}, `,
`fruit_message = ${db.escape(this.fruitMessage)}, `, `fruit_message = ${db.escape(this.fruitMessage)}, `,
`reminder_channel_id = ${db.escape(this.reminderChannelId)}, `, `reminder_channel_id = ${db.escape(this.reminderChannelId)}, `,
`watch_channel_id = ${db.escape(this.watchChannelId)} `, `watch_channel_id = ${db.escape(this.watchChannelId)}`
`WHERE guild_id = ${db.escape(this.guildId)}`
]; ];
return queryParts.join(''); return queryParts.join('');
break; break;

View File

@ -548,9 +548,10 @@ const functions = {
await interaction.update(this.builders.errorEmbed(findMessagesResponse.status)); await interaction.update(this.builders.errorEmbed(findMessagesResponse.status));
} }
}, },
reset(guildId) { reset(interaction) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
dbfn.deleteGuildInfo(guildId).then(res => { dbfn.deleteGuildInfo(interaction.guildId).then(res => {
functions.collectionBuilders.guildInfos(interaction.client);
resolve(res); resolve(res);
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);

View File

@ -1,47 +1,133 @@
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js'); const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const { GuildInfo } = require('../modules/CustomClasses.js');
const dbfn = require('../modules/dbfn.js'); const dbfn = require('../modules/dbfn.js');
const fn = require('../modules/functions.js'); const fn = require('../modules/functions.js');
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('notifications') .setName('notifications')
.setDescription('Setup a notification relay for improved water and fruit notifications') .setDescription('A notification relay for improved water and fruit notifications')
.addChannelOption(o => .addSubcommand(sc =>
o sc.setName('set')
.setName('watchchannel') .setDescription('Set up the notification relay for the first time')
.setDescription('The channel Grow A Tree sends your notifications in') .addChannelOption(o =>
.setRequired(true)) o.setName('watchchannel')
.addStringOption(o => .setDescription('The channel Grow A Tree sends your notifications in')
o .setRequired(true)
.setName('watermessage') )
.setDescription('Message to send for water reminders') .addStringOption(o =>
.setRequired(true)) o.setName('watermessage')
.addChannelOption(o => .setDescription('Message to send for water reminders')
o .setRequired(true)
.setName('pingchannel') )
.setDescription('The channel to send the water reminder in') .addChannelOption(o =>
.setRequired(true)) o.setName('pingchannel')
.addStringOption(o => .setDescription('The channel to send the water reminder in')
o .setRequired(true)
.setName('fruitmessage') )
.setDescription("Message to send for fruit reminders") .addStringOption(o =>
.setRequired(false)) 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)
)
)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles), .setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles),
async execute(interaction) { async execute(interaction) {
try { try {
await interaction.deferReply({ ephemeral: true }); await interaction.deferReply({ ephemeral: true });
if (interaction.client.guildInfos.has(interaction.guildId)) { const subcommand = interaction.options.getSubcommand();
const watchChannel = interaction.options.getChannel('watchchannel'); // if (process.env.DEBUG) console.log(`${typeof subcommand}: ${subcommand}`);
const waterMessage = interaction.options.getString('watermessage'); switch (subcommand) {
const fruitMessage = interaction.options.getString('fruitmessage') ? interaction.options.getString('fruitmessage') : interaction.options.getString('watermessage'); case "set":
const reminderChannel = interaction.options.getChannel('pingchannel'); if (interaction.client.guildInfos.has(interaction.guildId)) {
let guildInfo = interaction.client.guildInfos.get(interaction.guildId); const watchChannel = interaction.options.getChannel('watchchannel');
guildInfo.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id); const waterMessage = interaction.options.getString('watermessage');
let query = guildInfo.queryBuilder("setReminders"); const fruitMessage = interaction.options.getString('fruitmessage') ? interaction.options.getString('fruitmessage') : interaction.options.getString('watermessage');
console.log(query); const reminderChannel = interaction.options.getChannel('pingchannel');
await dbfn.setGuildInfo(query); let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
await interaction.editReply(`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`).catch(e => console.error(e)); guildInfo.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id);
fn.collectionBuilders.guildInfos(interaction.client); let query = guildInfo.queryBuilder("setReminders");
await dbfn.setGuildInfo(query);
const replyParts = [
`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`,
`Water Message: ${waterMessage}`
];
if (fruitMessage != "") replyParts.push(`Fruit Message: ${fruitMessage}`);
await interaction.editReply(replyParts.join("\n")).catch(e => console.error(e));
fn.collectionBuilders.guildInfos(interaction.client);
} else {
const watchChannel = interaction.options.getChannel('watchchannel');
const waterMessage = interaction.options.getString('watermessage');
const fruitMessage = interaction.options.getString('fruitmessage') ? interaction.options.getString('fruitmessage') : interaction.options.getString('watermessage');
const reminderChannel = interaction.options.getChannel('pingchannel');
let guildInfo = new GuildInfo()
.setId(interaction.guildId)
.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id);
let query = guildInfo.queryBuilder("setReminders");
await dbfn.setGuildInfo(query);
const replyParts = [
`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`,
`Water Message: ${waterMessage}`
];
if (fruitMessage != "") replyParts.push(`Fruit Message: ${fruitMessage}`);
await interaction.editReply(replyParts.join("\n")).catch(e => console.error(e));
fn.collectionBuilders.guildInfos(interaction.client);
}
break;
case "update":
if (interaction.client.guildInfos.has(interaction.guildId)) {
let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
const inWatchChannel = interaction.options.getChannel('watchchannel');
const inWaterMessage = interaction.options.getString('watermessage');
const inFruitMessage = interaction.options.getString('fruitmessage');
const inReminderChannel = interaction.options.getChannel('pingchannel');
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;
guildInfo.setReminders(outWaterMessage, outFruitMessage, outReminderChannelId, outWatchChannelId);
let query = guildInfo.queryBuilder("setReminders");
await dbfn.setGuildInfo(query);
const replyParts = [
`I'll watch <#${outWatchChannelId}> for Grow A Tree Notifications and relay them to <#${outReminderChannelId}>.`,
`Water Message: ${outWaterMessage}`
];
if (outFruitMessage != "") replyParts.push(`Fruit Message: ${outFruitMessage}`);
await interaction.editReply(replyParts.join("\n")).catch(e => console.error(e));
fn.collectionBuilders.guildInfos(interaction.client);
} else {
await interaction.editReply(fn.builders.errorEmbed("There is no existing notification relay to update!")).catch(e => console.error(e));
}
break;
default:
await interaction.editReply(fn.builders.errorEmbed("Invalid subcommand detected.")).catch(e => console.error(e));
break;
} }
} catch (err) { } catch (err) {
console.error("Error occurred while setting up a notification relay: " + err); console.error("Error occurred while setting up a notification relay: " + err);