This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
193 lines (170 loc) · 6.65 KB
/
index.js
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
require('dotenv').config();
const Discord = require('discord.js');
const Mongoose = require('mongoose');
const Moment = require('moment');
const Express = require('express');
const https = require('https');
const http = require('http');
const fs = require('fs');
const ExpiringDocumentManager = require('./Classes/ExpiringDocumentManager');
const EmbedGenerator = require('./Functions/embedGenerator');
const { loadEvents } = require('./Handlers/eventHandler');
const { pickUnique } = require('./Functions/pickUnique');
const router = require('./server');
const { processErrorHandler } = require('./Handlers/errorHandler');
const Infractions = require('./Schemas/Infractions');
const Giveaways = require('./Schemas/Giveaways');
const Reminders = require('./Schemas/Reminders');
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.MessageContent,
],
partials: [
Discord.Partials,
Discord.Partials.Message,
Discord.Partials.GuildMember,
Discord.Partials.ThreadMember,
Discord.Partials.Reaction,
],
});
processErrorHandler();
client.commands = new Discord.Collection();
client.subCommands = new Discord.Collection();
client.expiringDocumentsManager = {
infractions: new ExpiringDocumentManager(
Infractions,
'expires',
async (infraction) => {
if (infraction.type == 'ban') {
const guild = await client.guilds
.fetch({ guild: infraction.guild })
.catch(() => null);
if (guild)
guild.members.unban(infraction.user, 'Temporary ban expired').catch(() => null);
}
infraction.active = false;
await infraction.save();
},
{ active: true }
),
giveaways: new ExpiringDocumentManager(
Giveaways,
'expires',
async (giveaway) => {
const guild = await client.guilds.fetch({ guild: giveaway.guild }).catch(() => null);
if (guild) {
/** @type {Discord.TextChannel} */ const channel = await guild.channels
.fetch(giveaway.channel)
.catch(() => null);
if (channel) {
const message = await channel.messages
.fetch({ message: giveaway.giveaway })
.catch(() => null);
if (message) {
/** @type {Array<String>} */ const winners = pickUnique(
giveaway.entries,
giveaway.winners
);
const embed = new Discord.EmbedBuilder(message.embeds[0].data);
embed.setDescription(
[
giveaway.description ? giveaway.description : null,
giveaway.description ? '' : null,
`Winners: **${giveaway.winners}**, Entries: **${giveaway.entries.length}**`,
`Status: Ended`,
]
.filter((text) => text !== null)
.join('\n')
);
await message.edit({ embeds: [embed], components: [] });
if (winners.length == 0) {
await channel.send({
embeds: [
EmbedGenerator.errorEmbed(
`💔 | Nobody entered the giveaway, there are no winners!`
),
],
reply: { messageReference: message },
});
} else {
await channel.send({
content: winners.map((id) => `<@${id}>`).join(' '),
embeds: [EmbedGenerator.basicEmbed(`Congratulations winners!`)],
reply: { messageReference: message },
});
}
}
}
}
giveaway.active = false;
await giveaway.save();
},
{ active: true }
),
reminders: new ExpiringDocumentManager(Reminders, 'expires', async (reminder) => {
const user = await client.users.fetch(reminder.user);
if (user) {
const embed = EmbedGenerator.basicEmbed(reminder.reminder).setAuthor({
name: 'Guardian Reminder',
iconURL: client.user.displayAvatarURL(),
});
if (reminder.repeating) {
const ends = Moment().add(reminder.duration);
embed.setDescription(
`${
embed.data.description
}\n\nYou will be reminded again in <t:${ends.unix()}:R>(<t:${ends.unix()}:f>)`
);
}
await user.send({ embeds: [embed] });
}
if (reminder.repeating) {
reminder.time = Date.now();
reminder.expires = reminder.time + reminder.duration;
return await reminder.save();
} else {
await reminder.delete();
}
}),
};
const app = Express();
let server;
if (process.env.LIVE === 'true') {
server = https.createServer(
{
key: fs.readFileSync(`${__dirname}/data/server/privkey.pem`),
cert: fs.readFileSync(`${__dirname}/data/server/fullchain.pem`),
},
app
);
} else {
server = http.createServer(app);
}
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
next();
});
app.use('/', router);
module.exports.client = client;
module.exports.server = server;
client.on('guildCreate', async (guild) => {
const DevGuilds = ['1109243816822702180']; // replace with your dev guild of server IDs
if (!DevGuilds.includes(guild.id)) {
await guild.leave();
}
});
client.on('messageCreate', (message) => {
if (message.mentions.has(client.user)) {
message.reply('Hello! My prefix is `/`');
}
});
Mongoose.connect(process.env.MONGODB_URL).then(async () => {
console.log('Client is connected to the database.');
await loadEvents(client);
client.login(process.env.DISCORD_TOKEN).then(() => {});
});