-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.js
91 lines (86 loc) · 3.53 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
83
84
85
86
87
88
89
90
91
// Core bot setup
require('./randomUtil')
const fs = require('fs')
const config = require('./config.json')
const { token } = require('./.token.json')
const discord = require("discord.js")
const client = new discord.Client()
// Dynamically load all operations we care about into a single commander object
loadAllOperations = function(libNames){
let allOps = {}, meta = {}
// Get each lib by name
for (lib of libNames) {
meta[lib] = []
let libOps = require(lib);
for (op in libOps) {
// Stash all op names at meta[libname] for help reference
allOps[op] = libOps[op]
meta[lib].push(op)
}
// These will clobber eachother, this keeps them split up
meta[lib].helptext = libOps['helptext']()
}
return [ allOps, meta ]
}
// Always keep gravemind at the end
const MODULES = [ './discordlib', './dungeonary', './gravemind' ]
const [ commander, metadata ] = loadAllOperations(MODULES)
// In case something happens, we'll want to see logs
client.on("error", (e) => console.error(e))
// Startup callback
client.on('ready', () => {
if (process.env.NODE_ENV) {
console.log(`${process.env.NODE_ENV} mode activated!`)
} else {
console.log(`NODE_ENV not set, running in dev mode`)
}
console.log(`Tavernbot v${config.version} has logged in as ${client.user.tag}!`)
client.user.setPresence({
"status": "online",
"game": { "name": config.gameStatus }
})
})
// Command central
client.on('message', msg => {
// Contain the bot, and ensure we actually want to act on the command
let channelName = msg.channel.name ? msg.channel.name.toLowerCase() : "NOT_A_CHANNEL_NAME"
if (config.activeChannels.includes(channelName) || msg.channel.recipient) {
if (!msg.content.trim().startsWith(config.botkey) || msg.author.bot) return
// Normalize input
let parts = msg.content.trim().toLowerCase().substring(1).split(/\s+/)
let cmd = parts[0]
let input = parts[1] ? parts.slice(1).join(' ') : '' //Some cmds have no input, this lets us use if(input)
let execTime = new Date(Date.now()).toLocaleString();
// If we have the requested op, send it - otherwise, log it quietly
if (cmd in commander) {
console.log(execTime + ': running ' + cmd + '(' + input + ') for ' + msg.author.username)
// Works for a string or a promise return. Sick. https://stackoverflow.com/a/27760489
Promise.resolve( commander[cmd](input, msg, client) )
.then(function(result) {
msg.reply(result)
})
.catch(function(err) {
msg.reply(`your command met with a terrible fate and I nearly died. Have an admin check the logs plz`)
console.log(`${execTime}: ERR: ${err}`)
})
} else if (cmd == 'help') {
let fullHelp = `these are my powers:`
// Each library is a string
for (library in metadata){ // Already overloaded command, oops
fullHelp += `\n**${metadata[library].helptext}**: \n` // Set in each lib's index.js, saved at :17
// meta[lib] is a list of ops in that lib
for (var opName of metadata[library]) {
if ((opName) != 'helptext')
fullHelp += `${opName}\n`
}
}
fullHelp += `\nFor any command, run '${config.botkey}command help' for detailed use info. `
fullHelp += `If you notice something weird or broken, run **${config.botkey}feedback** for support info`
msg.channel.send(fullHelp)
} else {
console.log(`${execTime}: NOTICE: can't find ${cmd}(${input}) for ${msg.author.username}`)
}
}
});
// Turning the key and revving the bot engine
client.login(token)