forked from andresilvasantos/bitprophet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.js
62 lines (50 loc) · 1.23 KB
/
discord_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
var vars = require(__dirname + "/vars.js")
const DiscordBot = require("discord.js")
var chatBot = null
var ready = false
var messageQueue = []
const EventEmitter = require("events")
class Bot extends EventEmitter {
constructor() {
super()
}
init(listenChatIdOnly = false) {
var token = vars.options.discord.token
if(!token) return
chatBot = new DiscordBot.Client()
chatBot.login(token)
if(listenChatIdOnly) {
chatBot.on("message", (message) => {
if(message.author.bot) return
const chatId = message.channel.id
console.log("DISCORD CHAT ID:", chatId)
message.channel.send("CHAT ID: " + chatId)
})
return
}
chatBot.on("ready", () => {
ready = true
for(var i = 0; i < messageQueue.length; ++i) {
this.sendMessage(messageQueue[i])
}
messageQueue = []
})
chatBot.on("message", (message) => {
if(message.author.bot) return
message = message.content.toLowerCase()
this.emit("messageReceived", message)
})
}
sendMessage(message) {
var chatId = vars.options.discord.chatId
if(!chatBot || !chatId) return
if(!ready) {
messageQueue.push(message)
}
else {
var channel = chatBot.channels.get(chatId)
if(channel) channel.send(message)
}
}
}
module.exports = Bot