From 64596839adf0c08ed76d6c5cd2bfcb0deb7e80d2 Mon Sep 17 00:00:00 2001 From: kingomes <83099848+kingomes@users.noreply.github.com> Date: Thu, 7 Aug 2025 00:04:17 -0500 Subject: [PATCH] enter and leave giveaway should work with multiple giveaways (not end giveaway) --- src/commands/giveaway.ts | 227 ++++++++++++------------- src/commands/handlers/giveawayEnter.ts | 68 ++++++++ src/commands/handlers/giveawayLeave.ts | 61 +++++++ src/utils/db.ts | 2 +- 4 files changed, 242 insertions(+), 116 deletions(-) create mode 100644 src/commands/handlers/giveawayEnter.ts create mode 100644 src/commands/handlers/giveawayLeave.ts diff --git a/src/commands/giveaway.ts b/src/commands/giveaway.ts index ffa1683..03cd9aa 100644 --- a/src/commands/giveaway.ts +++ b/src/commands/giveaway.ts @@ -102,96 +102,127 @@ export default commandModule({ .then((embedMessage) => { let giveawayEnded = false; - db.prepare(`INSERT INTO giveaway_message(message_id, host_id) VALUES (?, ?)`).run( + db.prepare(`INSERT INTO giveaway_message(message_id, end_time, host_id) VALUES (?, ?, ?)`).run( embedMessage.id, + endTime.getTime(), ctx.userId ); - deps["@sern/client"].on("interactionCreate", async (interaction) => { - if (interaction.isButton()) { - if (interaction.customId === "enter" || interaction.customId === "leave") { - const messageId = interaction.message.id; - console.log(`Message ID: ${messageId}`); - const userId = interaction.user.id; + // deps["@sern/client"].on("interactionCreate", async (interaction) => { + // if (interaction.isButton()) { + // if (interaction.customId === "enter" || interaction.customId === "leave") { + // const messageId = interaction.message.id; + // console.log(`Message ID: ${messageId}`); + // const userId = interaction.user.id; - const host = db - .prepare( - `SELECT host_id FROM giveaway_message WHERE message_id = ?` - ) - .get(messageId); + // const host = db + // .prepare( + // `SELECT host_id FROM giveaway_message WHERE message_id = ?` + // ) + // .get(messageId); - if (interaction.customId === "enter") { - // Prevent host from entering - // if (host && host.host_id === userId) { - // await interaction.reply({ ephemeral: true, content: `You cannot enter the giveaway as the host!` }); - // return; - // } - // Check if already entered - const checkUser = db - .prepare( - `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` - ) - .get(messageId, userId); - if (checkUser.count === 0) { - db.prepare( - `INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)` - ).run(messageId, Date.now(), userId); - await interaction.reply({ - ephemeral: true, - content: `Giveaway entered!`, - }); - } else { - await interaction.reply({ - ephemeral: true, - content: `You are already entered!`, - }); - } - } else { - // Leave giveaway - const checkUser = db - .prepare( - `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` - ) - .get(messageId, userId); - if (checkUser.count === 1) { - db.prepare( - `DELETE FROM entries WHERE message_id = ? AND user_id = ?` - ).run(messageId, userId); - await interaction.reply({ - ephemeral: true, - content: `Giveaway left`, - }); - } else { - await interaction.reply({ - ephemeral: true, - content: `You cannot leave a giveaway you were not entered in`, - }); - } - } + // if (interaction.customId === "enter") { + // // Prevent host from entering + // // if (host && host.host_id === userId) { + // // await interaction.reply({ ephemeral: true, content: `You cannot enter the giveaway as the host!` }); + // // return; + // // } + // // Check if already entered + // const checkUser = db + // .prepare( + // `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` + // ) + // .get(messageId, userId); + // if (checkUser.count === 0) { + // db.prepare( + // `INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)` + // ).run(messageId, Date.now(), userId); + // await interaction.reply({ + // ephemeral: true, + // content: `Giveaway entered!`, + // }); + // } else { + // await interaction.reply({ + // ephemeral: true, + // content: `You are already entered!`, + // }); + // } + // } else { + // // Leave giveaway + // const checkUser = db + // .prepare( + // `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` + // ) + // .get(messageId, userId); + // if (checkUser.count === 1) { + // db.prepare( + // `DELETE FROM entries WHERE message_id = ? AND user_id = ?` + // ).run(messageId, userId); + // await interaction.reply({ + // ephemeral: true, + // content: `Giveaway left`, + // }); + // } else { + // await interaction.reply({ + // ephemeral: true, + // content: `You cannot leave a giveaway you were not entered in`, + // }); + // } + // } - // Get updated entry count - const entryCount = db - .prepare( - `SELECT COUNT(*) as count FROM entries WHERE message_id = ?` - ) - .get(messageId).count; + // // Get updated entry count + // const entryCount = db + // .prepare( + // `SELECT COUNT(*) as count FROM entries WHERE message_id = ?` + // ) + // .get(messageId).count; - // Edit embed - const embed = EmbedBuilder.from( - interaction.message.embeds[0] - ).spliceFields(0, 1, { - name: "\u200b", - value: `Hosted by: <@${host?.host_id ?? "unknown"}> - Entries: ${entryCount} - Ends: ${new Timestamp( - Number(endTimeStamp2) - ).getRelativeTime()} (${endTimeStamp})`, - }); + // // Edit embed + // const embed = EmbedBuilder.from( + // interaction.message.embeds[0] + // ).spliceFields(0, 1, { + // name: "\u200b", + // value: `Hosted by: <@${host?.host_id ?? "unknown"}> + // Entries: ${entryCount} + // Ends: ${new Timestamp( + // Number(endTimeStamp2) + // ).getRelativeTime()} (${endTimeStamp})`, + // }); - await interaction.message.edit({ embeds: [embed] }); - } - } - }); + // await interaction.message.edit({ embeds: [embed] }); + // } + + // if (interaction.customId === "edit" || interaction.customId === "end") { + // // if (!ownerIDs.includes(interaction.user.id)) { + // // await interaction.reply({ ephemeral: true, content: `You do not have permission to edit or end this giveaway!` }); + // // return; + // //} + // if (interaction.customId === "edit") { + // // Handle editing the giveaway + // await interaction.reply({ + // ephemeral: true, + // content: `Editing the giveaway is not implemented yet.`, + // }); + // } else if (interaction.customId === "end") { + // // Handle ending the giveaway + // if (giveawayEnded) { + // await interaction.reply({ + // ephemeral: true, + // content: `This giveaway has already ended!`, + // }); + // return; + // } + // giveawayEnded = true; + // clearTimeout(giveawayTimeout); + // await interaction.reply({ + // ephemeral: true, + // content: `Giveaway ended by <@${interaction.user.id}>`, + // }); + // endGiveaway(); + // } + // } + // } + // }); // test entries // db.prepare(`INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)`).run([embedMessage.id, 1, 1]) @@ -202,40 +233,6 @@ export default commandModule({ let intervalTime = endTime.getTime() - startTime.getTime(); - deps["@sern/client"].on("interactionCreate", async (interaction) => { - if (interaction.isButton()) { - if (interaction.customId === "edit" || interaction.customId === "end") { - // if (!ownerIDs.includes(interaction.user.id)) { - // await interaction.reply({ ephemeral: true, content: `You do not have permission to edit or end this giveaway!` }); - // return; - //} - if (interaction.customId === "edit") { - // Handle editing the giveaway - await interaction.reply({ - ephemeral: true, - content: `Editing the giveaway is not implemented yet.`, - }); - } else if (interaction.customId === "end") { - // Handle ending the giveaway - if (giveawayEnded) { - await interaction.reply({ - ephemeral: true, - content: `This giveaway has already ended!`, - }); - return; - } - giveawayEnded = true; - clearTimeout(giveawayTimeout); - await interaction.reply({ - ephemeral: true, - content: `Giveaway ended by <@${interaction.user.id}>`, - }); - endGiveaway(); - } - } - } - }); - function endGiveaway() { const stmt = db .prepare(`SELECT * FROM entries WHERE message_id = ?`) diff --git a/src/commands/handlers/giveawayEnter.ts b/src/commands/handlers/giveawayEnter.ts new file mode 100644 index 0000000..c967f07 --- /dev/null +++ b/src/commands/handlers/giveawayEnter.ts @@ -0,0 +1,68 @@ +import { commandModule, CommandType } from "@sern/handler"; +import { db } from "../../utils/db.js"; +import { EmbedBuilder } from "discord.js"; +import { Timestamp } from "#utils"; + +export default commandModule({ + type: CommandType.Button, + name: "enter", + async execute(ctx) { + const startTime = ctx.message.createdTimestamp; + + const messages = db.prepare(`SELECT * FROM giveaway_message`).all(); + + messages.map(async (message: { message_id: string; end_time: number, host_id: string }) => { + if (ctx.message.id === message.message_id && + !ctx.user.bot + ) { + const host = db + .prepare( + `SELECT host_id FROM giveaway_message WHERE message_id = ?` + ) + .get(message.message_id); + // if (host && host.host_id === ctx.user.id) { + // await ctx.reply({ ephemeral: true, content: `You cannot enter the giveaway as the host!` }); + // return; + // } + + const checkUser = db + .prepare( + `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` + ) + .get(message.message_id, ctx.user.id); + + if (checkUser.count === 0) { + db.prepare( + `INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)` + ).run([message.message_id, startTime, ctx.user.id]); + await ctx.reply({ ephemeral: true, content: `Giveaway entered!` }) + } + else await ctx.reply({ ephemeral: true, content: `You are already entered!` }) + + const entryCount = db + .prepare( + `SELECT COUNT(*) as count FROM entries WHERE message_id = ?` + ) + .get(message.message_id).count; + + const endTime = message.end_time + + const endTimeStamp: string = `` + const endTimeStamp2 = new Timestamp(endTime).timestamp + + const embed = EmbedBuilder.from( + ctx.message.embeds[0] + ).spliceFields(0, 1, { + name: "\u200b", + value: `Hosted by: <@${host?.host_id ?? "unknown"}> + Entries: ${entryCount} + Ends: ${new Timestamp( + Number(endTimeStamp2) + ).getRelativeTime()} (${endTimeStamp})`, + }); + + await ctx.message.edit({ embeds: [embed] }); + } + }); + }, +}); diff --git a/src/commands/handlers/giveawayLeave.ts b/src/commands/handlers/giveawayLeave.ts new file mode 100644 index 0000000..5ceacfa --- /dev/null +++ b/src/commands/handlers/giveawayLeave.ts @@ -0,0 +1,61 @@ +import { commandModule, CommandType } from "@sern/handler"; +import { db } from "../../utils/db.js"; +import { Timestamp } from "#utils"; +import { EmbedBuilder } from "discord.js"; + +export default commandModule({ + type: CommandType.Button, + name: "leave", + async execute(ctx) { + const deletedId = ctx.user.id + + const message = db + .prepare(`SELECT * FROM giveaway_message WHERE message_id = ?`) + .get(ctx.message.id); + + const host = db + .prepare( + `SELECT host_id FROM giveaway_message WHERE message_id = ?` + ) + .get(message.message_id); + + const checkUser = db + .prepare( + `SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?` + ) + .get(message.message_id, ctx.user.id); + + if (ctx.message.id === message.message_id && checkUser.count == 1) { + db.prepare(`DELETE FROM entries WHERE message_id = ? AND user_id = ?`).run( + message.message_id, + deletedId + ); + await ctx.reply({ ephemeral: true, content: `Giveaway left` }); + } + else await ctx.reply({ ephemeral: true, content: `You cannot leave a giveaway you were not entered in` }); + + const entryCount = db + .prepare( + `SELECT COUNT(*) as count FROM entries WHERE message_id = ?` + ) + .get(message.message_id).count; + + const endTime = message.end_time + + const endTimeStamp: string = `` + const endTimeStamp2 = new Timestamp(endTime).timestamp + + const embed = EmbedBuilder.from( + ctx.message.embeds[0] + ).spliceFields(0, 1, { + name: "\u200b", + value: `Hosted by: <@${host?.host_id ?? "unknown"}> + Entries: ${entryCount} + Ends: ${new Timestamp( + Number(endTimeStamp2) + ).getRelativeTime()} (${endTimeStamp})`, + }); + + await ctx.message.edit({ embeds: [embed] }); + }, +}); diff --git a/src/utils/db.ts b/src/utils/db.ts index dd2cc9f..2a33a92 100644 --- a/src/utils/db.ts +++ b/src/utils/db.ts @@ -4,4 +4,4 @@ db.pragma('journal_mode = WAL'); db.exec(`CREATE TABLE IF NOT EXISTS entries(message_id, timestamp, user_id)`); -db.exec(`CREATE TABLE IF NOT EXISTS giveaway_message(message_id, host_id)`) \ No newline at end of file +db.exec(`CREATE TABLE IF NOT EXISTS giveaway_message(message_id, end_time, host_id)`) \ No newline at end of file