started to add fail buttons but new issues to resolve

This commit is contained in:
kingomes
2025-02-05 16:19:21 -06:00
parent ae22b79248
commit f022225f15
4 changed files with 111 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
import { commandModule, CommandType, scheduledTask } from "@sern/handler";
import { commandModule, CommandType } from "@sern/handler";
import { ownerOnly, publish } from "#plugins";
import { ApplicationCommandOptionType, EmbedBuilder } from "discord.js";
import { ApplicationCommandOptionType, ButtonBuilder, ActionRowBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import { db } from "../utils/db.js";
import { add, addDays, addHours, addMinutes, addSeconds } from "date-fns"
import { Timestamp } from "#utils";
@@ -56,49 +56,12 @@ export default commandModule({
timeUnit2: timeLeft2
})
// This if chain uses date-fns to correctly calculate the time allocated to the giveaway based on what the
// user types (seconds, minutes, etc.)
// if the time unit before the "and" is "seconds" or one of the other entries in the secondNames array, add the time entered
// to the startTime and save that in the endTime
if (secondNames.includes(timeUnit1!)) {
endTime = endTime === startTime ? addSeconds(startTime, timeLeft1) : addSeconds(endTime, timeLeft1)
}
// if the time unit after the "and" is "seconds" or one of the other entries in the secondNames array, add the time entered
// to the startTime and save that in the endTime
if (secondNames.includes(timeUnit2!)) {
endTime = endTime === startTime ? addSeconds(startTime, timeLeft2!) : addSeconds(endTime, timeLeft2!)
}
// if the time unit before the "and" is "minutes" or one of the other entries in the minuteNames array, add the time entered
// to the startTime and save that in the endTime
if (minuteNames.includes(timeUnit1!)) {
endTime = endTime === startTime ? addMinutes(startTime, timeLeft1) : addMinutes(endTime, timeLeft1)
}
// if the time unit after the "and" is "minutes" or one of the other entries in the minuteNames array, add the time entered
// to the startTime and save that in the endTime
if (minuteNames.includes(timeUnit2!)) {
endTime = endTime === startTime ? addMinutes(startTime, timeLeft2!) : addMinutes(endTime, timeLeft2!)
}
// if the time unit before the "and" is "hours" or one of the other entries in the hourNames array, add the time entered
// to the startTime and save that in the endTime
if (hourNames.includes(timeUnit1!)) {
endTime = endTime === startTime ? addHours(startTime, timeLeft1) : addHours(endTime, timeLeft1)
}
// if the time unit after the "and" is "hours" or one of the other entries in the hourNames array, add the time entered
// to the startTime and save that in the endTime
if (hourNames.includes(timeUnit2!)) {
endTime = endTime === startTime ? addHours(startTime, timeLeft2!) : addHours(endTime, timeLeft2!)
}
// if the time unit before the "and" is "days" or one of the other entries in the dayNames array, add the time entered
// to the startTime and save that in the endTime
if (dayNames.includes(timeUnit1!)) {
endTime = endTime === startTime ? addDays(startTime, timeLeft1) : addDays(endTime, timeLeft1)
}
// if the time unit after the "and" is "days" or one of the other entries in the dayNames array, add the time entered
// to the startTime and save that in the endTime
if (dayNames.includes(timeUnit2!)) {
endTime = endTime === startTime ? addDays(startTime, timeLeft2!) : addDays(endTime, timeLeft2!)
}
endTime = add(startTime, {
seconds: secondNames.includes(timeUnit1!) ? timeLeft1 : secondNames.includes(timeUnit2!) ? timeLeft2 : 0,
minutes: minuteNames.includes(timeUnit1!) ? timeLeft1 : minuteNames.includes(timeUnit2!) ? timeLeft2 : 0,
hours: hourNames.includes(timeUnit1!) ? timeLeft1 : hourNames.includes(timeUnit2!) ? timeLeft2 : 0,
days: dayNames.includes(timeUnit1!) ? timeLeft1 : dayNames.includes(timeUnit2!) ? timeLeft2 : 0
})
const endTimeStamp: string = `<t:${Math.floor(endTime!.getTime() / 1000)}:f>`
const endTimeStamp2 = new Timestamp(endTime.getTime()).timestamp
@@ -115,6 +78,8 @@ export default commandModule({
await ctx.reply({
embeds: [embed],
}).then(embedMessage => {
let giveawayEnded = false
db.prepare(`INSERT INTO giveaway_message(message_id, host_id) VALUES (?, ?)`).run(embedMessage.id, ctx.userId)
embedMessage.react("🎉")
@@ -139,7 +104,8 @@ export default commandModule({
if (stmt.length > 0 && stmt[winnerIndex].user_id !== ctx.userId) {
const winnerId = stmt[winnerIndex].user_id
embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway!`, embeds: []})
embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway! ${stmt.length} users entered`, embeds: []})
giveawayEnded = true
}
else if (stmt.length > 1 && stmt[winnerIndex].user_id === ctx.userId) {
while (stmt[winnerIndex].user_id === ctx.userId) {
@@ -147,16 +113,45 @@ export default commandModule({
}
const winnerId = stmt[winnerIndex].user_id
embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway!`, embeds: []})
embedMessage.edit({content: `Congratulations <@${winnerId}> on winning the ${item} giveaway! ${stmt.length} users entered`, embeds: []})
giveawayEnded = true
}
else if ((stmt.length === 1 && stmt[winnerIndex].user_id === ctx.userId) || stmt.length === 0) {
embedMessage.edit({content: `Not enough eligible users`, embeds: []})
embedMessage.edit({content: `Couldn't determine a winner: Not enough eligible users. ${stmt.length} users entered`, embeds: [], components: [retryRows()]})
giveawayEnded = true
}
db.prepare(`DELETE FROM giveaway_message WHERE message_id = ?`).run(embedMessage.id)
db.prepare(`DELETE FROM entries WHERE message_id = ?`).run(embedMessage.id)
if (giveawayEnded) {
embedMessage.reactions.removeAll()
}
clearInterval(selfReactionInterval)
}, intervalTime)
})
}
})
})
function retryRows() {
const attemptReroll = new ButtonBuilder({
customId: 'reroll',
label: 'Attempt Reroll',
style: ButtonStyle.Primary
})
const resendGiveaway = new ButtonBuilder({
customId: 'resend',
label: 'Resend Giveaway',
style: ButtonStyle.Primary
})
const discardGiveaway = new ButtonBuilder({
customId: 'discard',
label: 'Discard Giveaway',
style: ButtonStyle.Primary
})
return new ActionRowBuilder<ButtonBuilder>()
.addComponents(attemptReroll, resendGiveaway, discardGiveaway);
}

View File

@@ -0,0 +1,20 @@
import { ownerIDs } from "#constants";
import { commandModule, CommandType } from "@sern/handler";
import { db } from "../../utils/db.js";
export default commandModule({
type: CommandType.Button,
name: "discard",
async execute(ctx) {
// if (!ownerIDs.includes(ctx.user.id))
// return ctx.reply({
// ephemeral: true,
// content: `You cannot discard the giveaway because you are not one of the owners`,
// }
db.prepare(`DELETE FROM giveaway_message WHERE message_id = ?`).run(ctx.message.id)
db.prepare(`DELETE FROM entries WHERE message_id = ?`).run(ctx.message.id)
ctx.message.reactions.removeAll()
},
});

View File

@@ -0,0 +1,28 @@
import { ownerIDs } from "#constants";
import { commandModule, CommandType } from "@sern/handler";
import { db } from "../../utils/db.js";
export default commandModule({
type: CommandType.Button,
name: "reroll",
async execute(ctx) {
// if (!ownerIDs.includes(ctx.user.id))
// return ctx.reply({
// ephemeral: true,
// content: `You cannot reroll the giveaway because you are not one of the owners`,
// }
console.log(ctx.message.id)
const stmt = db.prepare(`SELECT * FROM entries WHERE message_id = ?`).all(ctx.message.id)
if (stmt.length > 0) {
let winnerIndex = Math.floor(Math.random() * stmt.length)
const winnerId = stmt[winnerIndex].user_id
await ctx.reply({content: `Congratulations <@${winnerId}> on winning the giveaway! ${stmt.length} users entered`})
}
else {
await ctx.reply({content: `You cannot reroll because no one entered the giveaway!`})
}
},
});

View File

@@ -0,0 +1,19 @@
import { ownerIDs } from "#constants";
import { commandModule, CommandType } from "@sern/handler";
import { db } from "../../utils/db.js";
export default commandModule({
type: CommandType.Button,
name: "resend",
async execute(ctx) {
// if (!ownerIDs.includes(ctx.user.id))
// return ctx.reply({
// ephemeral: true,
// content: `You cannot resend the giveaway because you are not one of the owners`,
// }
console.log(ctx.message.interaction?.commandName)
let test = await ctx.guild?.commands.fetch(ctx.message.interaction?.id)!.then(res => res.toJSON())
console.log(test)
},
});