Skip to content

Commit b5c5537

Browse files
committed
Add a fail-safe timeout
1 parent beb0ce7 commit b5c5537

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

lib/commands/implementations/gulag.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const CommandOutput = require('../command-output');
44
const { makeBan } = require('../../chat-utils/punishment-helpers');
55

66
function gulag(defaultBanTime) {
7+
const pollDuration = 30000;
8+
9+
let failSafeTimeout = null;
710
let gulagActive = false;
811

912
return (input, services, rawMessage) => {
@@ -43,6 +46,7 @@ function gulag(defaultBanTime) {
4346
listener.on('err', (message) => {
4447
const error = JSON.parse(message);
4548
if (error.description === 'activepoll' && !gulagActive) {
49+
clearTimeout(failSafeTimeout);
4650
services.messageRelay.stopRelay('gulag');
4751
services.messageRelay.sendOutputMessage(
4852
'Poll in progress. Please wait for the current poll to finish.',
@@ -57,14 +61,19 @@ function gulag(defaultBanTime) {
5761
' vs ',
5862
)}`,
5963
);
64+
65+
// Fail-safe
66+
failSafeTimeout = setTimeout(() => {
67+
gulagActive = false;
68+
services.messageRelay.stopRelay('gulag');
69+
}, pollDuration + 5000);
6070
});
6171

6272
listener.on('pollstop', (message) => {
73+
clearTimeout(failSafeTimeout);
6374
gulagActive = false;
6475
services.messageRelay.stopRelay('gulag');
6576

66-
if (message === null) return; // pollstop is null if the bot loses connection to chat.
67-
6877
services.messageRelay.sendOutputMessage('GULAG has ended:');
6978

7079
const poll = JSON.parse(message);
@@ -100,7 +109,7 @@ function gulag(defaultBanTime) {
100109
'poll',
101110
JSON.stringify({
102111
weighted: false,
103-
time: 30000,
112+
time: pollDuration,
104113
question: `ENTER THE GULAG. Chatters battling it out to not get a ${muteString} ban. Vote KEEP not KICK!?`,
105114
options: users,
106115
}),

lib/commands/implementations/voteban.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const basePunishmentHelper = require('../base-punishment-helper');
77
const formatDuration = require('../../chat-utils/format-duration');
88

99
function voteBan(ipBan, defaultBanTime, weighted) {
10+
const pollDuration = 30000;
11+
12+
let failSafeTimeout = null;
1013
let votebanActive = false;
1114

1215
return function ban(input, services, rawMessage) {
@@ -39,6 +42,7 @@ function voteBan(ipBan, defaultBanTime, weighted) {
3942
listener.on('err', (message) => {
4043
const error = JSON.parse(message);
4144
if (error.description === 'activepoll' && !votebanActive) {
45+
clearTimeout(failSafeTimeout);
4246
services.messageRelay.stopRelay('voteban');
4347
services.messageRelay.sendOutputMessage(
4448
'Poll in progress. Please wait for the current poll to finish.',
@@ -53,14 +57,19 @@ function voteBan(ipBan, defaultBanTime, weighted) {
5357
muteString === 'PERMANENTLY' ? muteString : `for ${muteString}`
5458
} ${parsedReason ? ` Reason: ${parsedReason}` : ''}?`,
5559
);
60+
61+
// Fail-safe
62+
failSafeTimeout = setTimeout(() => {
63+
votebanActive = false;
64+
services.messageRelay.stopRelay('voteban');
65+
}, pollDuration + 5000);
5666
});
5767

5868
listener.on('pollstop', (message) => {
69+
clearTimeout(failSafeTimeout);
5970
votebanActive = false;
6071
services.messageRelay.stopRelay('voteban');
6172

62-
if (message === null) return; // pollstop is null if the bot loses connection to chat.
63-
6473
services.messageRelay.sendOutputMessage('Total votes:');
6574

6675
const poll = JSON.parse(message);
@@ -93,7 +102,7 @@ function voteBan(ipBan, defaultBanTime, weighted) {
93102
'poll',
94103
JSON.stringify({
95104
weighted,
96-
time: 30000,
105+
time: pollDuration,
97106
question: `Should we ban ${userToPunish} ${
98107
muteString === 'PERMANENTLY' ? muteString : `for ${muteString}`
99108
} ${parsedReason ? ` Reason: ${parsedReason}` : ''}`,

lib/message-routing/chat-service-router.js

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class ChatServiceRouter {
3333
this.bot.on('closed', () => {
3434
this.logger.info('Chat socket closed. Attempting to reconnect....');
3535
if (this.chatToConnectTo === 'dgg') {
36-
this.messageRelay.relayMessageToListeners('pollstop', null);
3736
setTimeout(() => {
3837
this.bot.connect();
3938
}, 5000);

0 commit comments

Comments
 (0)