-
Notifications
You must be signed in to change notification settings - Fork 2
/
runequest-discordbot.js
150 lines (113 loc) · 4.11 KB
/
runequest-discordbot.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
const Discord = require('discord.js');
const utils = require('./utils');
const config = require('./config');
const dieRollr = require('./dierollr/dierollr.js');
let knownGuildChannels = [];
const options = {};
if (config.intents)
options.ws = { intents: config.intents };
const fs = require('fs');
const filenames = fs.readdirSync('./commands').filter((filename) => filename.endsWith('.js'));
const allCommands = filenames.map((filename) => require(`./commands/${filename}`) );
let errorsLeft = config.errorMax || 20;
/**
* Create, setup listeners, then login a Discord Client
* @param loginKey {string} required
* @param userOptions (optional)
* @returns {Discord.Client}
*/
function setup(loginKey, userOptions = {}) {
let _options = Object.assign({}, options, userOptions);
/* 1. Create */
let client = new Discord.Client(_options);
/* 2. setup listeners (length) */
client.once('ready', () => {
console.log(config.name + ' is ready! Existing Guilds:');
console.dir( { guilds : client.guilds.cache.mapValues((c) => c.name) } );
return client.user.setActivity(` ${config.prefix}`, { type: 2 });
});
// main function for message handling
client.on('message', function(message) {
if (message.author.bot) // ignore other bots (and myself)
return;
let guildAndChannel = `${message.channel.guild.name}.${message.channel.name}`;
if (config.logLevel) {
let msg = `${guildAndChannel}.${message.author.username}: "${message.content}"`;
console.log(msg);
}
if (!knownGuildChannels.includes(guildAndChannel)) {
console.log(`New Guild.Channel: ${guildAndChannel} joined at ${new Date()}`);
knownGuildChannels.push(guildAndChannel);
}
let response = null;
try {
response = handleUserInput(message);
}
catch (err) {
response = "Sorry, there was an error processing your message: " + message.content;
console.dir({message, err});
--errorsLeft;
}
if (response) {
message.channel.send(response)
.then(function() {
if (errorsLeft <= 0) {
console.log(`**Exiting**, too many errors at ${new Date()}`);
process.exit(-1);
}
});
}
});
// log when we join a new guild
client.on("guildCreate", function(guild) { // doesn't seem to do much...
console.log("Joined a new guild: " + guild.name);
});
/* 3. Log our bot in using the token from https://discord.com/developers/applications */
return client.login(loginKey)
.then(() => client);
}
function handleUserInput(message) {
// console.dir(message);
let line = message.content.toLowerCase();
if (line.startsWith(config.prefix) ) {
let argumentsExcludingMentions = line.split(/ +/).filter((x) => !x.startsWith('<@!'));
let [ignored, userCommand, ...args] = argumentsExcludingMentions;
let response = null;
// handle 'help', 'info'
if (!userCommand || (userCommand === 'help'))
response = doHelp(args);
else if (userCommand === 'info')
response = doInfo(args);
else if (userCommand === 'err') // for testing
throw new Error('forced error');
// other commands get delegated
else
response = delegateToCommands(userCommand, args);
return response || 'rq-bot: bad input ' + line;
}
else if (line.startsWith(config.dierollPrefix))
return dieRollr(message);
return null;
}
function delegateToCommands(userCommand, args) {
for (let command of allCommands) {
if (command.aliases.includes(userCommand)) {
let doHandle = command.doHandle || utils.handler100s;
return doHandle(userCommand, args, utils, command);
}
}
return null;
}
function doHelp(args) {
let head = `All commands start with ${config.prefix}, followed by a command and options\n`;
args = args.length ? args : allCommands;
return args.reduce( helpForCommand, head);
}
function doInfo() {
return `${config.name} v${config.version}`;
}
function helpForCommand(acc, command) {
let help = command.help || 'No help available';
return acc + ` [${command.aliases}] (${command.name}) ${help} \n`
}
module.exports = { handleUserInput, setup, utils };