-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
454 lines (376 loc) · 20.2 KB
/
Copy pathindex.js
File metadata and controls
454 lines (376 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
require('dotenv').config();
const { Client, GatewayIntentBits, EmbedBuilder, REST, Routes, ActivityType } = require('discord.js');
const express = require('express');
const ms = require('ms');
const app = express();
const PORT = process.env.PORT || 3000;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions
]
});
const activeGiveaways = new Map();
const endedGiveaways = new Map();
// Slash commands
const commands = [
{
name: 'start',
description: 'Start a new giveaway',
options: [
{ name: 'channel', type: 7, description: 'Channel to start giveaway in', required: true },
{ name: 'duration', type: 3, description: 'Duration (e.g., 1d, 2h)', required: true },
{ name: 'prize', type: 3, description: 'Prize to win', required: true },
{ name: 'winners', type: 4, description: 'Number of winners', required: true }
]
},
{
name: 'end',
description: 'End a giveaway early',
options: [
{ name: 'message_id', type: 3, description: 'Giveaway message ID', required: true }
]
},
{
name: 'reroll',
description: 'Reroll an ended giveaway',
options: [
{ name: 'message_id', type: 3, description: 'Ended giveaway message ID', required: true }
]
},
{
name: 'stats',
description: 'Show bot statistics'
},
{
name: 'invite',
description: 'Get bot invite link'
},
{
name: 'support',
description: 'Get support server link'
},
{
name: 'help',
description: 'Show help information'
}
];
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
(async () => {
try {
console.log('Registering slash commands...');
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands }
);
console.log('Slash commands registered successfully!');
} catch (error) {
console.error('Error registering commands:', error);
}
})();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setActivity('/help', { type: ActivityType.Playing });
});
// Helper function to create giveaway embed
function createGiveawayEmbed(duration, prize, winners) {
return new EmbedBuilder()
.setTitle('🎉 GIVEAWAY 🎉')
.setDescription(
`**Prize:** ${prize}\n` +
`**Duration:** ${duration}\n` +
`**Winners:** ${winners}\n\n` +
'React with 🎉 to enter!'
)
.setColor('#FFD700')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
}
// Helper function to end giveaway
async function endGiveaway(messageId, channel) {
if (!activeGiveaways.has(messageId)) return false;
const giveaway = activeGiveaways.get(messageId);
clearTimeout(giveaway.timeout);
activeGiveaways.delete(messageId);
const message = await channel.messages.fetch(messageId).catch(() => null);
if (!message) return false;
const reactions = await message.reactions.cache.get('🎉').users.fetch();
const participants = reactions.filter(user => !user.bot).map(user => user.id);
let winners = [];
for (let i = 0; i < giveaway.winners && participants.length > 0; i++) {
const winnerIndex = Math.floor(Math.random() * participants.length);
winners.push(`<@${participants[winnerIndex]}>`);
participants.splice(winnerIndex, 1);
}
const winnerText = winners.length > 0 ? winners.join(', ') : 'No valid participants';
const endEmbed = new EmbedBuilder()
.setTitle('🎉 GIVEAWAY ENDED 🎉')
.setDescription(
`**Prize:** ${giveaway.prize}\n` +
`**Winners:** ${winnerText}`
)
.setColor('#FF0000')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
const endMessage = await channel.send({ embeds: [endEmbed] });
endedGiveaways.set(messageId, {
channelId: channel.id,
prize: giveaway.prize,
winners: giveaway.winners,
endedAt: new Date(),
endMessageId: endMessage.id
});
return true;
}
// Helper function to reroll giveaway
async function rerollGiveaway(messageId) {
if (!endedGiveaways.has(messageId)) return null;
const giveaway = endedGiveaways.get(messageId);
const channel = await client.channels.fetch(giveaway.channelId);
const originalMessage = await channel.messages.fetch(messageId).catch(() => null);
if (!originalMessage) return null;
const reactions = await originalMessage.reactions.cache.get('🎉').users.fetch();
const participants = reactions.filter(user => !user.bot).map(user => user.id);
let newWinners = [];
for (let i = 0; i < giveaway.winners && participants.length > 0; i++) {
const winnerIndex = Math.floor(Math.random() * participants.length);
newWinners.push(`<@${participants[winnerIndex]}>`);
participants.splice(winnerIndex, 1);
}
return {
prize: giveaway.prize,
winners: newWinners,
channel,
endMessageId: giveaway.endMessageId
};
}
// Prefix commands
client.on('messageCreate', async message => {
if (message.author.bot || !message.content.startsWith(process.env.PREFIX)) return;
const args = message.content.slice(process.env.PREFIX.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'start') {
if (!message.member.permissions.has('ManageMessages')) {
return message.reply('You need the Manage Messages permission to start giveaways.');
}
const channel = message.mentions.channels.first();
if (!channel) return message.reply('Please mention a valid channel.');
const duration = args[1];
if (!duration) return message.reply('Please specify a duration (e.g., 1d, 2h).');
const prize = args.slice(2, args.length - 1).join(' ');
if (!prize) return message.reply('Please specify a prize.');
const winners = parseInt(args[args.length - 1]);
if (isNaN(winners) || winners < 1) return message.reply('Please specify a valid number of winners.');
const embed = createGiveawayEmbed(duration, prize, winners);
const giveawayMessage = await channel.send({ embeds: [embed] });
await giveawayMessage.react('🎉');
const timeout = setTimeout(async () => {
await endGiveaway(giveawayMessage.id, channel);
}, ms(duration));
activeGiveaways.set(giveawayMessage.id, {
channelId: channel.id,
prize,
winners,
timeout
});
await message.reply(`Giveaway started in ${channel}! ${client.user.username} will handle the rest!`);
}
if (command === 'end') {
if (!message.member.permissions.has('ManageMessages')) {
return message.reply('You need the Manage Messages permission to end giveaways.');
}
const messageId = args[0];
if (!messageId) return message.reply('Please provide a giveaway message ID.');
const success = await endGiveaway(messageId, message.channel);
if (!success) return message.reply('Could not find an active giveaway with that ID.');
await message.reply(`${client.user.username} ended the giveaway successfully!`);
}
if (command === 'reroll') {
if (!message.member.permissions.has('ManageMessages')) {
return message.reply('You need the Manage Messages permission to reroll giveaways.');
}
const messageId = args[0];
if (!messageId) return message.reply('Please provide an ended giveaway message ID.');
const result = await rerollGiveaway(messageId);
if (!result) return message.reply('Could not find an ended giveaway with that ID.');
const winnerText = result.winners.length > 0 ? result.winners.join(', ') : 'No valid participants';
const rerollEmbed = new EmbedBuilder()
.setTitle('🎉 GIVEAWAY REROLLED 🎉')
.setDescription(
`**Prize:** ${result.prize}\n` +
`**New Winners:** ${winnerText}`
)
.setColor('#00FF00')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
const endMessage = await result.channel.messages.fetch(result.endMessageId).catch(() => null);
if (endMessage) {
await endMessage.edit({ embeds: [rerollEmbed] });
} else {
await result.channel.send({ embeds: [rerollEmbed] });
}
await message.reply(`${client.user.username} rerolled the giveaway successfully!`);
}
if (command === 'stats') {
const embed = new EmbedBuilder()
.setTitle(`${client.user.username} Statistics`)
.addFields(
{ name: 'Servers', value: client.guilds.cache.size.toString(), inline: true },
{ name: 'Users', value: client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0).toString(), inline: true },
{ name: 'Active Giveaways', value: activeGiveaways.size.toString(), inline: true },
{ name: 'Ended Giveaways', value: endedGiveaways.size.toString(), inline: true }
)
.setColor('#7289DA')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
await message.reply({ embeds: [embed] });
}
if (command === 'invite') {
const inviteLink = `https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}&permissions=277025770560&scope=bot%20applications.commands`;
await message.reply(`Invite ${client.user.username} to your server: ${inviteLink}`);
}
if (command === 'support') {
await message.reply(`Join our support server: https://discord.com/invite/9MVAPpfs8D\n\nNeed help with ${client.user.username}? We're here to help!`);
}
if (command === 'help') {
const embed = new EmbedBuilder()
.setTitle(`${client.user.username} Commands Help`)
.setDescription(`Here are all the available commands for ${client.user.username}:`)
.addFields(
{ name: `${process.env.PREFIX}start #channel duration prize winners`, value: 'Start a new giveaway' },
{ name: `${process.env.PREFIX}end message_id`, value: 'End a giveaway early' },
{ name: `${process.env.PREFIX}reroll message_id`, value: 'Reroll an ended giveaway' },
{ name: `${process.env.PREFIX}stats`, value: 'Show bot statistics' },
{ name: `${process.env.PREFIX}invite`, value: 'Get bot invite link' },
{ name: `${process.env.PREFIX}support`, value: 'Get support server link' },
{ name: `${process.env.PREFIX}help`, value: 'Show this help message' }
)
.setColor('#7289DA')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
await message.reply({ embeds: [embed] });
}
});
// Slash commands
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
if (commandName === 'start') {
if (!interaction.memberPermissions.has('ManageMessages')) {
return interaction.reply({ content: 'You need the Manage Messages permission to start giveaways.', ephemeral: true });
}
const channel = options.getChannel('channel');
const duration = options.getString('duration');
const prize = options.getString('prize');
const winners = options.getInteger('winners');
const embed = createGiveawayEmbed(duration, prize, winners);
const giveawayMessage = await channel.send({ embeds: [embed] });
await giveawayMessage.react('🎉');
const timeout = setTimeout(async () => {
await endGiveaway(giveawayMessage.id, channel);
}, ms(duration));
activeGiveaways.set(giveawayMessage.id, {
channelId: channel.id,
prize,
winners,
timeout
});
await interaction.reply({ content: `Giveaway started in ${channel}! ${client.user.username} will handle the rest!`, ephemeral: true });
}
if (commandName === 'end') {
if (!interaction.memberPermissions.has('ManageMessages')) {
return interaction.reply({ content: 'You need the Manage Messages permission to end giveaways.', ephemeral: true });
}
const messageId = options.getString('message_id');
const success = await endGiveaway(messageId, interaction.channel);
if (!success) {
return interaction.reply({ content: 'Could not find an active giveaway with that ID.', ephemeral: true });
}
await interaction.reply({ content: `${client.user.username} ended the giveaway successfully!`, ephemeral: true });
}
if (commandName === 'reroll') {
if (!interaction.memberPermissions.has('ManageMessages')) {
return interaction.reply({ content: 'You need the Manage Messages permission to reroll giveaways.', ephemeral: true });
}
const messageId = options.getString('message_id');
const result = await rerollGiveaway(messageId);
if (!result) {
return interaction.reply({ content: 'Could not find an ended giveaway with that ID.', ephemeral: true });
}
const winnerText = result.winners.length > 0 ? result.winners.join(', ') : 'No valid participants';
const rerollEmbed = new EmbedBuilder()
.setTitle('🎉 GIVEAWAY REROLLED 🎉')
.setDescription(
`**Prize:** ${result.prize}\n` +
`**New Winners:** ${winnerText}`
)
.setColor('#00FF00')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
const endMessage = await result.channel.messages.fetch(result.endMessageId).catch(() => null);
if (endMessage) {
await endMessage.edit({ embeds: [rerollEmbed] });
} else {
await result.channel.send({ embeds: [rerollEmbed] });
}
await interaction.reply({ content: `${client.user.username} rerolled the giveaway successfully!`, ephemeral: true });
}
if (commandName === 'stats') {
const embed = new EmbedBuilder()
.setTitle(`${client.user.username} Statistics`)
.addFields(
{ name: 'Servers', value: client.guilds.cache.size.toString(), inline: true },
{ name: 'Users', value: client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0).toString(), inline: true },
{ name: 'Active Giveaways', value: activeGiveaways.size.toString(), inline: true },
{ name: 'Ended Giveaways', value: endedGiveaways.size.toString(), inline: true }
)
.setColor('#7289DA')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
await interaction.reply({ embeds: [embed] });
}
if (commandName === 'invite') {
const inviteLink = `https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}&permissions=277025770560&scope=bot%20applications.commands`;
await interaction.reply({ content: `Invite ${client.user.username} to your server: ${inviteLink}`, ephemeral: true });
}
if (commandName === 'support') {
await interaction.reply({
content: `${client.user.username} support server: https://discord.com/invite/9MVAPpfs8D\n\nGet help with giveaways and more!`,
ephemeral: true
});
}
if (commandName === 'help') {
const embed = new EmbedBuilder()
.setTitle(`${client.user.username} Commands`)
.setDescription(`Here are all the available commands for ${client.user.username}:`)
.addFields(
{ name: '/start channel duration prize winners', value: 'Start a new giveaway' },
{ name: '/end message_id', value: 'End a giveaway early' },
{ name: '/reroll message_id', value: 'Reroll an ended giveaway' },
{ name: '/stats', value: 'Show bot statistics' },
{ name: '/invite', value: 'Get bot invite link' },
{ name: '/support', value: 'Get support server link' },
{ name: '/help', value: 'Show this help message' }
)
.setColor('#7289DA')
.setFooter({ text: `${client.user.username} Giveaway System` })
.setTimestamp();
await interaction.reply({ embeds: [embed] });
}
});
app.get('/', (req, res) => {
res.send(`${client.user?.username || 'Giveaway Bot'} is running!`);
});
client.login(process.env.TOKEN)
.then(() => {
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`${client.user.username} is ready!`);
});
})
.catch(err => {
console.error('Failed to login:', err);
process.exit(1);
});