djs-builder 0.6.35 → 0.6.37

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 +1059 -4
  2. package/function/giveaway.js +161 -161
  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,54 @@ 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,
56
+ id : id = false
51
57
  },
52
- reaction: { type: type, emoji: emoji, label: label, style: style },
53
58
  }) {
54
59
  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");
60
+ if (!endTime) return { error: "End time is required." };
61
+ if (!channelId) return { error: "Channel ID is required." };
62
+ if (!context) return { error: "Context is required." };
63
+
64
+ if (type === "reaction" && !emoji) return { error: "Emoji is not required." };
60
65
 
61
66
  let row = [];
62
67
  if (type === "button") {
63
68
  row = CreateRow([
64
- {
65
- id: "djs-builder-giveaway",
66
- style: style || 1,
67
- label: label,
68
- emoji: emoji,
69
- },
69
+ [
70
+ {
71
+ id: id || "djs-builder-giveaway",
72
+ style: style || 1,
73
+ label: label,
74
+ emoji: emoji,
75
+ },
76
+ ],
70
77
  ]);
71
78
  }
72
79
 
73
80
  let embed;
74
81
 
75
82
  if (Scustom) {
76
- embed = new EmbedBuilder(Scustom);
83
+ embed = Scustom;
77
84
  } else {
78
85
  embed = new EmbedBuilder()
79
86
  .setTitle(Stitle || "🎉 Giveaway")
@@ -93,7 +100,7 @@ async function Gstart({
93
100
  });
94
101
 
95
102
  const channel = await context.guild.channels.cache.get(channelId);
96
- if (!channel) return console.log("❌ No Channel found");
103
+ if (!channel) return { error: "❌ No Channel found" };
97
104
 
98
105
  try {
99
106
  const message = await channel.send({
@@ -115,32 +122,30 @@ async function Gstart({
115
122
  if (Ethumbnail) end_embed.setThumbnail(Ethumbnail);
116
123
  }
117
124
 
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
- });
125
+ end_embed.addFields({
126
+ name: "🎉 Giveaway",
127
+ value: `- Winer(s): ${winers}\n- Time : <t:${Math.floor(
128
+ endTime / 1000
129
+ )}:R>\n- Hosted By : ${context.user ? context.user : context.author}`,
130
+ inline: true,
131
+ });
143
132
 
133
+ await giveaway.create({
134
+ guildId: context.guild.id,
135
+ channelId: channelId,
136
+ messageId: message.id,
137
+ hoster: context.user ? context.user.id : context.author.id,
138
+ endTime: endTime,
139
+ endType: false,
140
+ paused: false,
141
+ pausedTime: [],
142
+ winers: [],
143
+ winwesNumber: winers,
144
+ ended: false,
145
+ reaction: type,
146
+ endEmbed: end_embed.toJSON(),
147
+ users: [],
148
+ });
144
149
 
145
150
  if (type === "reaction") message.react(emoji);
146
151
  } catch (e) {
@@ -160,44 +165,37 @@ async function Gcheck(client) {
160
165
  }
161
166
  };
162
167
 
163
- check();
168
+ check();
164
169
  setInterval(check, 10 * 1000);
165
170
  }
166
171
 
167
172
  //////////////////////////////////* Giveaway Pick Winners 🎛️
168
173
 
169
-
170
174
  function pickWinners(arr, count) {
171
175
  const shuffled = [...arr].sort(() => Math.random() - 0.5);
172
176
  return shuffled.slice(0, count);
173
177
  }
174
178
 
175
-
176
-
177
179
  //////////////////////////////////! Giveaway End 📆
178
180
 
179
-
180
181
  async function giveaway_end(client, g, type) {
181
-
182
-
183
182
  const guild = await client.guilds.cache.get(g.guildId);
184
183
  const channel = await guild.channels.cache.get(g.channelId);
185
- if (!channel) return console.log("❌ No Channel found");
184
+ if (!channel) return { error: "❌ No Channel found" };
186
185
  const message = await channel.messages.fetch(g.messageId);
187
- if (!message) return console.log("❌ No Message found");
186
+ if (!message) return { error: "❌ No Message found" };
188
187
 
189
188
  try {
190
-
191
189
  let users_id = [];
192
190
  if (g.reaction === "reaction") {
193
191
  const reaction = message.reactions.cache.first();
194
192
  const users = await reaction.users.fetch();
195
193
  users_id = users.filter((u) => !u.bot).map((u) => u.id);
194
+ } else {
195
+ users_id = g.users;
196
196
  }
197
197
 
198
-
199
-
200
- if (!users_id.length) {
198
+ if (!users_id.length) {
201
199
  await message.reply("❌ No Users found for this giveaway.");
202
200
  await giveaway.findOneAndUpdate(
203
201
  { messageId: message.id },
@@ -206,17 +204,16 @@ async function giveaway_end(client, g, type) {
206
204
  return;
207
205
  }
208
206
 
209
-
210
207
  const winners = pickWinners(users_id, g.winersNumber);
211
208
 
212
-
213
209
  await message.edit({
214
210
  embeds: [g.endEmbed],
211
+ components: [],
215
212
  });
216
213
  await message.reply({
217
214
  content: `🎉 **Giveaway Ended!**\n> Winner(s):\n- <@${winners
218
215
  .map((u) => `${u}`)
219
- .join(">\n- <@")}>`,
216
+ .join(">, <@")}>`,
220
217
  });
221
218
 
222
219
  await giveaway.findOneAndUpdate(
@@ -232,41 +229,38 @@ async function giveaway_end(client, g, type) {
232
229
 
233
230
  async function Greroll(client, messageId) {
234
231
  const g = await giveaway.findOne({ messageId });
235
- if (!g) return null;
232
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
236
233
 
237
234
  const guild = await client.guilds.cache.get(g.guildId);
238
235
  const channel = await guild.channels.cache.get(g.channelId);
239
- if (!channel) return console.log("❌ No Channel found");
236
+ if (!channel) return { error: "❌ No Channel found" };
240
237
  const message = await channel.messages.fetch(g.messageId);
241
- if (!message) return console.log("❌ No Message found");
238
+ if (!message) return { error: "❌ No Message found" };
242
239
 
243
240
  try {
244
-
245
241
  let users_id = [];
246
242
  if (g.reaction === "reaction") {
247
243
  const reaction = message.reactions.cache.first();
248
244
  const users = await reaction.users.fetch();
249
245
  users_id = users.filter((u) => !u.bot).map((u) => u.id);
246
+ } else {
247
+ users_id = g.users;
250
248
  }
251
249
 
252
-
253
-
254
- if (!users_id.length) {
250
+ if (!users_id.length) {
255
251
  await message.reply("❌ No Users found for this giveaway.");
256
- await giveaway.findOneAndUpdate(
252
+ return giveaway.findOneAndUpdate(
257
253
  { messageId: message.id },
258
254
  { ended: true }
259
255
  );
260
- return;
261
256
  }
262
257
 
263
-
264
258
  const winners = pickWinners(users_id, g.winersNumber);
265
259
 
266
260
  await message.reply({
267
- content: `🔄️ **Giveaway Reroll!**\n> Winners:\n- <@${winners
261
+ content: `🔄️ **Giveaway Reroll!**\n> Winner(s):\n- <@${winners
268
262
  .map((u) => `${u}`)
269
- .join(">\n- <@")}>`,
263
+ .join(">, <@")}>`,
270
264
  });
271
265
 
272
266
  let new_endType;
@@ -282,8 +276,6 @@ async function Greroll(client, messageId) {
282
276
  { messageId: messageId },
283
277
  { winers: winners, endType: new_endType }
284
278
  );
285
-
286
-
287
279
  } catch (e) {
288
280
  console.log(e);
289
281
  }
@@ -304,14 +296,14 @@ async function Glist(type) {
304
296
  messageId: g.messageId,
305
297
  guildId: g.guildId,
306
298
  channelId: g.channelId,
307
- hoster: g.hoster,
299
+ hoster: g.hoster,
308
300
  ended: g.ended,
309
301
  endTime: g.endTime,
310
- paused: g.paused,
311
- pausedTime: g.pausedTime,
312
- endType: g.endType,
313
- winers: g.winers,
314
- winwesNumber: g.winwesNumber,
302
+ paused: g.paused,
303
+ pausedTime: g.pausedTime,
304
+ endType: g.endType,
305
+ winers: g.winers,
306
+ winwesNumber: g.winwesNumber,
315
307
  endEmbed: g.endEmbed,
316
308
  };
317
309
  });
@@ -321,22 +313,22 @@ async function Glist(type) {
321
313
 
322
314
  //////////////////////////////////* Giveaway Pause ⏸️
323
315
 
324
- async function Gpause(client ,messageId) {
316
+ async function Gpause(client, messageId) {
325
317
  const g = await giveaway.findOne({ messageId });
326
- if (!g) return null;
318
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
327
319
 
328
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
329
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
320
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
321
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
330
322
 
331
- const guild = await client.guilds.cache.get(g.guildId);
323
+ const guild = await client.guilds.cache.get(g.guildId);
332
324
  const channel = await guild.channels.cache.get(g.channelId);
333
- if (!channel) return console.log("❌ No Channel found");
325
+ if (!channel) return { error: "❌ No Channel found" };
334
326
  const message = await channel.messages.fetch(g.messageId);
335
- if (!message) return console.log("❌ No Message found");
327
+ if (!message) return { error: "❌ No Message found" };
336
328
 
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}>`;
329
+ message.embeds[0].data.fields.find(
330
+ (f) => f.name === "🎉 Giveaway"
331
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : Pause ⏸️\n- Hosted By : <@${g.hoster}>`;
340
332
 
341
333
  await message.edit({ embeds: [message.embeds[0]] });
342
334
  await giveaway.findOneAndUpdate(
@@ -347,25 +339,26 @@ async function Gpause(client ,messageId) {
347
339
 
348
340
  //////////////////////////////////* Giveaway Resume ▶️
349
341
 
350
- async function Gresume(client,messageId) {
342
+ async function Gresume(client, messageId) {
351
343
  const g = await giveaway.findOne({ messageId });
352
- if (!g) return null;
344
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
345
+
346
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
347
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
353
348
 
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);
349
+ const guild = await client.guilds.cache.get(g.guildId);
358
350
  const channel = await guild.channels.cache.get(g.channelId);
359
- if (!channel) return console.log("❌ No Channel found");
351
+ if (!channel) return { error: "❌ No Channel found" };
360
352
  const message = await channel.messages.fetch(g.messageId);
361
- if (!message) return console.log("❌ No Message found");
353
+ if (!message) return { error: "❌ No Message found" };
362
354
 
363
- const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
364
- console.log(g)
355
+ const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
365
356
 
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}>`;
357
+ message.embeds[0].data.fields.find(
358
+ (f) => f.name === "🎉 Giveaway"
359
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
360
+ time / 1000
361
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
369
362
 
370
363
  await message.edit({ embeds: [message.embeds[0]] });
371
364
 
@@ -382,7 +375,7 @@ const time = Date.now() + g.pausedTime[g.pausedTime.length - 1];
382
375
 
383
376
  async function Gdelete(messageId) {
384
377
  const g = await giveaway.findOne({ messageId });
385
- if (!g) return null;
378
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
386
379
 
387
380
  await giveaway.findOneAndDelete({ messageId: messageId });
388
381
 
@@ -393,55 +386,57 @@ async function Gdelete(messageId) {
393
386
  }
394
387
 
395
388
  ////////////////////////////////////* Giveaway Add User
396
-
397
- async function GaddUser(messageId,client ,userId) {
389
+ async function GaddUser(messageId, userId) {
398
390
  const g = await giveaway.findOne({ messageId });
399
- if (!g) return null;
391
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
400
392
 
393
+ const all_users = g.users || [];
394
+ if (all_users.includes(userId)) return { error: "❌ User Already Joined" };
401
395
 
402
- await giveaway.findOneAndUpdate(
403
- { messageId: messageId },
404
- { $push: { users: userId } }
405
- );
396
+ if (!all_users.includes(userId)) {
397
+ all_users.push(userId);
398
+ await giveaway.findOneAndUpdate({ messageId }, { users: all_users });
399
+ }
406
400
  }
407
401
 
408
- ////////////////////////////////////! Giveaway remove User
409
-
402
+ ////////////////////////////////////! Giveaway Remove User
410
403
  async function GremoveUser(messageId, userId) {
411
404
  const g = await giveaway.findOne({ messageId });
412
- if (!g) return null;
405
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
413
406
 
414
- await giveaway.findOneAndUpdate(
415
- { messageId: messageId },
416
- { $pull: { users: userId } }
417
- );
407
+ const all_users = g.users || [];
408
+
409
+ if (!all_users.includes(userId)) return { error: "❌ User Not Joined" };
410
+
411
+ const index = all_users.indexOf(userId);
412
+ if (index !== -1) all_users.splice(index, 1);
413
+
414
+ await giveaway.findOneAndUpdate({ messageId }, { users: all_users });
418
415
  }
419
416
 
420
417
  ////////////////////////////////////* Giveaway Add Time
421
418
 
422
419
  async function GaddTime(messageId, client, time) {
423
420
  const g = await giveaway.findOne({ messageId });
424
- if (!g) return null;
421
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
425
422
 
426
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
427
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
423
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
424
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
428
425
 
429
- const guild = await client.guilds.cache.get(g.guildId);
426
+ const guild = await client.guilds.cache.get(g.guildId);
430
427
  const channel = await guild.channels.cache.get(g.channelId);
431
- if (!channel) return console.log("❌ No Channel found");
428
+ if (!channel) return { error: "❌ No Channel found" };
432
429
  const message = await channel.messages.fetch(g.messageId);
433
- if (!message) return console.log("❌ No Message found");
430
+ if (!message) return { error: "❌ No Message found" };
434
431
  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}>`;
432
+ return { error: "❌ Invalid time provided" };
433
+ const finel_time = Number(g.endTime) + Number(time);
444
434
 
435
+ message.embeds[0].data.fields.find(
436
+ (f) => f.name === "🎉 Giveaway"
437
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
438
+ finel_time / 1000
439
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
445
440
 
446
441
  await message.edit({ embeds: [message.embeds[0]] });
447
442
 
@@ -453,29 +448,27 @@ const finel_time = Number(g.endTime) + Number(time);
453
448
 
454
449
  ////////////////////////////////////! Giveaway Remove Time
455
450
 
456
- async function GremoveTime(messageId,client, time) {
451
+ async function GremoveTime(messageId, client, time) {
457
452
  const g = await giveaway.findOne({ messageId });
458
- if (!g) return null;
453
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
459
454
 
460
- if(g.paused === true) return console.log("❌ Giveaway Already Paused");
461
- if(g.ended === true) return console.log("❌ Giveaway Already Ended");
455
+ if (g.paused === true) return { error: "❌ Giveaway Already Paused" };
456
+ if (g.ended === true) return { error: "❌ Giveaway Already Ended" };
462
457
 
463
- const guild = await client.guilds.cache.get(g.guildId);
458
+ const guild = await client.guilds.cache.get(g.guildId);
464
459
  const channel = await guild.channels.cache.get(g.channelId);
465
- if (!channel) return console.log("❌ No Channel found");
460
+ if (!channel) return { error: "❌ No Channel found" };
466
461
  const message = await channel.messages.fetch(g.messageId);
467
- if (!message) return console.log("❌ No Message found");
462
+ if (!message) return { error: "❌ No Message found" };
468
463
  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}>`;
464
+ return { error: "❌ Invalid time provided" };
465
+ const finel_time = Number(g.endTime) - Number(time);
478
466
 
467
+ message.embeds[0].data.fields.find(
468
+ (f) => f.name === "🎉 Giveaway"
469
+ ).value = `- Winer(s): ${g.winwesNumber}\n- Time : <t:${Math.floor(
470
+ finel_time / 1000
471
+ )}:R>\n- Hosted By : <@${g.hoster}>`;
479
472
 
480
473
  await message.edit({ embeds: [message.embeds[0]] });
481
474
 
@@ -485,7 +478,13 @@ const finel_time = Number(g.endTime) - Number(time);
485
478
  );
486
479
  }
487
480
 
481
+ ////////////////////////////////////? Giveaway Data
488
482
 
483
+ async function Gdata(messageId) {
484
+ const g = await giveaway.findOne({ messageId });
485
+ if (!g) return { error: "❌ Giveaway Data Not Found" };
486
+ return g;
487
+ }
489
488
 
490
489
  module.exports = {
491
490
  Gstart,
@@ -498,5 +497,6 @@ module.exports = {
498
497
  GaddUser,
499
498
  GremoveUser,
500
499
  GaddTime,
501
- GremoveTime
500
+ GremoveTime,
501
+ Gdata,
502
502
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "djs-builder",
3
- "version": "0.6.35",
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)",
3
+ "version": "0.6.37",
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": {