Skip to content

Commit 3b362d8

Browse files
authored
Defer to discord for disabling at-everyone in webhooks (#497)
Discord already has permissions for whether the bot can ping everyone, and the webhook send method has a `disableEveryone` option, so let's defer to the inbuilt Discord permission for this. When permissions can't be found (shouldn't happen), defaults to disabling the pings. Fixes #494
1 parent 2b4bcb1 commit 3b362d8

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/bot.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,18 @@ class Bot {
555555
const webhook = this.findWebhook(channel);
556556
if (webhook) {
557557
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`);
558+
const permissions = discordChannel.permissionsFor(this.discord.user);
559+
let canPingEveryone = false;
560+
if (permissions) {
561+
canPingEveryone = permissions.has(discord.Permissions.FLAGS.MENTION_EVERYONE);
562+
}
558563
const avatarURL = this.getDiscordAvatar(author, channel);
559564
const username = _.padEnd(author.substring(0, USERNAME_MAX_LENGTH), USERNAME_MIN_LENGTH, '_');
560565
webhook.client.sendMessage(withMentions, {
561566
username,
562567
text,
563-
avatarURL
568+
avatarURL,
569+
disableEveryone: !canPingEveryone,
564570
}).catch(logger.error);
565571
return;
566572
}

test/bot.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ describe('Bot', function () {
986986
username: 'n_',
987987
text,
988988
avatarURL: null,
989+
disableEveryone: true,
989990
});
990991
});
991992

@@ -999,6 +1000,48 @@ describe('Bot', function () {
9991000
username: '12345678901234567890123456789012',
10001001
text,
10011002
avatarURL: null,
1003+
disableEveryone: true,
1004+
});
1005+
});
1006+
1007+
it('does not ping everyone if user lacks permission', function () {
1008+
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
1009+
const bot = new Bot(newConfig);
1010+
const text = 'message';
1011+
const permission = discord.Permissions.FLAGS.VIEW_CHANNEL
1012+
+ discord.Permissions.FLAGS.SEND_MESSAGES;
1013+
bot.discord.channels.get('1234').setPermissionStub(
1014+
bot.discord.user,
1015+
new discord.Permissions(permission),
1016+
);
1017+
bot.connect();
1018+
bot.sendToDiscord('nick', '#irc', text);
1019+
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
1020+
username: 'nick',
1021+
text,
1022+
avatarURL: null,
1023+
disableEveryone: true,
1024+
});
1025+
});
1026+
1027+
it('sends @everyone messages if the bot has permission to do so', function () {
1028+
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
1029+
const bot = new Bot(newConfig);
1030+
const text = 'message';
1031+
const permission = discord.Permissions.FLAGS.VIEW_CHANNEL
1032+
+ discord.Permissions.FLAGS.SEND_MESSAGES
1033+
+ discord.Permissions.FLAGS.MENTION_EVERYONE;
1034+
bot.discord.channels.get('1234').setPermissionStub(
1035+
bot.discord.user,
1036+
new discord.Permissions(permission),
1037+
);
1038+
bot.connect();
1039+
bot.sendToDiscord('nick', '#irc', text);
1040+
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
1041+
username: 'nick',
1042+
text,
1043+
avatarURL: null,
1044+
disableEveryone: false,
10021045
});
10031046
});
10041047

test/stubs/discord-stub.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default function createDiscordStub(sendStub, discordUsers) {
2929
}, textChannel);
3030
const textChannelObj = new discord.TextChannel(guild, textChannelData);
3131
textChannelObj.send = sendStub;
32+
const permissions = new discord.Collection();
33+
textChannelObj.setPermissionStub = (user, perms) => permissions.set(user, perms);
34+
textChannelObj.permissionsFor = user => permissions.get(user);
3235
this.channels.set(textChannelObj.id, textChannelObj);
3336
return textChannelObj;
3437
}

0 commit comments

Comments
 (0)