mirror of
https://github.com/sern-handler/sern-community
synced 2026-06-06 01:16:57 +00:00
minor fixes + more exclude host
This commit is contained in:
@@ -25,22 +25,19 @@ export default commandModule({
|
||||
],
|
||||
execute: async (ctx, { deps }) => {
|
||||
const item = ctx.options.getString("item")
|
||||
let timeLeftString = ctx.options.getString("time")
|
||||
const timeLeftString = ctx.options.getString("time")
|
||||
|
||||
let timeUnit1
|
||||
let timeLeft1
|
||||
let timeUnit2
|
||||
let timeLeft2
|
||||
if (!timeLeftString?.includes("and")) {
|
||||
timeUnit1 = timeLeftString?.split(" ")[1]
|
||||
timeLeft1 = Number(timeLeftString?.split(" ")[0])
|
||||
}
|
||||
else {
|
||||
let timeLeftStringPart1 = timeLeftString.split("and")[0]
|
||||
timeUnit1 = timeLeftStringPart1?.split(" ")[1]
|
||||
timeLeft1 = Number(timeLeftStringPart1?.split(" ")[0])
|
||||
|
||||
let timeLeftStringPart2 = timeLeftString.split("and")[1].replace(timeLeftString.split("and")[1].substring(0, 1), "")
|
||||
const [part1, part2] = timeLeftString?.split("and")
|
||||
timeUnit1 = part1?.split(" ")[1]
|
||||
timeLeft1 = Number(part1?.split(" ")[0])
|
||||
|
||||
if (part2) {
|
||||
const timeLeftStringPart2 = part2.replace(part2.substring(0, 1), "")
|
||||
timeUnit2 = timeLeftStringPart2?.split(" ")[1]
|
||||
timeLeft2 = Number(timeLeftStringPart2?.split(" ")[0])
|
||||
}
|
||||
@@ -49,37 +46,56 @@ export default commandModule({
|
||||
|
||||
let endTime: Date
|
||||
|
||||
let secondNames = ['seconds', 'second', 'sec', 'secs']
|
||||
let minuteNames = ['minutes', 'minute', 'min', 'mins']
|
||||
let hourNames = ['hours', 'hour', 'hr', 'hrs']
|
||||
let dayNames = ['days', 'day']
|
||||
const secondNames = ['seconds', 'second', 'sec', 'secs']
|
||||
const minuteNames = ['minutes', 'minute', 'min', 'mins']
|
||||
const hourNames = ['hours', 'hour', 'hr', 'hrs']
|
||||
const dayNames = ['days', 'day']
|
||||
|
||||
endTime = add(startTime, {
|
||||
timeUnit1: timeLeft1,
|
||||
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!)
|
||||
}
|
||||
@@ -92,8 +108,7 @@ export default commandModule({
|
||||
.setDescription('React to enter the giveaway!')
|
||||
.addFields(
|
||||
{name: '\u200b', value: `Hosted by: <@${ctx.userId}>`},
|
||||
{name: '\u200b', value: `Time Left: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()}`},
|
||||
{name: '\u200b', value: `Ends at: ${endTimeStamp}`}
|
||||
{name: '\u200b', value: `Ends: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()} (${endTimeStamp})`}
|
||||
)
|
||||
|
||||
|
||||
@@ -102,9 +117,17 @@ export default commandModule({
|
||||
}).then(embedMessage => {
|
||||
embedMessage.react("🎉")
|
||||
|
||||
setInterval(() => {
|
||||
const userReactions = embedMessage.reactions.cache.filter(reaction => reaction.users.cache.has(ctx.userId));
|
||||
|
||||
for (const reaction of userReactions.values()) {
|
||||
reaction.users.remove(ctx.userId);
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
let intervalTime = endTime.getTime() - startTime.getTime()
|
||||
|
||||
let interval = setInterval(() => {
|
||||
setTimeout(() => {
|
||||
const stmt = db.prepare(`SELECT * FROM entrees`).all()
|
||||
|
||||
let winnerIndex = Math.floor(Math.random() * stmt.length)
|
||||
@@ -116,12 +139,10 @@ export default commandModule({
|
||||
embed.setFields(
|
||||
{name: '\u200b', value: `Winner: <@${winnerId}>`},
|
||||
{name: '\u200b', value: `Hosted by: <@${ctx.userId}>`},
|
||||
{name: '\u200b', value: `Ended at: ${endTimeStamp}`}
|
||||
{name: '\u200b', value: `Ended: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()} (${endTimeStamp})`}
|
||||
)
|
||||
|
||||
embedMessage.edit({embeds: [embed]})
|
||||
|
||||
clearInterval(interval)
|
||||
}
|
||||
else if (stmt.length > 1 && stmt[winnerIndex].user_id === ctx.userId) {
|
||||
winnerIndex = Math.floor(Math.random() * stmt.length)
|
||||
@@ -131,12 +152,10 @@ export default commandModule({
|
||||
embed.setFields(
|
||||
{name: '\u200b', value: `Not enough eligible users`},
|
||||
{name: '\u200b', value: `Hosted by: <@${ctx.userId}>`},
|
||||
{name: '\u200b', value: `Ended at: ${endTimeStamp}`}
|
||||
{name: '\u200b', value: `Ended: ${new Timestamp(Number(endTimeStamp2)).getRelativeTime()} (${endTimeStamp})`}
|
||||
)
|
||||
|
||||
embedMessage.edit({embeds: [embed]})
|
||||
|
||||
clearInterval(interval)
|
||||
}
|
||||
}, intervalTime)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user