Skip to content

Commit 677aa00

Browse files
committed
Update refresh command
1 parent 1173f50 commit 677aa00

File tree

1 file changed

+61
-43
lines changed

1 file changed

+61
-43
lines changed

plugins/bdo_boss_tracker/plugin.js

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,52 +82,70 @@ class BDOBossTrackerPlugin extends BasePlugin {
8282
console.log("Found " + this.GUILD_BOSS_CALLOUTS_CHANNELS.length + " callout channels");
8383
}
8484

85+
fetchLatestTimer() {
86+
// There is at most 1 message on the timer channel
87+
this.IHAU_TIMER_CHANNEL.fetchMessages()
88+
.then(messages => {
89+
if (messages.size > 0) {
90+
const new_timer = messages.first().content;
91+
this.queueTimerPageRefresh(new_timer);
92+
}
93+
})
94+
.catch(console.error);
95+
}
96+
97+
fetchLatestCallout() {
98+
// The latest callout should be the latest post by the IHA Bot
99+
this.IHAU_UPDATE_CHANNEL.fetchMessages()
100+
.then(messages => {
101+
const filteredMessages = messages.filter(message => message.author.id === this.IHAU_BOT_ID);
102+
if (filteredMessages) {
103+
const new_callout = filteredMessages.first().content;
104+
this.queueLivePageRefresh(new_callout);
105+
}
106+
})
107+
.catch(console.error);
108+
}
109+
85110
initListener() {
86111
console.log("Initializing Listeners");
87112
this.fetchChannels();
88113
this.LISTENER_CLIENT.on('ready', () => {
89114
console.log(this.LISTENER_CLIENT.user.username + " user is ready");
90115
this.IHAU_UPDATE_CHANNEL = this.LISTENER_CLIENT.channels.find('id', IHAU_BOSS_LIVE_CHANNEL_ID);
91-
const ihauTimerChannel = this.LISTENER_CLIENT.channels.find('id', IHAU_BOSS_TIMER_CHANNEL_ID);
92-
93-
ihauTimerChannel.fetchMessages()
94-
.then(messages => {
95-
if (messages.size > 0) {
96-
const new_timer = messages.first().content;
97-
this.queueTimerPageRefresh(new_timer);
98-
}
99-
})
100-
.catch(console.log);
101-
});
102-
103-
this.LISTENER_CLIENT.on('message', message => {
104-
// Listen for boss timer changes
105-
const guild = message.guild;
106-
const author = message.author;
107-
const channel = message.channel;
108-
109-
if (channel.type == "text" && guild.available && guild.id == IHAU_GUILD_ID && author.id == IHAU_BOT_ID) {
110-
// Update from IHAU's bot
111-
if (channel.id == IHAU_BOSS_TIMER_CHANNEL_ID) {
112-
// Boss Timer update
113-
this.queueTimerPageRefresh(message.content);
114-
} else if (channel.id == IHAU_BOSS_LIVE_CHANNEL_ID) {
115-
// Live updates
116-
this.queueLivePageRefresh(message.content);
117-
}
118-
} else if (author.id != this.LISTENER_CLIENT.user.id && channel.type == "dm") {
119-
// Auto-respond
120-
channel.sendMessage("You've caught me! I am actually a bot. For more information please message @rouganstriker#5241")
116+
this.IHAU_TIMER_CHANNEL = this.LISTENER_CLIENT.channels.find('id', IHAU_BOSS_TIMER_CHANNEL_ID);
117+
118+
this.fetchLatestTimer();
119+
});
120+
121+
this.LISTENER_CLIENT.on('message', message => {
122+
// Listen for boss timer changes
123+
const guild = message.guild;
124+
const author = message.author;
125+
const channel = message.channel;
126+
127+
if (channel.type == "text" && guild.available && guild.id == IHAU_GUILD_ID && author.id == IHAU_BOT_ID) {
128+
// Update from IHAU's bot
129+
if (channel.id == IHAU_BOSS_TIMER_CHANNEL_ID) {
130+
// Boss Timer update
131+
this.queueTimerPageRefresh(message.content);
132+
} else if (channel.id == IHAU_BOSS_LIVE_CHANNEL_ID) {
133+
// Live updates
134+
this.queueLivePageRefresh(message.content);
121135
}
122-
});
136+
} else if (author.id != this.LISTENER_CLIENT.user.id && channel.type == "dm") {
137+
// Auto-respond
138+
channel.sendMessage("You've caught me! I am actually a bot. For more information please message @rouganstriker#5241")
139+
}
140+
});
123141

124-
this.LISTENER_CLIENT.on('reconnecting', () => {
125-
console.warn("Attempting to reconnect...");
126-
});
142+
this.LISTENER_CLIENT.on('reconnecting', () => {
143+
console.warn("Attempting to reconnect...");
144+
});
127145

128-
this.LISTENER_CLIENT.on('error', (error) => {
129-
console.error(error);
130-
});
146+
this.LISTENER_CLIENT.on('error', (error) => {
147+
console.error(error);
148+
});
131149

132150
this.LISTENER_CLIENT.login(process.env.BDO_BOSS_TRACKER_LISTENER_TOKEN);
133151
}
@@ -168,7 +186,7 @@ class BDOBossTrackerPlugin extends BasePlugin {
168186
};
169187
const handleError = (error) => {
170188
this.timerUpdateLock.unlock();
171-
console.log(error);
189+
console.error(error);
172190
}
173191

174192
channel.fetchMessages()
@@ -219,7 +237,7 @@ class BDOBossTrackerPlugin extends BasePlugin {
219237
};
220238
const handleError = (error) => {
221239
this.liveUpdateLock.unlock();
222-
console.log(error);
240+
console.error(error);
223241
}
224242

225243
channel.fetchMessages({limit: 100})
@@ -298,21 +316,21 @@ class BDOBossTrackerPlugin extends BasePlugin {
298316
initCommands() {
299317
/*
300318
* BDO boss tracker related commands
301-
* - /refreshBossTimer
302-
* - /refreshBossCallouts
319+
* - !refreshBossTimer
320+
* - !refreshBossCallouts
303321
*/
304322
this.commands = [];
305323

306324
this.commands.push(new Command(
307325
'refreshBossTimer',
308326
'Refresh the boss timer',
309-
() => { this.queueTimerPageRefresh(this.lastTimerUpdate); }
327+
this.fetchLatestTimer.bind(this)
310328
));
311329

312330
this.commands.push(new Command(
313331
'refreshBossCallouts',
314332
'Refresh the boss callouts',
315-
() => { this.queueLivePageRefresh(this.lastLiveUpdate); }
333+
this.fetchLatestCallout.bind(this)
316334
));
317335
}
318336

0 commit comments

Comments
 (0)