-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshard.ts
133 lines (119 loc) · 3.21 KB
/
shard.ts
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
import { config as dotenv } from "dotenv";
import { readdirSync } from "fs";
import { join } from "path";
import {
ActivityType,
Client,
GatewayIntentBits,
PermissionsBitField,
ShardClientUtil,
} from "discord.js";
import Logger from "./logger";
import db from "./mongo";
import custom_statuses from "./custom_statuses.json";
db.connect().then(() => {});
dotenv();
const events: Map<string, any> = new Map();
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.MessageContent,
],
});
function loadEvents() {
if (process.env.ENABLEDEBUG)
bot.on("debug", function (m) {
console.log(m);
});
for (const file of readdirSync(join(__dirname, "events")).filter((f) =>
f.endsWith(".js"),
)) {
const requiredEvent = require(`./events/${file}`);
events.set(file.replace(".js", ""), requiredEvent);
}
events.forEach((_value, key) => {
const event = events.get(key);
bot.on(key, async (...args: any[]) => {
await event(...args);
});
});
}
async function logDebug(message: any) {
console.log(`SHARD ${bot.shard?.ids[0]}:\n${message}`);
}
loadEvents();
bot.login().catch((e) => {
Logger(e);
process.exit();
});
process.on("enableDebug", async function () {
bot.on("debug", logDebug);
});
process.on("disableDebug", async function () {
bot.off("debug", logDebug);
});
const mongo = db.db("bot");
setInterval(async function (): Promise<void> {
const selectedStatus =
custom_statuses[Math.round(Math.random() * (custom_statuses.length - 1))];
bot.user?.setActivity(selectedStatus, {
shardId: bot.shard?.ids[0],
state: selectedStatus,
type: ActivityType.Custom,
});
}, 120000);
setInterval(async function (): Promise<void> {
try {
const bans = await mongo
.collection("bans")
.find({ unban: { $lte: Date.now() } })
.toArray();
for (const ban of bans) {
const shard = ShardClientUtil.shardIdForGuildId(
ban.server,
bot.shard?.count ?? 1,
);
await bot.shard?.broadcastEval(
async (c) => {
const server = await c.guilds.fetch(ban.server).catch(() => {});
if (
!server?.members?.me?.permissions?.has(
PermissionsBitField.Flags.BanMembers,
)
)
return;
const member = await server.bans.fetch(ban.user).catch(() => {});
if (!member) return;
try {
await server.bans.remove(member.user.id, "Temporary ban expired.");
await mongo
.collection("bans")
.findOneAndDelete({ user: member.user.id });
} catch (e) {
console.error(e);
}
},
{ shard: shard },
);
}
} catch (e) {
Logger(e);
return;
}
try {
await mongo
.collection("reports")
.updateMany(
{ created: { $lte: Date.now() - 2592000000 } },
{ $set: { "message.content": "[ Content Deleted ]" } },
);
} catch (e) {
Logger(e);
}
}, 30000);