-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullhorn.js
115 lines (101 loc) · 4.26 KB
/
Bullhorn.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
/**
CONFIG FILE DEFINITIONS:
{
"token": "string", // the bot token from discord's developer page
"id": "UserId", // The bot's user id
"watchChannel": "", //
"DaveID": "UserId", // ID of Dave, the author
"watchUser": "UserId", // Id of the user to re-broadcast to others
"broadcastTo": ["ChanID", // ID of additional channels to add to the broadcast list
"ChanID",
"ChanID"]
}
*/
let ME;
let config = {};
if (process.argv[2]) {
let configfile = './' + process.argv[2];
config = require(configfile);
} else {
console.error("No config file given. start with node BullHorn.js configFileName");
}
let broadcastToChannels = [];
// Set up discord.js client
const Discord = require('discord.js');
const client = new Discord.Client({
autoReconnect: true
});
//Message Recieved
client.on('message', message => {
// logger shouldn't check it's own stuff)
if (message.author.id != client.user.id) {
if (message.author.id == config.watchUser && message.channel.type == "dm") {
console.log(`Sending message out: ${message.content}`);
broadcastToChannels.forEach((chan) => {
chan.send(message.content)
.then(message => console.log(`Sent message to ${message.channel.guild.name}#${message.channel.name}`))
.catch(console.error);
});
}
}
});
// Connected!
client.on('ready', () => {
console.log('Bullhorn is ready!');
ME = client.user;
ME.setActivity("Shawn's Minion");
// Add channels from config file
config.broadcastTo.forEach((id) => {
let chan = client.channels.get(id);
if (chan) {
//Check if it already exists in the list
if (broadcastToChannels.includes(chan)) {
console.log(`Server:${chan.guild.name}\tChannel already listed. Skipping`);
} else {
console.log(`Server:${chan.guild.name}\tChannel:${chan.name} found from config file!`);
if (chan.permissionsFor(ME).has("SEND_MESSAGES")) {
console.log(`Server:${chan.guild.name}\tI can send to that channel! Added to broadcast list`);
broadcastToChannels.push(chan)
} else {
console.error(`Server:${chan.guild.name}\tI can't send to that channel! I need permission to SEND_MESSAGES.`);
}
}
} else {
console.error(`Server:UNKNOWN\tCan't find any channel with ID ${id}.`);
}
})
// get list of guilds (servers) and see if there is a channel named "#announcements"
client.guilds.forEach((guild) => {
let FindAnnouncementsPromise = new Promise((result) => { result(guild.channels.find((chan) => chan.name.toLowerCase() == "announcements")) });
FindAnnouncementsPromise.then((chan) => {
if (chan) {
//Check if it already exists in the list
if (config.broadcastTo.includes(chan.id)) {
console.log(`Server:${guild.name}\tAnnouncement channel already listed in config. Skipping`);
} else {
console.log(`Server:${guild.name}\tAnnouncement channel found!`);
if (chan.permissionsFor(ME).has("SEND_MESSAGES")) {
console.log(`Server:${guild.name}\tI can send to that channel! Added to broadcast list`);
broadcastToChannels.push(chan);
} else {
console.error(`Server:${guild.name}\tI can't send to that channel! I need permission to SEND_MESSAGES.`);
}
}
} else {
console.error(`Server:${guild.name}\thas no "announcements" channel.`);
}
}).catch((e) => console.error(e));
})
// Find Shawn and obey his every whim.
client.fetchUser(config.watchUser).then((Shawn) => {
Shawn.createDM().then((dm) => {
dm.send("Hey there, Oh Great and Powerful One. I'm online. Please give me a minute and I should be ready to go.").then();
ShawnDM = dm;
})
}).catch((e) => console.error(e));
});
// connect
console.log("Logging in!");
client.login(config.token)
.then()
.catch((e) => console.error(e));