diff --git a/.vscode/settings.json b/.vscode/settings.json index 109b9876..858730b5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "eslint.validate": ["javascript"] } diff --git a/README.md b/README.md index 16485432..34dd05fd 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,8 @@ const manager = new GiveawaysManager(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/enmap.js b/examples/custom-databases/enmap.js index a7635004..80710740 100644 --- a/examples/custom-databases/enmap.js +++ b/examples/custom-databases/enmap.js @@ -54,7 +54,8 @@ const manager = new GiveawayManagerWithOwnDatabase(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/mongoose.js b/examples/custom-databases/mongoose.js index 7e974580..eb2041ed 100644 --- a/examples/custom-databases/mongoose.js +++ b/examples/custom-databases/mongoose.js @@ -123,7 +123,8 @@ const manager = new GiveawayManagerWithOwnDatabase(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/mysql.js b/examples/custom-databases/mysql.js index 6aff55e6..1f9f9404 100644 --- a/examples/custom-databases/mysql.js +++ b/examples/custom-databases/mysql.js @@ -118,7 +118,8 @@ const manager = new GiveawayManagerWithOwnDatabase(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/nano.js b/examples/custom-databases/nano.js index 9767e1a2..dfbbd1e9 100644 --- a/examples/custom-databases/nano.js +++ b/examples/custom-databases/nano.js @@ -68,7 +68,8 @@ const manager = new GiveawayManagerWithOwnDatabase( // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/prisma.ts b/examples/custom-databases/prisma.ts new file mode 100644 index 00000000..2ef50b98 --- /dev/null +++ b/examples/custom-databases/prisma.ts @@ -0,0 +1,93 @@ +// Asuming you have a typescript based setup with prisma.io setup + +// Sample for your schema.prisma: +/* + model Giveaways { + message_id String @id + data Json + } +*/ + +import Discord from 'discord.js'; +import { GiveawaysManager } from 'discord-giveaways'; +import { PrismaClient } from 'prisma' + +const client = new Discord.Client({ + intents: [Discord.IntentsBitField.Flags.Guilds, Discord.IntentsBitField.Flags.GuildMessageReactions] +}); +const prisma = new PrismaClient(); + +export class GiveawayManagerWithOwnDatabase extends GiveawaysManager { + async registerEvents() { + // get all giveaways on init + this.getAllGiveaways(); + } + + async getAllGiveaways() { + const rawGiveawaysDB = await prisma.giveaways.findMany({ + where: { message_id: {} }, + select: { data: true, message_id: true }, + }) || []; + const rawGiveaways = rawGiveawaysDB + .filter(v => v?.data && v?.message_id) + .map(row => + typeof row.data === "string" ? JSON.parse(row.data as string, parseCallback) : row.data + ) as Giveaway[]; + return rawGiveaways; + // IF YOU USE DEDICATED SHARDING YOU NEED TO FILTER THE OUTPUT BASED ON YOUR SHARDS! + // const shardIds = getInfo().info.SHARD_LIST || [0]; // getInfo() is a part of discord-cross-hosting + // const totalShardsCount = getInfo().info.TOTAL_SHARDS; // getInfo() is a part of discord-cross-hosting + // return rawGiveaways.filter(g => + // shardIds.includes(discordjs.ShardClientUtil.shardIdForGuildId(g.guildId, )) + // ); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId: string, giveawayData: GiveawayData): Promise { + await prisma.giveaways.create({ + data: { + message_id: messageId, + data: JSON.stringify(giveawayData, stringifyCallback), + }, + }); + return true; + } + + // This function is called when a giveaway needs to be edited in the database. + async editGiveaway(messageId: string, giveawayData: GiveawayData): Promise { + await prisma.giveaways.update({ + where: { message_id: messageId }, + data: { data: JSON.stringify(giveawayData, stringifyCallback), + }, + }); + return true; + } + + // This function is called when a giveaway needs to be deleted from the database. + async deleteGiveaway(messageId: string): Promise { + await prisma.giveaways.delete({ + where: { message_id: messageId }, + }); + return true; + } +} + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: '#FF0000', + embedColorEnd: '#000000', + reaction: '🎉' + } +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { + console.log('Bot is ready!'); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); + diff --git a/examples/custom-databases/quick.db.js b/examples/custom-databases/quick.db.js index aa3c4653..d1ec1ed4 100644 --- a/examples/custom-databases/quick.db.js +++ b/examples/custom-databases/quick.db.js @@ -62,7 +62,8 @@ const manager = new GiveawayManagerWithOwnDatabase(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/quick.replit.js b/examples/custom-databases/quick.replit.js index cae48d42..e26650d1 100644 --- a/examples/custom-databases/quick.replit.js +++ b/examples/custom-databases/quick.replit.js @@ -73,7 +73,8 @@ const manager = new GiveawayManagerWithOwnDatabase( // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/quickmongo.js b/examples/custom-databases/quickmongo.js index 808f31e1..9526e9a0 100644 --- a/examples/custom-databases/quickmongo.js +++ b/examples/custom-databases/quickmongo.js @@ -59,7 +59,8 @@ const manager = new GiveawayManagerWithOwnDatabase( // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/examples/custom-databases/replit.js b/examples/custom-databases/replit.js index de80d2dc..a3a06863 100644 --- a/examples/custom-databases/replit.js +++ b/examples/custom-databases/replit.js @@ -69,7 +69,8 @@ const manager = new GiveawayManagerWithOwnDatabase(client, { // We now have a giveawaysManager property to access the manager everywhere! client.giveawaysManager = manager; -client.on('ready', () => { +// djs changed on.("ready" to "clientReady" +client.on('clientReady', () => { console.log('Bot is ready!'); }); diff --git a/package.json b/package.json index f996e20b..93ade969 100644 --- a/package.json +++ b/package.json @@ -43,16 +43,13 @@ }, "devDependencies": { "@types/node": "^18.0.3", - "discord.js": "^14.0.3", + "discord.js": "^14.22.1", "eslint": "^8.20.0", "eslint-plugin-node": "^11.1.0", "jsdoc": "^3.6.11", "jsdoc-skyceil": "Androz2091/jsdoc-skyceil", "typescript": "^4.7.4" }, - "peerDependencies": { - "discord.js": ">=14.0.0" - }, "engines": { "node": ">=16.9.0" } diff --git a/src/Giveaway.js b/src/Giveaway.js index c5aa12bd..dbb7a273 100644 --- a/src/Giveaway.js +++ b/src/Giveaway.js @@ -132,6 +132,11 @@ class Giveaway extends EventEmitter { * @type {?Discord.Message} */ this.message = null; + /** + * The Entries amount of how many entered the Giveaway (Amount will be valid, once rolled (ended)!) + * @type {number} + */ + this.finalEntrantAmount = null; } /** @@ -550,6 +555,8 @@ class Giveaway extends EventEmitter { const users = await this.fetchAllEntrants().catch(() => {}); if (!users?.size) return []; + this.finalEntrantAmount = users.size; + // Bonus Entries let userArray; if (!this.isDrop && this.bonusEntries.length) { diff --git a/src/Manager.js b/src/Manager.js index 046dfe21..5930fb4f 100644 --- a/src/Manager.js +++ b/src/Manager.js @@ -33,7 +33,7 @@ class GiveawaysManager extends EventEmitter { constructor(client, options, init = true) { super(); if (!client?.options) throw new Error(`Client is a required option. (val=${client})`); - if ( + if (!options?.ignoreMissingIntents && !new Discord.IntentsBitField(client.options.intents).has( Discord.IntentsBitField.Flags.GuildMessageReactions ) @@ -669,7 +669,8 @@ class GiveawaysManager extends EventEmitter { async _init() { let rawGiveaways = await this.getAllGiveaways(); - await (this.client.readyAt ? Promise.resolve() : new Promise((resolve) => this.client.once('ready', resolve))); + // djs changed on.("ready" to "clientReady" + await (this.client.readyAt ? Promise.resolve() : new Promise((resolve) => this.client.once('clientReady', resolve))); // Filter giveaways for each shard if (this.client.shard && this.client.guilds.cache.size) { diff --git a/typings/index.d.ts b/typings/index.d.ts index 8df936fa..f8dd3b4c 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -89,6 +89,7 @@ export interface PauseOptions { export interface GiveawaysManagerOptions { storage?: string; forceUpdateEvery?: number; + ignoreMissingIntents?: boolean; endedGiveawaysLifetime?: number; default?: { botsCanWin?: boolean;