-
Notifications
You must be signed in to change notification settings - Fork 33
/
discordbot.js
72 lines (62 loc) · 2.55 KB
/
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
const Discord = require('discord.js');
const SWG = require('./swgclient');
const config = require('./config');
SWG.login(config.SWG);
var client, server, notif, chat, notifRole;
function discordBot() {
client = new Discord.Client();
client.on('message', message => {
if (message.content.startsWith('!server')) {
message.reply(SWG.isConnected ? "The server is UP!" : "The server is DOWN :(");
}
if (message.content.startsWith('!fixchat')) {
message.reply("rebooting chat bot");
process.exit(0);
}
if (message.content.startsWith('!pausechat')) {
message.reply(SWG.paused ? "unpausing" : "pausing");
SWG.paused = !SWG.paused;
}
if (message.channel.name != config.Discord.ChatChannel) return;
if (message.author.username == config.Discord.BotName) return;
SWG.sendChat(message.cleanContent, server.members.get(message.author.id).displayName);
});
client.on('disconnect', event => {
try {notif.send("RoC-Bot disconnected");}catch(ex){}
client = server = notif = chat = notifRole = null;
console.log("Discord disconnect: " + JSON.stringify(event,null,2));
setTimeout(discordBot, 1000);
});
client.login(config.Discord.BotToken)
.then(t => {
client.user.setPresence({ status: "online", game: {name: "Progor-Chat"}});
server = client.guilds.find("name", config.Discord.ServerName);
notif = server.channels.find("name", config.Discord.NotificationChannel);
chat = server.channels.find("name", config.Discord.ChatChannel);
notifRole = server.roles.find("name", config.Discord.NotificationMentionRole);
})
.catch(reason => {
console.log(reason);
setTimeout(discordBot, 1000);
});
}
discordBot();
SWG.serverDown = function() {
if (notif) notif.send(notifRole + " server DOWN");
}
SWG.serverUp = function() {
if (notif) notif.send(notifRole + " server UP!");
}
SWG.reconnected = function() {
if (chat) chat.send("chat bot reconnected");
}
SWG.recvChat = function(message, player) {
console.log("sending chat to discord " + player + ": " + message);
if (chat) chat.send("**" + player + ":** " + message);
else console.log("discord disconnected");
}
SWG.recvTell = function(from, message) {
console.log("received tell from: " + from + ": " + message);
if (from != config.SWG.Character) SWG.sendTell(from, "Hi!");
}
setInterval(() => SWG.sendTell(config.SWG.Character, "ping"), 30000);