forked from rb28z2/progress-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.js
87 lines (73 loc) · 2.5 KB
/
discord.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
// eslint-disable-next-line no-unused-vars
import colors from "colors";
import Discord from "discord.js";
const client = new Discord.Client();
import config from "./config.js";
import * as common from "./common.js";
export function initDiscord() {
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`.yellow);
});
let lastUpdated = exports.lastUpdated;
client.on("message", msg => {
let authenticated = config.discordListenChannels.includes(msg.channel.id) ||
(msg.channel.parent &&
config.discordListenCategories.includes(msg.channel.parent.id));
if (common.triggerMatch(msg.content) && authenticated) {
common.runCommand(msg.content, {
service: "discord",
reply: m => msg.channel.send(discordify(m)),
message: msg
});
}
});
client.on("error", error => {
console.log(error);
});
client.login(config.discordKey);
}
export function discordSay(message) {
message = discordify(message);
config.discordNotifyChannels.forEach( async value => {
let channel = await client.channels.fetch(value);
await channel.send(message);
});
}
function discordify(message) {
return message.replace(/<\/?b>/g, "**").replace(/<\/?i>/g, "*").replace(/<\/?s>/g, "~~");
}
export function addDiscordTracker(show, source, topic = false) {
if (!("discordTrackers" in common.stats.shows[show])) {
common.stats.shows[show].discordTrackers = {};
}
if (topic) {
source.message.channel.setTopic(discordify(common.getEpisodeStatus(show)))
.then(channel => {
common.stats.shows[show].discordTrackers[channel.id] = "topic";
}).catch(error => source.reply("Failed to set channel topic: " + error.message))
} else {
source.reply(common.getEpisodeStatus(show))
.then(msg => {
common.stats.shows[show].discordTrackers[msg.channel.id] = msg.id;
}).catch(error => source.reply("Failed to send message: " + error.message));
}
}
export function clearDiscordTracker(show, source) {
if ("discordTrackers" in common.stats.shows[show]) {
delete common.stats.shows[show].discordTrackers[source.message.channel.id];
}
}
export function updateDiscordTrackers(show) {
let status = discordify(common.getEpisodeStatus(show));
if ("discordTrackers" in common.stats.shows[show]) {
for (const [channelId, msgId] of Object.entries(common.stats.shows[show].discordTrackers)) {
client.channels.fetch(channelId).then(channel => {
if (msgId === "topic") {
channel.setTopic(status)
} else {
channel.messages.fetch(msgId).then(msg => msg.edit(status));
}
})
}
}
}