Make bot check channel permissions before sending messages (#161)

This commit is contained in:
Vylpes 2022-06-05 14:11:01 +01:00 committed by GitHub
parent aa070bb7a7
commit 1403619bda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 146 additions and 142 deletions

View file

@ -1,4 +1,4 @@
import { MessageEmbed } from "discord.js";
import { MessageEmbed, Permissions, TextChannel } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext";
export default class ErrorEmbed extends MessageEmbed {
@ -13,7 +13,16 @@ export default class ErrorEmbed extends MessageEmbed {
this.context = context;
}
public SendToCurrentChannel() {
public async SendToCurrentChannel() {
const channel = this.context.message.channel as TextChannel;
const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! });
if (!botMember) return;
const permissions = channel.permissionsFor(botMember);
if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return;
this.context.message.channel.send({ embeds: [ this ]});
}
}

View file

@ -1,17 +1,20 @@
import { MessageEmbed, TextChannel, User, Guild } from "discord.js";
import { MessageEmbed, TextChannel, User, Guild, Client, Permissions } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
export default class EventEmbed extends MessageEmbed {
public guild: Guild;
constructor(guild: Guild, title: string) {
private client: Client;
constructor(client: Client, guild: Guild, title: string) {
super();
super.setColor(0x3050ba);
super.setTitle(title);
this.guild = guild;
this.client = client;
}
// Detail methods
@ -28,7 +31,7 @@ export default class EventEmbed extends MessageEmbed {
}
// Send methods
public SendToChannel(name: string) {
public async SendToChannel(name: string) {
const channel = this.guild.channels.cache
.find(channel => channel.name == name) as TextChannel;
@ -37,6 +40,14 @@ export default class EventEmbed extends MessageEmbed {
return;
}
const botMember = await this.guild.members.fetch({ user: this.client.user! });
if (!botMember) return;
const permissions = channel.permissionsFor(botMember);
if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return;
channel.send({embeds: [ this ]});
}

View file

@ -1,4 +1,4 @@
import { MessageEmbed, TextChannel, User } from "discord.js";
import { MessageEmbed, Permissions, TextChannel, User } from "discord.js";
import ErrorMessages from "../../constants/ErrorMessages";
import { ICommandContext } from "../../contracts/ICommandContext";
import SettingsHelper from "../SettingsHelper";
@ -30,7 +30,16 @@ export default class LogEmbed extends MessageEmbed {
}
// Send methods
public SendToCurrentChannel() {
public async SendToCurrentChannel() {
const channel = this.context.message.channel as TextChannel;
const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! });
if (!botMember) return;
const permissions = channel.permissionsFor(botMember);
if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return;
this.context.message.channel.send({ embeds: [ this ]});
}

View file

@ -1,4 +1,4 @@
import { MessageEmbed } from "discord.js";
import { MessageEmbed, Permissions, TextChannel } from "discord.js";
import { ICommandContext } from "../../contracts/ICommandContext";
export default class PublicEmbed extends MessageEmbed {
@ -20,7 +20,16 @@ export default class PublicEmbed extends MessageEmbed {
}
// Send methods
public SendToCurrentChannel() {
public async SendToCurrentChannel() {
const channel = this.context.message.channel as TextChannel;
const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! });
if (!botMember) return;
const permissions = channel.permissionsFor(botMember);
if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return;
this.context.message.channel.send({ embeds: [ this ]});
}
}