Skip to content

Commit

Permalink
fix: remove permissions when the bot has no permissions in the channel (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mittelblut9 authored Nov 8, 2023
2 parents 55de419 + 53f7d58 commit d4664e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
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 @@ 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(
'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

0 comments on commit d4664e3

Please sign in to comment.