djs-builder 0.6.34 → 0.6.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1063 -4
  2. package/function/giveaway.js +157 -158
  3. package/package.json +2 -2
@@ -1,13 +1,12 @@
1
1
  const { Schema, model } = require("mongoose");
2
- const { EmbedBuilder, embedLength } = require("discord.js");
2
+ const { EmbedBuilder } = require("discord.js");
3
3
  const { CreateRow } = require("../function/function");
4
- const crypto = require("crypto");
5
4
  const giveawaySchema = new Schema({
6
5
  ended: { type: Boolean, default: false },
7
6
  guildId: { type: String, required: true },
8
7
  channelId: { type: String, required: true },
9
8
  messageId: { type: String, required: true },
10
- hoster: { type: String, required: true },
9
+ hoster: { type: String, required: true },
11
10
  endEmbed: { type: Object, required: true },
12
11
  winwesNumber: { type: Number, default: 1 },
13
12
  winers: Array,
@@ -34,46 +33,53 @@ async function Gstart({
34
33
  winers: winers,
35
34
  channelId: channelId,
36
35
  embed: {
37
- custom: Scustom,
38
- title: Stitle,
39
- description: Sdescription,
40
- color: Scolor,
41
- image: Simage,
42
- theumbnail: Sthumbnail,
43
- },
36
+ custom: Scustom = false,
37
+ title: Stitle = false,
38
+ description: Sdescription = false,
39
+ color: Scolor = false,
40
+ image: Simage = false,
41
+ theumbnail: Sthumbnail = false,
42
+ } = {},
44
43
  endEmbed: {
45
- custom: Ecustom,
46
- title: Etitle,
47
- description: Edescription,
48
- color: Ecolor,
49
- image: Eimage,
50
- theumbnail: Ethumbnail,
44
+ custom: Ecustom = false,
45
+ title: Etitle = false,
46
+ description: Edescription = false,
47
+ color: Ecolor = false,
48
+ image: Eimage = false,
49
+ theumbnail: Ethumbnail = false,
50
+ } = {},
51
+ reaction: {
52
+ type: type = false,
53
+ emoji: emoji = false,
54
+ label: label = false,
55
+ style: style = false,
51
56
  },
52
- reaction: { type: type, emoji: emoji, label: label, style: style },
53
57
  }) {
54
58
  if (!winers) winers = 1;
55
- if (!endTime) return console.log("❌ No End Time provided");
56
- if (!channelId) return console.log("❌ No Channel provided");
57
- if (!type) return console.log(" No Type provided");
58
- if (!emoji && !label) return console.log("❌ No Emoji or Label provided");
59
- if (!emoji && type === "reaction") return console.log("❌ No Emoji provided");
59
+ if (!endTime) return { error: "End time is required." };
60
+ if (!channelId) return { error: "Channel ID is required." };
61
+ if (!context) return { error: "Context is required." };
62
+
63
+ if (type === "reaction" && !emoji) return { error: "Emoji is not required." };
60
64
 
61
65
  let row = [];
62
66
  if (type === "button") {
63
67
  row = CreateRow([
64
- {
65
- id: "djs-builder-giveaway",
66
- style: style || 1,
67
- label: label,
68
- emoji: emoji,
69
- },
68
+ [
69
+ {
70
+ id: "djs-builder-giveaway",
71
+ style: style || 1,
72
+ label: label,
73
+ emoji: emoji,
74
+ },
75
+ ],
70
76
  ]);
71
77
  }
72
78
 
73
79
  let embed;
74
80
 
75
81
  if (Scustom) {
76
- embed = new EmbedBuilder(Scustom);
82
+ embed = Scustom;
77
83
  } else {
78
84
  embed = new EmbedBuilder()
79
85
  .setTitle(Stitle || "🎉 Giveaway")
@@ -93,7 +99,7 @@ async function Gstart({
93
99
  });
94
100
 
95
101
  const channel = await context.guild.channels.cache.get(channelId);
96
- if (!channel) return console.log("❌ No Channel found");
102
+ if (!channel) return { error: "❌ No Channel found" };
97
103
 
98
104
  try {
99
105
  const message = await channel.send({
@@ -115,32 +121,30 @@ async function Gstart({
115
121
  if (Ethumbnail) end_embed.setThumbnail(Ethumbnail);
116
122
  }
117
123
 
118
- end_embed.addFields({
119
- name: "🎉 Giveaway",
120
- value: `- Winer(s): ${winers}\n- Time : <t:${Math.floor(
121
- endTime / 1000
122
- )}:R>\n- Hosted By : ${context.user ? context.user : context.author}`,
123
- inline: true,
124
- });
125
-
126
- console.log(context.author.id)
127
- await giveaway.create({
128
- guildId: context.guild.id,
129
- channelId: channelId,
130
- messageId: message.id,
131
- hoster: context.user ? context.user.id : context.author.id,
132
- endTime: endTime,
133
- endType: false,
134
- paused: false,
135
- pausedTime: [],
136
- winers: [],
137
- winwesNumber: winers,
138
- ended: false,
139
- reaction: type,
140
- endEmbed: end_embed.toJSON(),
141
- users: [],
142
- });
124
+ end_embed.addFields({
125
+ name: "🎉 Giveaway",
126
+ value: `- Winer(s): ${winers}\n- Time : <t:${Math.floor(
127
+ endTime / 1000
128
+ )}:R>\n- Hosted By : ${context.user ? context.user : context.author}`,
129
+ inline: true,
130
+ });
143
131
 
132
+ await giveaway.create({
133
+ guildId: context.guild.id,
134
+ channelId: channelId,
135
+ messageId: message.id,
136
+ hoster: context.user ? context.user.id : context.author.id,
137
+ endTime: endTime,
138
+ endType: false,
139
+ paused: false,
140
+ pausedTime: [],
141
+ winers: [],
142
+ winwesNumber: winers,
143
+ ended: false,
144
+ reaction: type,
145
+ endEmbed: end_embed.toJSON(),
146
+ users: [],
147
+ });
144
148
 
145
149
  if (type === "reaction") message.react(emoji);
146
150
  } catch (e) {
@@ -160,44 +164,37 @@ async function Gcheck(client) {
160
164
  }
161
165
  };
162
166
 
163
- check();
167
+ check();
164
168
  setInterval(check, 10 * 1000);
165
169
  }
166
170
 
167
171
  //////////////////////////////////* Giveaway Pick Winners 🎛️
168
172
 
169
-
170
173
  function pickWinners(arr, count) {
171
174
  const shuffled = [...arr].sort(() => Math.random() - 0.5);
172
175
  return shuffled.slice(0, count);
173
176
  }
174
177
 
175
-
176
-
177
178
  //////////////////////////////////! Giveaway End 📆
178
179
 
179
-
180
180
  async function giveaway_end(client, g, type) {
181
-
182
-
183
181
  const guild = await client.guilds.cache.get(g.guildId);
184
182
  const channel = await guild.channels.cache.get(g.channelId);
185
- if (!channel) return console.log("❌ No Channel found");
183
+ if (!channel) return { error: "❌ No Channel found" };
186
184
  const message = await channel.messages.fetch(g.messageId);
187
- if (!message) return console.log("❌ No Message found");
185
+ if (!message) return { error: "❌ No Message found" };
188
186
 
189
187
  try {
190
-
191
188
  let users_id = [];
192
189
  if (g.reaction === "reaction") {
193
190
  const reaction = message.reactions.cache.first();
194
191
  const users = await reaction.users.fetch();
195
192
  users_id = users.filter((u) => !u.bot).map((u) => u.id);
193
+ } else {
194
+ users_id = g.users;
196
195
  }
197
196
 
198
-
199
-
200
- if (!users_id.length) {
197
+ if (!users_id.length) {
201
198
  await message.reply("❌ No Users found for this giveaway.");
202
199
  await giveaway.findOneAndUpdate(
203
200
  { messageId: message.id },
@@ -206,12 +203,11 @@ async function giveaway_end(client, g, type) {
206
203
  return;
207
204
  }
208
205
 
209
-
210
206
  const winners = pickWinners(users_id, g.winersNumber);
211
207
 
212
-
213
208
  await message.edit({
214
209
  embeds: [g.endEmbed],
210
+ components: [],
215
211
  });
216
212
  await message.reply({
217
213
  content: `🎉 **Giveaway Ended!**\n> Winner(s):\n- <@${winners
@@ -232,35 +228,32 @@ async function giveaway_end(client, g, type) {
232
228
 
233
229
  async function Greroll(client, messageId) {
234
230
  const g = await giveaway.findOne({ messageId });
235
- if (!g) return null;
231
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
236
232
 
237
233
  const guild = await client.guilds.cache.get(g.guildId);
238
234
  const channel = await guild.channels.cache.get(g.channelId);
239
- if (!channel) return console.log("❌ No Channel found");
235
+ if (!channel) return { error: "❌ No Channel found" };
240
236
  const message = await channel.messages.fetch(g.messageId);
241
- if (!message) return console.log("❌ No Message found");
237
+ if (!message) return { error: "❌ No Message found" };
242
238
 
243
239
  try {
244
-
245
240
  let users_id = [];
246
241
  if (g.reaction === "reaction") {
247
242
  const reaction = message.reactions.cache.first();
248
243
  const users = await reaction.users.fetch();
249
244
  users_id = users.filter((u) => !u.bot).map((u) => u.id);
245
+ } else {
246
+ users_id = g.users;
250
247
  }
251
248
 
252
-
253
-
254
- if (!users_id.length) {
249
+ if (!users_id.length) {
255
250
  await message.reply("❌ No Users found for this giveaway.");
256
- await giveaway.findOneAndUpdate(
251
+ return giveaway.findOneAndUpdate(
257
252
  { messageId: message.id },
258
253
  { ended: true }
259
254
  );
260
- return;
261
255
  }
262
256
 
263
-
264
257
  const winners = pickWinners(users_id, g.winersNumber);
265
258
 
266
259
  await message.reply({
@@ -282,8 +275,6 @@ async function Greroll(client, messageId) {
282
275
  { messageId: messageId },
283
276
  { winers: winners, endType: new_endType }
284
277
  );
285
-
286
-
287
278
  } catch (e) {
288
279
  console.log(e);
289
280
  }
@@ -304,14 +295,14 @@ async function Glist(type) {
304
295
  messageId: g.messageId,
305
296
  guildId: g.guildId,
306
297
  channelId: g.channelId,
307
- hoster: g.hoster,
298
+ hoster: g.hoster,
308
299
  ended: g.ended,
309
300
  endTime: g.endTime,
310
- paused: g.paused,
311
- pausedTime: g.pausedTime,
312
- endType: g.endType,
313
- winers: g.winers,
314
- winwesNumber: g.winwesNumber,
301
+ paused: g.paused,
302
+ pausedTime: g.pausedTime,
303
+ endType: g.endType,
304
+ winers: g.winers,
305
+ winwesNumber: g.winwesNumber,
315
306
  endEmbed: g.endEmbed,
316
307
  };
317
308
  });
@@ -321,22 +312,22 @@ async function Glist(type) {
321
312
 
322
313
  //////////////////////////////////* Giveaway Pause ⏸️
323
314
 
324
- async function Gpause(client ,messageId) {
315
+ async function Gpause(client, messageId) {
325
316
  const g = await giveaway.findOne({ messageId });
326
- if (!g) return null;
317
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
327
318
 
328
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
329
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
319
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
320
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
330
321
 
331
- const guild = await client.guilds.cache.get(g.guildId);
322
+ const guild = await client.guilds.cache.get(g.guildId);
332
323
  const channel = await guild.channels.cache.get(g.channelId);
333
- if (!channel) return console.log("❌ No Channel found");
324
+ if (!channel) return { error: "❌ No Channel found" };
334
325
  const message = await channel.messages.fetch(g.messageId);
335
- if (!message) return console.log("❌ No Message found");
326
+ if (!message) return { error: "❌ No Message found" };
336
327
 
337
-
338
- console.log(g)
339
- message.embeds[0].data.fields.find((f) => f.name === "🎉 Giveaway").value = `- Winer(s): ${g.winwesNumber}\n- Time : Pause ⏸️\n- Hosted By : <@${g.hoster}>`;
328
+ message.embeds[0].data.fields.find(
329
+ (f) => f.name === "🎉 Giveaway"
330
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : Pause ⏸️\n- Hosted By : <@${g.hoster}>`;
340
331
 
341
332
  await message.edit({ embeds: [message.embeds[0]] });
342
333
  await giveaway.findOneAndUpdate(
@@ -347,25 +338,26 @@ async function Gpause(client ,messageId) {
347
338
 
348
339
  //////////////////////////////////* Giveaway Resume ▶️
349
340
 
350
- async function Gresume(client,messageId) {
341
+ async function Gresume(client, messageId) {
351
342
  const g = await giveaway.findOne({ messageId });
352
- if (!g) return null;
343
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
344
+
345
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
346
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
353
347
 
354
- if(g.paused === false) return console.log("❌ Giveaway Already Active");
355
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
356
-
357
- const guild = await client.guilds.cache.get(g.guildId);
348
+ const guild = await client.guilds.cache.get(g.guildId);
358
349
  const channel = await guild.channels.cache.get(g.channelId);
359
- if (!channel) return console.log("❌ No Channel found");
350
+ if (!channel) return { error: "❌ No Channel found" };
360
351
  const message = await channel.messages.fetch(g.messageId);
361
- if (!message) return console.log("❌ No Message found");
352
+ if (!message) return { error: "❌ No Message found" };
362
353
 
363
- const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
364
- console.log(g)
354
+ const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
365
355
 
366
- message.embeds[0].data.fields.find((f) => f.name === "🎉 Giveaway").value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
367
- time / 1000
368
- )}:R>\n- Hosted By : <@${g.hoster}>`;
356
+ message.embeds[0].data.fields.find(
357
+ (f) => f.name === "🎉 Giveaway"
358
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
359
+ time / 1000
360
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
369
361
 
370
362
  await message.edit({ embeds: [message.embeds[0]] });
371
363
 
@@ -382,7 +374,7 @@ const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
382
374
 
383
375
  async function Gdelete(messageId) {
384
376
  const g = await giveaway.findOne({ messageId });
385
- if (!g) return null;
377
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
386
378
 
387
379
  await giveaway.findOneAndDelete({ messageId: messageId });
388
380
 
@@ -393,55 +385,57 @@ async function Gdelete(messageId) {
393
385
  }
394
386
 
395
387
  ////////////////////////////////////* Giveaway Add User
396
-
397
- async function GaddUser(messageId,client ,userId) {
388
+ async function GaddUser(messageId, userId) {
398
389
  const g = await giveaway.findOne({ messageId });
399
- if (!g) return null;
390
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
400
391
 
392
+ const all_users = g.users || [];
393
+ if (all_users.includes(userId)) return { error: "❌ User Already Joined" };
401
394
 
402
- await giveaway.findOneAndUpdate(
403
- { messageId: messageId },
404
- { $push: { users: userId } }
405
- );
395
+ if (!all_users.includes(userId)) {
396
+ all_users.push(userId);
397
+ await giveaway.findOneAndUpdate({ messageId }, { users: all_users });
398
+ }
406
399
  }
407
400
 
408
- ////////////////////////////////////! Giveaway remove User
409
-
401
+ ////////////////////////////////////! Giveaway Remove User
410
402
  async function GremoveUser(messageId, userId) {
411
403
  const g = await giveaway.findOne({ messageId });
412
- if (!g) return null;
404
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
413
405
 
414
- await giveaway.findOneAndUpdate(
415
- { messageId: messageId },
416
- { $pull: { users: userId } }
417
- );
406
+ const all_users = g.users || [];
407
+
408
+ if (!all_users.includes(userId)) return { error: "❌ User Not Joined" };
409
+
410
+ const index = all_users.indexOf(userId);
411
+ if (index !== -1) all_users.splice(index, 1);
412
+
413
+ await giveaway.findOneAndUpdate({ messageId }, { users: all_users });
418
414
  }
419
415
 
420
416
  ////////////////////////////////////* Giveaway Add Time
421
417
 
422
418
  async function GaddTime(messageId, client, time) {
423
419
  const g = await giveaway.findOne({ messageId });
424
- if (!g) return null;
420
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
425
421
 
426
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
427
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
422
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
423
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
428
424
 
429
- const guild = await client.guilds.cache.get(g.guildId);
425
+ const guild = await client.guilds.cache.get(g.guildId);
430
426
  const channel = await guild.channels.cache.get(g.channelId);
431
- if (!channel) return console.log("❌ No Channel found");
427
+ if (!channel) return { error: "❌ No Channel found" };
432
428
  const message = await channel.messages.fetch(g.messageId);
433
- if (!message) return console.log("❌ No Message found");
429
+ if (!message) return { error: "❌ No Message found" };
434
430
  if (typeof time !== "number" || time <= 0)
435
- return console.log("❌ Invalid time provided");
436
- const finel_time = Number(g.endTime) + Number(time);
437
-
438
-
439
-
440
-
441
- message.embeds[0].data.fields.find((f) => f.name === "🎉 Giveaway").value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
442
- finel_time / 1000
443
- )}:R>\n- Hosted By : <@${g.hoster}>`;
431
+ return { error: "❌ Invalid time provided" };
432
+ const finel_time = Number(g.endTime) + Number(time);
444
433
 
434
+ message.embeds[0].data.fields.find(
435
+ (f) => f.name === "🎉 Giveaway"
436
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
437
+ finel_time / 1000
438
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
445
439
 
446
440
  await message.edit({ embeds: [message.embeds[0]] });
447
441
 
@@ -453,29 +447,27 @@ const finel_time = Number(g.endTime) + Number(time);
453
447
 
454
448
  ////////////////////////////////////! Giveaway Remove Time
455
449
 
456
- async function GremoveTime(messageId,client, time) {
450
+ async function GremoveTime(messageId, client, time) {
457
451
  const g = await giveaway.findOne({ messageId });
458
- if (!g) return null;
452
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
459
453
 
460
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
461
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
454
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
455
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
462
456
 
463
- const guild = await client.guilds.cache.get(g.guildId);
457
+ const guild = await client.guilds.cache.get(g.guildId);
464
458
  const channel = await guild.channels.cache.get(g.channelId);
465
- if (!channel) return console.log("❌ No Channel found");
459
+ if (!channel) return { error: "❌ No Channel found" };
466
460
  const message = await channel.messages.fetch(g.messageId);
467
- if (!message) return console.log("❌ No Message found");
461
+ if (!message) return { error: "❌ No Message found" };
468
462
  if (typeof time !== "number" || time <= 0)
469
- return console.log("❌ Invalid time provided");
470
- const finel_time = Number(g.endTime) - Number(time);
471
-
472
-
473
-
474
-
475
- message.embeds[0].data.fields.find((f) => f.name === "🎉 Giveaway").value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
476
- finel_time / 1000
477
- )}:R>\n- Hosted By : <@${g.hoster}>`;
463
+ return { error: "❌ Invalid time provided" };
464
+ const finel_time = Number(g.endTime) - Number(time);
478
465
 
466
+ message.embeds[0].data.fields.find(
467
+ (f) => f.name === "🎉 Giveaway"
468
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
469
+ finel_time / 1000
470
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
479
471
 
480
472
  await message.edit({ embeds: [message.embeds[0]] });
481
473
 
@@ -485,7 +477,13 @@ const finel_time = Number(g.endTime) - Number(time);
485
477
  );
486
478
  }
487
479
 
480
+ ////////////////////////////////////? Giveaway Data
488
481
 
482
+ async function Gdata(messageId) {
483
+ const g = await giveaway.findOne({ messageId });
484
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
485
+ return g;
486
+ }
489
487
 
490
488
  module.exports = {
491
489
  Gstart,
@@ -498,5 +496,6 @@ module.exports = {
498
496
  GaddUser,
499
497
  GremoveUser,
500
498
  GaddTime,
501
- GremoveTime
499
+ GremoveTime,
500
+ Gdata,
502
501
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "djs-builder",
3
- "version": "0.6.34",
4
- "note": "🎉 Package Update! 🥏\n\n- Add Level System 🏆 \n\n- 🛠 Fixes:\n- Minor bugs fixed\n- Improved stability and error handling\n\n🔗 Learn more on [NPM](https://www.npmjs.com/package/djs-builder)",
3
+ "version": "0.6.36",
4
+ "note": "🎉 Package Update! 🥏\n\n- Add Giveaway System 🎉 \n\n- 🛠 Fixes:\n- Minor bugs fixed\n- Improved stability and error handling\n\n🔗 Learn more on [NPM](https://www.npmjs.com/package/djs-builder)",
5
5
  "description": "🎉 Package Update! 🥏",
6
6
  "main": "handler/starter.js",
7
7
  "dependencies": {