diff --git a/src/commands/giveaway.ts b/src/commands/giveaway.ts index 208af13..dba1f24 100644 --- a/src/commands/giveaway.ts +++ b/src/commands/giveaway.ts @@ -115,8 +115,7 @@ export default commandModule({ await ctx.reply({ embeds: [embed], }).then(embedMessage => { - const stmt = db.prepare(`INSERT INTO giveaway_message(message_id) VALUES (?)`) - stmt.run(embedMessage.id) + db.prepare(`INSERT INTO giveaway_message(message_id, host_id) VALUES (?, ?)`).run(embedMessage.id, ctx.userId) embedMessage.react("🎉") @@ -126,6 +125,7 @@ export default commandModule({ for (const reaction of userReactions.values()) { reaction.users.remove(ctx.userId); + ctx.interaction.followUp({content: "As the host of the giveaway, you cannot enter it.", ephemeral: true}) } }, 1000) @@ -148,10 +148,25 @@ export default commandModule({ embedMessage.edit({embeds: [embed]}) - embedMessage.reply(`Congratulations <@${winnerId}> on winning the ${item} giveaway!`) + embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway!`, embeds: []}) } else if (stmt.length > 1 && stmt[winnerIndex].user_id === ctx.userId) { - winnerIndex = Math.floor(Math.random() * stmt.length) + while (stmt[winnerIndex].user_id === ctx.userId) { + winnerIndex = Math.floor(Math.random() * stmt.length) + } + const winnerId = stmt[winnerIndex].user_id + + embed.setDescription('\u200b') + embed.setFields( + {name: '\u200b', value: `Winner: <@${winnerId}>`}, + {name: '\u200b', value: `Hosted by: <@${ctx.userId}>`}, + {name: '\u200b', value: `Ended: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()} (${endTimeStamp})`} + ) + + embedMessage.edit({embeds: [embed]}) + + embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway!`, embeds: []}) + } else if ((stmt.length === 1 && stmt[winnerIndex].user_id === ctx.userId) || stmt.length === 0) { embed.setDescription('\u200b') @@ -163,11 +178,10 @@ export default commandModule({ embedMessage.edit({embeds: [embed]}) } - db.prepare(`DELETE FROM giveaway_message`).run() + db.prepare(`DELETE FROM giveaway_message WHERE message_id = ?`).run(embedMessage.id) + db.prepare(`DELETE FROM entries WHERE message_id = ?`).run(embedMessage.id) clearInterval(selfReactionInterval) }, intervalTime) }) - - db.prepare(`DELETE FROM entries`).run() } }) \ No newline at end of file diff --git a/src/events/embedReact.ts b/src/events/embedReact.ts index 9b2b14c..24414fa 100644 --- a/src/events/embedReact.ts +++ b/src/events/embedReact.ts @@ -6,14 +6,14 @@ export default discordEvent({ execute: async (reaction, potentialWinners) => { const startTime = reaction.message.createdTimestamp - const message_id = db.prepare(`SELECT message_id FROM giveaway_message LIMIT 1`).get() + const messages = db.prepare(`SELECT * FROM giveaway_message`).all() - if (reaction.emoji.name === '🎉' && reaction.message.id === message_id.message_id) { - const stmt = db.prepare(`INSERT INTO entries(timestamp, user_id) VALUES (?, ?)`) - - if (potentialWinners.bot === false) { - stmt.run([startTime, potentialWinners.id]) + messages.map((message: { message_id: string, host_id: string }) => { + if (reaction.emoji.name === '🎉' && reaction.message.id === message.message_id && !potentialWinners.bot && message.host_id !== reaction.message.author?.id) { + const stmt = db.prepare(`INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)`) + + stmt.run([message.message_id, startTime, potentialWinners.id]) } - } + }) } }) \ No newline at end of file diff --git a/src/utils/db.ts b/src/utils/db.ts index 269b396..dd2cc9f 100644 --- a/src/utils/db.ts +++ b/src/utils/db.ts @@ -3,5 +3,5 @@ export const db = new Database('giveaway.db'); db.pragma('journal_mode = WAL'); -db.exec(`CREATE TABLE IF NOT EXISTS entries(timestamp, user_id)`); -db.exec(`CREATE TABLE IF NOT EXISTS giveaway_message(message_id)`) \ No newline at end of file +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