proper perms checks for selfrole setting

This commit is contained in:
Erik 2023-12-30 22:43:01 +02:00
parent e905d766f8
commit 861169f083

View File

@ -1,4 +1,4 @@
import { CommandError, Setting } from '../../../interfaces/index.js'; import { Setting } from '../../../interfaces/index.js';
import DiscordClient from '../../../DiscordClient.js'; import DiscordClient from '../../../DiscordClient.js';
import InvokerWrapper from '../../wrappers/InvokerWrapper.js'; import InvokerWrapper from '../../wrappers/InvokerWrapper.js';
import { CommandOptionType, CommandParams } from '../../../../../@types/Client.js'; import { CommandOptionType, CommandParams } from '../../../../../@types/Client.js';
@ -9,7 +9,6 @@ import Emojis from '../../../../constants/Emojis.js';
class SelfroleSetting extends Setting class SelfroleSetting extends Setting
{ {
constructor (client: DiscordClient) constructor (client: DiscordClient)
{ {
super(client, { super(client, {
@ -77,8 +76,9 @@ class SelfroleSetting extends Setting
// old channel for deleting old message if one exists // old channel for deleting old message if one exists
const oldChannel = await guild.resolveChannel<TextChannel>(setting.channel); const oldChannel = await guild.resolveChannel<TextChannel>(setting.channel);
const newChannel = channel?.asChannel || oldChannel; const newChannel = channel?.asChannel || oldChannel;
if (!newChannel?.isTextBased()) if (newChannel && !newChannel.isTextBased())
throw new CommandError(invoker, { index: 'ERR_INVALID_CHANNEL_TYPE' }); // throw new CommandError(invoker, { index: 'ERR_INVALID_CHANNEL_TYPE' });
return { error: true, index: 'ERR_INVALID_CHANNEL_TYPE' };
if (channel) if (channel)
setting.channel = channel.asChannel.id; // Set the new channel if one is given setting.channel = channel.asChannel.id; // Set the new channel if one is given
if (text) if (text)
@ -95,11 +95,15 @@ class SelfroleSetting extends Setting
} }
} }
if (setting.roles.length && setting.channel && setting.roles.length <= 25) if (newChannel && setting.roles.length && setting.channel && setting.roles.length <= 25)
{ {
const perms = newChannel.permissionsFor(this.client.user!);
const missing = perms?.missing([ 'SendMessages', 'ViewChannel' ]) ?? [];
if (missing.length)
return { error: true, index: 'ERR_CHANNEL_PERMS', params: { perms: missing.join('`, `'), channel: newChannel.name } };
const resolvedRoles = await guild.resolveRoles(setting.roles); const resolvedRoles = await guild.resolveRoles(setting.roles);
const selectMenu: StringSelectMenuComponentData = { const selectMenu: StringSelectMenuComponentData = {
type: ComponentType.SelectMenu, type: ComponentType.StringSelect,
customId: 'selfrole-select', customId: 'selfrole-select',
maxValues: resolvedRoles.length, maxValues: resolvedRoles.length,
options: resolvedRoles.map((r) => options: resolvedRoles.map((r) =>
@ -137,13 +141,10 @@ class SelfroleSetting extends Setting
const msg = await newChannel.send(payload); const msg = await newChannel.send(payload);
setting.message = msg.id; setting.message = msg.id;
} }
} }
return { index: 'SETTING_SUCCESS_ALT' }; return { index: 'SETTING_SUCCESS_ALT' };
} }
} }
export default SelfroleSetting; export default SelfroleSetting;