-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNomicBot.js
314 lines (224 loc) · 7.36 KB
/
NomicBot.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
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
/**
* Nomic Discord bot
*
* @author Anthony Wilson
*
* @version 4.3.0
*
* @since 2021-8-7
*/
"use strict";
//Discord.js classes
const { Client, Intents } = require("discord.js");
//Fetch
const fetch = require("node-fetch");
//Timers
const timers = require("timers");
//Various configurations/settings
global.Config = require("./config.json");
global.cmdpref = Config.prefix;
global.sitePath = Config.sitePath;
//Some minor things will function differently if Nomic Bot is running in development mode
global.devmode = Config.devmode;
//Secure/sensitive information (token, player user IDs, etc)
global.SecureInfo = require("./secureinfo.json");
//Rule class
global.Rule = require(sitePath+"/Rules/rule-class.js").Rule;
//Rules, player info, and propositions lists
global.RawRules = new Rule( require(sitePath+"/Rules/rules.json") );
global.Rules = new Rule( require(sitePath+"/Rules/rules.json") );
global.Players = require(sitePath+"/Players/players.json");
global.Propositions = require(sitePath+"/Propositions/propositions.json");
//Import miscellaneous utility functions
const Utils = require("./utils.js");
global.rand = Utils.rand;
global.logMessage = Utils.logMessage;
global.identifyPlayer = Utils.identifyPlayer;
global.getUser = Utils.getUser;
global.decodeVaultData = Utils.decodeVaultData;
global.encodeVaultData = Utils.encodeVaultData;
global.updateFile = Utils.updateFile;
global.getAttrList = Utils.getAttrList;
//Command functions and commands list
const Commands = require("./commands.js");
//Functions specific to propositions
global.PropositionFunctions = require("./propositions.js");
global.PropositionParsing = require(sitePath+"/Propositions/parse.js");
//Create a new client instance
global.client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS
],
partials: ["MESSAGE", "CHANNEL", "REACTION"]
});
/**
* @listens ready
* Triggered when the application has successfully connected.
*/
client.on("ready", () => {
console.log(`Connected to ${client.user.tag}`);
//Set the status of the bot
setPresence();
//Update the message in #links that links to the website
updateServerURLMsg();
//Schedule proposition checks
for(var p = 0;p < Propositions.length;p ++){
PropositionFunctions.scheduleUpdate(p);
}
///Still to do: Get disconnect notifications working
//console.log(client.ws);
//console.log(client.ws.shards.get(0));
//console.log("Function:",client.ws.shards.get(0).heartbeatInterval._onTimeout.toString());
logMessage("Nomic Bot has successfully started");
});
/**
* @async
* Update the message in #links that links to the website.
* Initially called by @event ready then runs again every 5 minutes.
*/
var updateServerURLMsg = async () => {
var req = await fetch("http://ifconfig.me/ip");
var ip = await req.text();
var msg = "Nomic website links\nGithub: <https://anthonyw2.github.io/Nomic/>\nAnthony's server: http://"+ip+":8084/Nomic";
var linksChannel = client.channels.cache.get(SecureInfo.channels[5].ID);
//Only used once to send the initial message
///linksChannel.send(msg);
//Get the message with the server URL in it
var message = await linksChannel.messages.fetch("962154858981507122");
//Check if the message needs to be updated
if(message.content != msg){
message.edit(msg);
logMessage("Updated server URL message");
}
//Reset the presence of the bot while we're here
setPresence();
//Update the link message again (if necessary) in 5 minutes
timers.setTimeout(updateServerURLMsg, 1000*60*5);
}
/**
* @async
* Update the status of the bot
* Initially called by @event ready and called again every 5 minutes.
*/
var setPresence = async () => {
//Set a different presence if the bot is running in development mode
if(devmode){
client.user.setPresence({
status: "online",
activities: [
{ type: "PLAYING", name: "Development" }
]
});
return;
}
//Set default presence
client.user.setPresence({
status: "online",
activities: [
{ type: "PLAYING", name: "Nomic" },
///{ type: "WATCHING", name: "your votes" },
///{ type: "LISTENING", name: "the void" },
]
});
}
/**
* @async
* @listens messageCreate
* Triggered when a message is sent in a Guild or DM that Nomic Bot has access to.
* @param {Message} message
*/
client.on("messageCreate", async (message) => {
//Test if the message is in the #propositions channel
if(message.channelId === SecureInfo.channels[1].ID){
PropositionFunctions.createProposition(message);
}
//Execute the command contained in the message if applicable
Commands.processMessage(message);
});
/**
* @async
* @listens interactionCreate
* Triggered when a slash command is used in a Guild or DM that Nomic Bot has access to.
* @param {CommandInteraction} interaction
*/
client.on("interactionCreate", async (interaction) => {
Commands.processInteraction(interaction);
});
/**
* @async
* @listens messageUpdate
* Triggered when a message is edited in a Guild or DM that Nomic Bot has access to.
* @param {Message} oldMessage
* @param {Message} newMessage
*/
client.on("messageUpdate", async (oldMessage, newMessage) => {
//Test if the message is in the #propositions channel
if(newMessage.channelId === SecureInfo.channels[1].ID){
PropositionFunctions.updateProposition(newMessage);
}
});
/**
* @async
* @listens messageReactionAdd
* Triggered when a user reacts to a message.
* @param {MessageReaction} reaction
* @param {User} user
*/
client.on("messageReactionAdd", async (reaction, user) => {
//If the reaction message is not cached, attempt to fetch it from Discord
if(reaction.partial){
try {
await reaction.fetch();
}catch(error) {
console.error("Failed to fetch reaction message", error);
return;
}
}
if(reaction.message.channelId === SecureInfo.channels[1].ID){
//if(user.id != SecureInfo.botID || reaction._emoji.id == Config.emoji.rightvote){
if(user.id != SecureInfo.botID){
PropositionFunctions.handleVote(reaction);
}
}
});
/**
* @async
* @listens messageReactionRemove
* Triggered when a user removes a reaction to a message.
* @issue Known issue: If the reaction isn't already cached, this event will not trigger.
* @param {MessageReaction} reaction
* @param {User} user
*/
client.on("messageReactionRemove", async (reaction, user) => {
//If the reaction message is not cached, attempt to fetch it from Discord
if(reaction.partial){
try {
await reaction.fetch();
}catch(error) {
console.error("Failed to fetch reaction message", error);
return;
}
}
if(reaction.message.channelId === SecureInfo.channels[1].ID){
if(user.id != SecureInfo.botID){
PropositionFunctions.handleVote(reaction);
}
}
});
/**
* @async
* @listens rateLimit
* Triggered when the client hits a rate limit while making a request
* @param {RateLimitData} rateLimitData
*/
//client.on("rateLimit", async (rateLimitData) => {
//
// console.log("Rate limited",rateLimitData);
//
//});
// Connect to the Discord API and log into the Nomic Bot application
client.login(SecureInfo.token);