Skip to content

Commit

Permalink
Defer to discord for disabling at-everyone in webhooks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Throne3d committed Aug 1, 2019
1 parent ef553d3 commit f6d0910
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,18 @@ class Bot {
const webhook = this.findWebhook(channel);
if (webhook) {
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`);
const permissions = discordChannel.permissionsFor(this.discord.user);
let canPingEveryone = false;
if (permissions) {
canPingEveryone = permissions.has(discord.Permissions.FLAGS.MENTION_EVERYONE);
}
const avatarURL = this.getDiscordAvatar(author, channel);
const username = _.padEnd(author.substring(0, USERNAME_MAX_LENGTH), USERNAME_MIN_LENGTH, '_');
webhook.client.sendMessage(withMentions, {
username,
text,
avatarURL
avatarURL,
disableEveryone: !canPingEveryone,
}).catch(logger.error);
return;
}
Expand Down
43 changes: 43 additions & 0 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ describe('Bot', function () {
username: 'n_',
text,
avatarURL: null,
disableEveryone: true,
});
});

Expand All @@ -999,6 +1000,48 @@ describe('Bot', function () {
username: '12345678901234567890123456789012',
text,
avatarURL: null,
disableEveryone: true,
});
});

it('does not ping everyone if user lacks permission', function () {
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
const bot = new Bot(newConfig);
const text = 'message';
const permission = discord.Permissions.FLAGS.VIEW_CHANNEL
+ discord.Permissions.FLAGS.SEND_MESSAGES;
bot.discord.channels.get('1234').setPermissionStub(
bot.discord.user,
new discord.Permissions(permission),
);
bot.connect();
bot.sendToDiscord('nick', '#irc', text);
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
username: 'nick',
text,
avatarURL: null,
disableEveryone: true,
});
});

it('sends @everyone messages if the bot has permission to do so', function () {
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
const bot = new Bot(newConfig);
const text = 'message';
const permission = discord.Permissions.FLAGS.VIEW_CHANNEL
+ discord.Permissions.FLAGS.SEND_MESSAGES
+ discord.Permissions.FLAGS.MENTION_EVERYONE;
bot.discord.channels.get('1234').setPermissionStub(
bot.discord.user,
new discord.Permissions(permission),
);
bot.connect();
bot.sendToDiscord('nick', '#irc', text);
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
username: 'nick',
text,
avatarURL: null,
disableEveryone: false,
});
});

Expand Down
3 changes: 3 additions & 0 deletions test/stubs/discord-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default function createDiscordStub(sendStub, discordUsers) {
}, textChannel);
const textChannelObj = new discord.TextChannel(guild, textChannelData);
textChannelObj.send = sendStub;
const permissions = new discord.Collection();
textChannelObj.setPermissionStub = (user, perms) => permissions.set(user, perms);
textChannelObj.permissionsFor = user => permissions.get(user);
this.channels.set(textChannelObj.id, textChannelObj);
return textChannelObj;
}
Expand Down

0 comments on commit f6d0910

Please sign in to comment.