This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (127 loc) · 5.01 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
const logCmd = require("./logging.js");
const fs = require("fs");
const { AutoPoster } = require('topgg-autoposter');
// Import the discord.js module
global.Discord = require("discord.js");
// Create an instance of a Discord client
global.client = new global.Discord.Client(
{ ws: { intents: [
'GUILDS',
'GUILD_MESSAGES',
'GUILD_MEMBERS',
'DIRECT_MESSAGES',
'DIRECT_MESSAGE_REACTIONS',
(1<<15), // message_content
'DIRECT_MESSAGE_TYPING',
]}}
);
const ap = AutoPoster(fs.readFileSync(`${process.env.HOME}/.corki/topgg_token`).toString().trim(), global.client);
ap.on('posted', () => console.log('posted stats to top.gg'));
// bot will only start reacting to information
// from Discord _after_ ready is emitted
global.client.on("ready", () => {
console.log("Ready to fly!");
// in case of wanting to broadcast more: "corki.js.org | @Corki help \n \nPortal - corki.js.org/portal"
global.client.user.setActivity("corki.js.org | @ me for help", {
game: {
url: "https://corki.js.org",
type: 0
}
}).catch(console.error);
// spawn daemons:
// start web portal server
global.daemon_portal_server = require("./web/server.js");
// start auto-roles daemon
global.daemon_auto_roles = require("./sam/auto_roles/daemon.js");
const DBL = require("dblapi.js");
try {
const dbl_token = fs.readFileSync(`${process.env.HOME}/.corki/dbl_api_key`).toString().trim();
global.dbl = new DBL(dbl_token, global.client);
// Optional events
global.dbl.on('posted', () => {
//console.log('DBL server count posted!');
});
global.dbl.on('error', e => {
//console.log(`discorbotslist error! ${e}`);
});
} catch (e) {
console.log("DBL failed " + e);
}
});
// set up our list of commands
global.commands = []
.concat(require("./cmds/dev_cmds"))
.concat(require("./cmds/basic_cmds"))
.concat(require("./cmds/international_cmds"))
.concat(require("./cmds/text_cmds"))
.concat(require("./sam/roles_cmds"))
.concat(require("./sam/welcome"))
.concat(require("./lol/lol_commands"))
.concat(require("./lol/champgg_commands"))
.concat(require("./lol/lb_cmds"))
.concat(require("./rss/rss_cmds"))
.concat(require("./sam/prefix_cmds"))
.concat(require("./cmds/define"))
.concat(require("./cmds/help_cmds"))
.concat(require("./sam/blacklist_cmds"));
//
const interactions = [] .concat(require("./reactions"));
const bl = require("./sam/blacklist"); // should ignore msgs in certan servers/channels
const prefix = require("./sam/prefix"); // respond to different commands in dfferent severs
const bot_admins = require("./bot_admins"); // bot owner auth & meta stuff
const ct = require('./ct'); // Cross-guild-communication
// message event listener
async function messageHandler(msg) {
if ((msg.guild && bl.guilds().includes(msg.guild.id)) || bl.chans().includes(msg.channel.id))
return;
// check for prefix
const prefixes = [ `<@${global.client.user.id}>`, `<@!${global.client.user.id}>` ] // mention
.concat(msg.guild ? prefix.getGuildPrefixes(msg.guild.id) : [ '-' ]);
// performance for this is O(N) :(
// check to see if it has a relevant prefix
for (let i = 0; i < prefixes.length; i++)
// if it does, try to find the command
if (msg.content.match(new RegExp('^' + prefixes[i]))) {
msg.content = msg.content.replace(new RegExp('^' + prefixes[i]), "").trim();
// check each possible command
for (let i = 0; i < commands.length; i++)
// if it matches, run it
if (commands[i].condition(msg)) {
commands[i].act(msg)
.catch(e => {
msg.channel.send(`Sorry, that errored. If there's anything you would like to add, send a \`-bug\` report\n\`\`\`\n${e.stack}\n\`\`\``);
bot_admins.sendBugReport(msg, ` Error:\n\`\`\`\n${e.stack}\n\`\`\``);
});
break; // we're done here
}
break;
}
// special interactions can come from any message
// ie - if somoene says something bad about corki he reacts w/ question mark
interactions.forEach(i => {
if (i.condition(msg))
i.act(msg).catch(e => bot_admins.sendBugReport(msg,
`Interaction Error:\n\`\`\`\n${e.stack}\n\`\`\``));
});
};
global.client.on('message', messageHandler);
global.client.on('messageCreate', messageHandler);
// something broke
global.client.on("error", async e => {
bot_admins.sendBugReport(null, `Client Error:\n\`\`\`\n${e.stack || e.message || e}\n\`\`\``);
console.error("Client Error:", e);
});
// when corki is added to a server give server owner a quick intro
global.client.on("guildCreate", bot_admins.joinGuild);
// when corki is removed from server, try to ask owner why (only works if they're in a mutual server)
global.client.on("guildDelete", bot_admins.leaveGuild);
const welcome = require("./sam/welcome");
// welcome new members to a server
global.client.on("guildMemberAdd", member => {
// User-defined welcome messages
welcome.welcomeNewMember(member);
// TODO: ask new user to `-lol add` accts or sth (if desired by mods)
});
const token = fs.readFileSync(`${process.env.HOME}/.corki/disc_key`).toString().trim();
// Log bot in using token
global.client.login(token);