forked from ethersphere/swarm-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (91 loc) · 2.5 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
const config = require("config");
const Redis = require("ioredis");
const redis = new Redis(config.get("redis"));
if (!config.get("server")) {
console.error("Please configure a server");
process.exit(-1);
}
// Discord.js
const { Client, Intents } = require("discord.js");
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES,
],
});
// Background jobs
const deployed = require("./jobs/deployed");
const fund = require("./jobs/fund");
const plurRadio = require("./jobs/plur-radio");
// Requires bot and applications.commands
const commands = {
faucet: require("./commands/faucet"),
cleaner: require("./commands/cleaner"),
};
// Requires Manage Messages
const messages = [
require("./messages/cleaner"),
require("./messages/foreign-languages"),
];
// Jobs
const jobs = {};
// Functions
async function startJobs() {
jobs.deployed = await deployed.create(redis);
jobs.deployed.start();
jobs.fund = await fund.create({ redis, discord: client });
jobs.fund.start();
jobs.plurRadio = await plurRadio.create({ discord: client });
jobs.plurRadio.start();
}
client.on("ready", async () => {
console.log(`Logged in as ${client.user.tag}`);
const app = client.guilds.cache.get(config.get("server"));
// Delete old commands
const oldCommands = await app.commands.fetch();
await Promise.all(
[...oldCommands.keys()].map((id) => app.commands.delete(id))
);
// Add new commands
await Object.values(commands).map((command) =>
app.commands.create(command.CONFIG)
);
console.log(`Commands set up`);
await startJobs();
console.log("Jobs started");
});
client.on("interaction", (interaction) => {
if (
!interaction.isCommand() ||
interaction.guildID !== config.get("server")
) {
return;
}
const command = commands[interaction.commandName];
if (!command) {
return;
}
command.execute(interaction, { redis });
});
client.on("message", (message) => {
for (const { execute } of messages) {
execute(message, { redis });
}
});
// Log into the bot
client.login(config.get("bot.token"));
// Graceful shutdown
const events = ["SIGINT", "SIGTERM", "SIGQUIT"];
const gracefulShutdown = async () => {
console.log("Stopping jobs...");
for (const job of Object.values(jobs)) {
if (job.stop) {
await job.stop();
}
}
console.log("Closing redis and Discord...");
client.destroy();
redis.disconnect();
};
events.map((event) => process.on(event, gracefulShutdown));