This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
botHandler.js
87 lines (73 loc) · 2.64 KB
/
botHandler.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
const Discord = require("discord.js")
const config = require("./config.json")
const botCommands = require('./commands')
const charImporter = require('./helper/charImporter')
const charExporter = require('./helper/charExporter')
const cache = require('./helper/cache')
const initController = require('./helper/initController')
const bot = new Discord.Client()
async function getChannelById(channelid) {
let channel = bot.channels.cache.get(channelid)
if (!channel) {
channel = await bot.channels.fetch(channelid)
}
return channel;
}
module.exports = {
init: function() {
config.token = process.env.BOT_SECRET
bot.commands = new Discord.Collection()
bot.login(config.token)
Object.keys(botCommands).map(key => {
bot.commands.set(botCommands[key].name, botCommands[key])
});
bot.on("ready", async () => {
bot.user.setActivity(config.prefix + 'hilfe', { type: 'WATCHING' })
await cache.load()
console.log("bot ready")
})
bot.on('message', msg => {
try {
// import
if (msg.channel.type === 'dm' && msg.attachments.size > 0) {
charImporter.import(msg)
}
const args = msg.content.split(/[ |\,]+/)
const content = args.shift().toLowerCase()
if (!content.startsWith(config.prefix)) return
const command = content.slice(config.prefix.length) // remove the prelimiter
if (!bot.commands.has(command)) {
return
}
bot.commands.get(command).execute(msg, args)
} catch (error) {
console.error(error)
msg.reply('there was an error trying to execute that command!')
}
})
bot.on('messageReactionAdd', initController.onReaction)
},
sendDM: async function (userid, message) {
let user = bot.users.cache.get(userid)
if (!user) {
user = await bot.users.fetch(userid)
}
user.send(message)
},
getChannel: async function (userid) {
const channelid = await cache.getChannel(userid)
if (channelid) {
return getChannelById(channelid)
} else {
throw new Error("no channel set")
}
},
sendChannelMessage: async function (channelid, message) {
const channel = await getChannelById(channelid)
channel.send(message)
},
getCharacter: function (userid) {
const character = cache.getCharacter(userid)
return charExporter.export(character)
}
}