This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbot.js
82 lines (63 loc) · 2.43 KB
/
bot.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
var discord = require('discord.js');
var fs = require('fs');
var randomColour = require('randomcolor'); // yes, the creator of this package does not speak the real english
var Config = require('./config.json');
class Bot {
constructor(){
this.servers = require('./servers.json');
this.discordClient = new discord.Client({sync: true});
this.discordClient.on("ready", () => {this.initialize();});
this.discordClient.on("message", (msg) => {this.processMessage(msg)});
this.discordClient.login(Config.discord_token);
}
initialize() {
this.log("Connected to discord.");
setInterval(() => {
this.randomizeRoleColors();
}, Config.randomize_delay*1000);
}
processMessage(msg) {
if(msg.content.startsWith(">addrole")) {
for(var role of msg.mentions.roles.array()) {
msg.reply("Added " + role + " to list of rainbow roles.");
this.addRainbowRole(msg.guild.id, role.id);
}
}
}
randomizeRoleColors() {
for(var server in this.servers) {
var liveGuild = this.discordClient.guilds.get(server);
if (!liveGuild) {
this.error("Guild with ID " + server+ " no longer exists or the bot has been removed from it.");
continue;
}
for(var role of this.servers[server]) {
var liveRole = liveGuild.roles.get(role);
liveRole.setColor(randomColour(), "Rainbowbot random role color randomizer.");
}
}
}
addRainbowRole(guild, role) {
if(this.servers[guild] == undefined) {
this.servers[guild] = [];
}
for(var existingRole of this.servers[guild]) {
if(existingRole == role) {
return "That role has already been added.";
}
}
this.servers[guild].push(role);
this.saveServers();
}
saveServers() {
fs.writeFileSync("./servers.json", JSON.stringify(this.servers), "utf8");
this.log("Saved servers file.");
}
log(message) {
console.log("\x1b[32mINFO\x1b[37m - \x1b[0m" + message);
}
error(message) {
console.log("\x1b[31mERROR\x1b[37m - \x1b[0m" + message);
}
}
var instance = new Bot();