removed entry can't win giveaway (#49)

This commit is contained in:
Jacob Nguyen
2024-12-14 15:42:33 -06:00
committed by GitHub
4 changed files with 47 additions and 28 deletions

View File

@@ -115,6 +115,8 @@ export default commandModule({
await ctx.reply({
embeds: [embed],
}).then(embedMessage => {
db.prepare(`INSERT INTO giveaway_message(message_id, host_id) VALUES (?, ?)`).run(embedMessage.id, ctx.userId)
embedMessage.react("🎉")
//checks if author reacted to itself
@@ -123,45 +125,38 @@ 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)
let intervalTime = endTime.getTime() - startTime.getTime()
setTimeout(() => {
const stmt = db.prepare(`SELECT * FROM entrees`).all()
const stmt = db.prepare(`SELECT * FROM entries WHERE message_id = ?`).all(embedMessage.id)
let winnerIndex = Math.floor(Math.random() * stmt.length)
if (stmt.length > 0 && stmt[winnerIndex].user_id !== ctx.userId) {
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) {
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
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')
embed.setFields(
{name: '\u200b', value: `Not enough eligible users`},
{name: '\u200b', value: `Hosted by: <@${ctx.userId}>`},
{name: '\u200b', value: `Ended: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()} (${endTimeStamp})`}
)
embedMessage.edit({embeds: [embed]})
embedMessage.edit({content: `Not enough eligible users`, embeds: []})
}
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 entrees`).run()
}
})
})

View File

@@ -6,10 +6,16 @@ export default discordEvent({
execute: async (reaction, potentialWinners) => {
const startTime = reaction.message.createdTimestamp
const stmt = db.prepare(`INSERT INTO entrees(timestamp, user_id) VALUES (?, ?)`)
if (potentialWinners.bot === false) {
stmt.run([startTime, potentialWinners.id])
}
const messages = db.prepare(`SELECT * FROM giveaway_message`).all()
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.interaction?.user.id) {
const checkUser = db.prepare(`SELECT COUNT(*) as count FROM entries WHERE message_id = ? AND user_id = ?`).get(message.message_id, potentialWinners.id);
if (checkUser.count === 0) {
const stmt = db.prepare(`INSERT INTO entries(message_id, timestamp, user_id) VALUES (?, ?, ?)`).run([message.message_id, startTime, potentialWinners.id])
}
}
})
}
})
})

View File

@@ -0,0 +1,17 @@
import { discordEvent } from "@sern/handler";
import { db } from "../utils/db.js"
export default discordEvent({
name: 'messageReactionRemove',
execute: async (reaction, deletedEntry) => {
const deletedId = deletedEntry.id
const message_id = db.prepare(`SELECT message_id FROM giveaway_message LIMIT 1`).get()
if (reaction.emoji.name === '🎉' && reaction.message.id === message_id.message_id) {
const stmt = db.prepare(`DELETE FROM entries WHERE user_id = ?`)
stmt.run(deletedId)
}
}
})

View File

@@ -3,4 +3,5 @@ export const db = new Database('giveaway.db');
db.pragma('journal_mode = WAL');
db.exec(`CREATE TABLE IF NOT EXISTS entrees(timestamp, user_id)`);
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)`)