mirror of
https://github.com/sern-handler/sern-community
synced 2026-06-06 01:16:57 +00:00
enter and leave giveaway should work with multiple giveaways (not end giveaway)
This commit is contained in:
@@ -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 = ?`)
|
||||
|
||||
68
src/commands/handlers/giveawayEnter.ts
Normal file
68
src/commands/handlers/giveawayEnter.ts
Normal file
@@ -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 = `<t:${Math.floor(endTime! / 1000)}:f>`
|
||||
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] });
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
61
src/commands/handlers/giveawayLeave.ts
Normal file
61
src/commands/handlers/giveawayLeave.ts
Normal file
@@ -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 = `<t:${Math.floor(endTime! / 1000)}:f>`
|
||||
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] });
|
||||
},
|
||||
});
|
||||
@@ -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)`)
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS giveaway_message(message_id, end_time, host_id)`)
|
||||
Reference in New Issue
Block a user