Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove permissions when the bot has no permissions in the channel #1774

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion utils/classes/Notifications/Notifications.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const { Message } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const YouTubeSettings = require('./YouTube/YouTubeSettings');
const TwitchNotifier = require('./Twitch/TwitchLogic');

module.exports = class Notification {
constructor() {}

async sendNotification({ channel, content = null, embed = null, components = null }) {
async sendNotification({
channel,
content = null,
embed = null,
components = null,
channel_id = null,
type = '',
}) {
return new Promise(async (resolve, reject) => {
if (!channel) reject('No channel provided.');
if (!content && !embed) reject('No content or embed provided.');
Expand All @@ -18,6 +27,22 @@
const msg = await channel.send(options);
return resolve(msg);
} catch (err) {
if (channel_id && err.status === 403) {
if (type === 'yt') {
new YouTubeSettings(channel.guild.id).remove({
guild_id: channel.guild.id,
channel_id: channel_id,
});
return reject(
'I do not have permission to send messages in that channel. I have removed the channel from the YouTube settings.'
);
} else if (type === 'twitch') {

Check notice on line 39 in utils/classes/Notifications/Notifications.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

utils/classes/Notifications/Notifications.js#L39

Add the missing "else" clause.
new TwitchNotifier().delete(channel.guild.id, channel_id);
return reject(
'I do not have permission to send messages in that channel. I have removed the channel from the Twitch settings.'
);
}
}
return reject(err);
}
});
Expand Down
2 changes: 1 addition & 1 deletion utils/classes/Notifications/Twitch/TwitchLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ module.exports = class TwitchNotifier {
.then(() => {
resolve(global.t.trans(['success.notifications.twitch.removed'], guild_id));
})
.catch((err) => {
.catch(() => {
reject(global.t.trans(['error.notifications.twitch.removeChannel'], guild_id));
});
});
Expand Down
5 changes: 4 additions & 1 deletion utils/classes/Notifications/Twitch/TwitchNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module.exports = class TwitchNotification extends TwitchNotifier {
message = await this.sendTwitchNotification(messageContent, embed, {
dc_channel: dcChannel,
link: `https://twitch.tv/${stream.userDisplayName}`,
twitch_id: data.twitch_id,
});
}
if (!(message instanceof Message)) {
Expand Down Expand Up @@ -248,7 +249,7 @@ module.exports = class TwitchNotification extends TwitchNotifier {
});
}

sendTwitchNotification(content, embed, { dc_channel, link }) {
sendTwitchNotification(content, embed, { dc_channel, link, twitch_id }) {
return new Promise(async (resolve, reject) => {
this.notificationApi
.sendNotification({
Expand All @@ -264,6 +265,8 @@ module.exports = class TwitchNotification extends TwitchNotifier {
.setEmoji('🔴')
),
],
channel_id: twitch_id,
type: 'twitch',
})
.then((message) => {
console.info(`🔎 Twitch stream handler checked streamer: ${link}...`);
Expand Down
2 changes: 2 additions & 0 deletions utils/classes/Notifications/YouTube/YouTubeNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ module.exports = class YouTubeNotification extends YouTubeLogic {
channel,
content: embedContent,
embed: embed,
channel_id: upload.channel_id,
type: 'yt',
});

if (!message || !message.id) continue;
Expand Down
Loading