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;
case "setReminders":
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)}, `,
`reminder_channel_id = ${db.escape(this.reminderChannelId)}, `,
`watch_channel_id = ${db.escape(this.watchChannelId)} `,
`WHERE guild_id = ${db.escape(this.guildId)}`
`watch_channel_id = ${db.escape(this.watchChannelId)}`
];
return queryParts.join('');
break;

View File

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

View File

@ -1,35 +1,68 @@
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const { GuildInfo } = require('../modules/CustomClasses.js');
const dbfn = require('../modules/dbfn.js');
const fn = require('../modules/functions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('notifications')
.setDescription('Setup a notification relay for improved water and fruit notifications')
.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')
o.setName('watchchannel')
.setDescription('The channel Grow A Tree sends your notifications in')
.setRequired(true))
.setRequired(true)
)
.addStringOption(o =>
o
.setName('watermessage')
o.setName('watermessage')
.setDescription('Message to send for water reminders')
.setRequired(true))
.setRequired(true)
)
.addChannelOption(o =>
o
.setName('pingchannel')
o.setName('pingchannel')
.setDescription('The channel to send the water reminder in')
.setRequired(true))
.setRequired(true)
)
.addStringOption(o =>
o
.setName('fruitmessage')
o.setName('fruitmessage')
.setDescription("Message to send for fruit reminders")
.setRequired(false))
.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),
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) {
case "set":
if (interaction.client.guildInfos.has(interaction.guildId)) {
const watchChannel = interaction.options.getChannel('watchchannel');
const waterMessage = interaction.options.getString('watermessage');
@ -38,10 +71,63 @@ module.exports = {
let guildInfo = interaction.client.guildInfos.get(interaction.guildId);
guildInfo.setReminders(waterMessage, fruitMessage, reminderChannel.id, watchChannel.id);
let query = guildInfo.queryBuilder("setReminders");
console.log(query);
await dbfn.setGuildInfo(query);
await interaction.editReply(`I'll watch <#${watchChannel.id}> for Grow A Tree Notifications and relay them to <#${reminderChannel.id}>.`).catch(e => console.error(e));
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) {
console.error("Error occurred while setting up a notification relay: " + err);