From 2ab69143eb09775abfe081c63cd77e83016beb61 Mon Sep 17 00:00:00 2001 From: Mittelblut9 Date: Wed, 8 Nov 2023 14:57:53 +0100 Subject: [PATCH 1/2] fix: remove permissions when the bot has no permissions in the channel --- utils/classes/Notifications/Notifications.js | 24 ++++++++++++++++++- .../Notifications/Twitch/TwitchLogic.js | 2 +- .../Twitch/TwitchNotification.js | 5 +++- .../YouTube/YouTubeNotification.js | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/utils/classes/Notifications/Notifications.js b/utils/classes/Notifications/Notifications.js index e42260465..2b8a168ed 100644 --- a/utils/classes/Notifications/Notifications.js +++ b/utils/classes/Notifications/Notifications.js @@ -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.'); @@ -18,6 +27,19 @@ module.exports = class Notification { 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') { + new TwitchNotifier().delete(channel.guild.id, channel_id); + } + } return reject(err); } }); diff --git a/utils/classes/Notifications/Twitch/TwitchLogic.js b/utils/classes/Notifications/Twitch/TwitchLogic.js index 15e47d88b..f95f66c94 100644 --- a/utils/classes/Notifications/Twitch/TwitchLogic.js +++ b/utils/classes/Notifications/Twitch/TwitchLogic.js @@ -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)); }); }); diff --git a/utils/classes/Notifications/Twitch/TwitchNotification.js b/utils/classes/Notifications/Twitch/TwitchNotification.js index 3016bcd80..e811f3555 100644 --- a/utils/classes/Notifications/Twitch/TwitchNotification.js +++ b/utils/classes/Notifications/Twitch/TwitchNotification.js @@ -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)) { @@ -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({ @@ -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}...`); diff --git a/utils/classes/Notifications/YouTube/YouTubeNotification.js b/utils/classes/Notifications/YouTube/YouTubeNotification.js index 069766c9c..3dfc74a05 100644 --- a/utils/classes/Notifications/YouTube/YouTubeNotification.js +++ b/utils/classes/Notifications/YouTube/YouTubeNotification.js @@ -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; From 53f7d58b29e679151121c873fd64cce39dee8ad7 Mon Sep 17 00:00:00 2001 From: Mittelblut9 Date: Wed, 8 Nov 2023 15:01:32 +0100 Subject: [PATCH 2/2] fix: add missing reject function --- utils/classes/Notifications/Notifications.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/classes/Notifications/Notifications.js b/utils/classes/Notifications/Notifications.js index 2b8a168ed..9c94363fa 100644 --- a/utils/classes/Notifications/Notifications.js +++ b/utils/classes/Notifications/Notifications.js @@ -38,6 +38,9 @@ module.exports = class Notification { ); } else if (type === 'twitch') { 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);