Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript"]
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/enmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
93 changes: 93 additions & 0 deletions examples/custom-databases/prisma.ts
Original file line number Diff line number Diff line change
@@ -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<any>): Promise<boolean> {
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<any>): Promise<boolean> {
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<boolean> {
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);

3 changes: 2 additions & 1 deletion examples/custom-databases/quick.db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/quick.replit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/quickmongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/custom-databases/replit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
7 changes: 7 additions & 0 deletions src/Giveaway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface PauseOptions {
export interface GiveawaysManagerOptions<ExtraData> {
storage?: string;
forceUpdateEvery?: number;
ignoreMissingIntents?: boolean;
endedGiveawaysLifetime?: number;
default?: {
botsCanWin?: boolean;
Expand Down