Add check to sub/unsub to see if room is already subbed or not

This commit is contained in:
Christopher Cookman 2024-05-16 18:39:26 -06:00
parent d76c2f3192
commit b1db0c1d42
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -775,6 +775,15 @@ discord.on("interactionCreate", async (interaction) => {
return; return;
} }
message = interaction.options.getString("message") || null; message = interaction.options.getString("message") || null;
// Check that the channel isn't already subbed to the room
db.get(`SELECT * FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err, row) => {
if (err) {
console.error(err.message);
interaction.reply({ content: "Failed to subscribe to room", ephemeral: true });
} else if (row) {
return interaction.reply({ content: `Already subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true });
} else {
db.run(`INSERT INTO channels (channelid, iemchannel, custommessage) VALUES (?, ?, ?)`, [interaction.channel.id, room, message], (err) => { db.run(`INSERT INTO channels (channelid, iemchannel, custommessage) VALUES (?, ?, ?)`, [interaction.channel.id, room, message], (err) => {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
@ -783,6 +792,8 @@ discord.on("interactionCreate", async (interaction) => {
interaction.reply({ content: `Subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true }); interaction.reply({ content: `Subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true });
} }
}); });
}
});
break; break;
case "unsubscribe": case "unsubscribe":
// Check that the room is valid // Check that the room is valid
@ -791,6 +802,15 @@ discord.on("interactionCreate", async (interaction) => {
interaction.reply({ content: "Invalid room", ephemeral: true }); interaction.reply({ content: "Invalid room", ephemeral: true });
return; return;
} }
// Check that the channel is subbed to the room
db.get(`SELECT * FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err, row) => {
if (err) {
console.error(err.message);
interaction.reply({ content: "Failed to unsubscribe from room", ephemeral: true });
} else if (!row) {
return interaction.reply({ content: `Not subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true });
} else {
db.run(`DELETE FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err) => { db.run(`DELETE FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err) => {
if (err) { if (err) {
console.error(err.message); console.error(err.message);
@ -799,6 +819,8 @@ discord.on("interactionCreate", async (interaction) => {
interaction.reply({ content: `Unsubscribed from \`${getWFOByRoom(room).location}\``, ephemeral: true }); interaction.reply({ content: `Unsubscribed from \`${getWFOByRoom(room).location}\``, ephemeral: true });
} }
}); });
}
});
break; break;
case "list": case "list":
db.all(`SELECT iemchannel, custommessage FROM channels WHERE channelid = ?`, [interaction.channel.id], (err, rows) => { db.all(`SELECT iemchannel, custommessage FROM channels WHERE channelid = ?`, [interaction.channel.id], (err, rows) => {