diff --git a/src/bot/commands/checkin/handlers/checkin-audit.ts b/src/bot/commands/checkin/handlers/checkin-audit.ts index d52f49f..5e26862 100644 --- a/src/bot/commands/checkin/handlers/checkin-audit.ts +++ b/src/bot/commands/checkin/handlers/checkin-audit.ts @@ -3,7 +3,7 @@ import { registerCommand } from '@commands/registry' import { AUDIT_FLAME_CHANNEL, FLAMEWARDEN_ROLE } from '@config/discord' import { CHECKIN_AUDIT_ID } from '@events/interaction-create/checkin/handlers/audit-modal' import { createCheckinReviewModal, encodeSnowflake, getCustomId } from '@utils/component' -import { getChannel, getMember, sendReply } from '@utils/discord' +import { getBot, getChannel, getMember, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { SlashCommandBuilder } from 'discord.js' @@ -26,7 +26,9 @@ registerCommand({ throw new CheckinAuditError(CheckinAudit.ERR.NotGuild) const channel = await getChannel(interaction.guild, AUDIT_FLAME_CHANNEL) as TextChannel - CheckinAudit.assertMissPerms(interaction.client.user, channel) + CheckinAudit.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + CheckinAudit.assertMissPerms(bot, channel) const thread = await CheckinAudit.assertThreadUnderChannel(interaction.guild, interaction.channelId, channel) CheckinAudit.assertNotArchivedThread(thread) CheckinAudit.assertNotPrivateThread(thread) diff --git a/src/bot/commands/checkin/handlers/checkin-status.ts b/src/bot/commands/checkin/handlers/checkin-status.ts index 0ea5e03..69fbb00 100644 --- a/src/bot/commands/checkin/handlers/checkin-status.ts +++ b/src/bot/commands/checkin/handlers/checkin-status.ts @@ -1,7 +1,7 @@ import type { ChatInputCommandInteraction, Client, GuildMember } from 'discord.js' import { registerCommand } from '@commands/registry' import { AUDIT_FLAME_CHANNEL, FLAMEWARDEN_ROLE } from '@config/discord' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { SlashCommandBuilder } from 'discord.js' @@ -23,8 +23,10 @@ registerCommand({ if (!interaction.inCachedGuild()) throw new CheckinStatusError(CheckinStatus.ERR.NotGuild) - const channel = await CheckinStatus.assertAllowedChannel(interaction.guild, interaction.channelId, AUDIT_FLAME_CHANNEL) - CheckinStatus.assertMissPerms(interaction.client.user, channel) + const channel = await CheckinStatus.assertAllowedTextChannel(interaction.guild, interaction.channelId, AUDIT_FLAME_CHANNEL) + CheckinStatus.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + CheckinStatus.assertMissPerms(bot, channel) const member = interaction.member as GuildMember CheckinStatus.assertMember(member) diff --git a/src/bot/commands/checkin/handlers/index.ts b/src/bot/commands/checkin/handlers/index.ts index 859949f..8ec785e 100644 --- a/src/bot/commands/checkin/handlers/index.ts +++ b/src/bot/commands/checkin/handlers/index.ts @@ -4,7 +4,7 @@ import { CHECKIN_CHANNEL } from '@config/discord' import { CHECKIN_ID } from '@events/interaction-create/checkin/handlers/modal' import { Checkin } from '@events/interaction-create/checkin/validators' import { encodeSnowflake, getCustomId } from '@utils/component' -import { getAttachments, sendReply } from '@utils/discord' +import { getAttachments, getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { DUMMY } from '@utils/placeholder' @@ -31,8 +31,10 @@ registerCommand({ if (!interaction.inCachedGuild()) throw new CheckinError(Checkin.ERR.NotGuild) - const channel = await Checkin.assertAllowedChannel(interaction.guild, interaction.channelId, CHECKIN_CHANNEL) - Checkin.assertMissPerms(interaction.client.user, channel) + const channel = await Checkin.assertAllowedTextChannel(interaction.guild, interaction.channelId, CHECKIN_CHANNEL) + Checkin.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) const attachments = getAttachments(interaction, Checkin.ATTACHMENT_COUNT) const tempToken = Checkin.setTempItem(attachments) diff --git a/src/bot/commands/embed/handlers/role-grant-create.ts b/src/bot/commands/embed/handlers/role-grant-create.ts index 7e0ef6c..cd14275 100644 --- a/src/bot/commands/embed/handlers/role-grant-create.ts +++ b/src/bot/commands/embed/handlers/role-grant-create.ts @@ -4,7 +4,7 @@ import { LabelBuilder, ModalBuilder, TextInputBuilder } from '@discordjs/builder import { EMBED_ROLE_GRANT_CREATE_MODAL_ID } from '@events/interaction-create/embed/handlers/role-grant-create-modal' import { RoleGrantCreate } from '@events/interaction-create/embed/validators/role-grant-create' import { encodeSnowflake, getCustomId } from '@utils/component' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { DUMMY } from '@utils/placeholder' @@ -30,7 +30,8 @@ registerCommand({ throw new EmbedRoleGrantError(RoleGrantCreate.ERR.NotGuild) const channel = interaction.channel as TextChannel - RoleGrantCreate.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + RoleGrantCreate.assertMissPerms(bot, channel) const buttonName = interaction.options.getString('button-name', true) const role = interaction.options.getRole('role', true) diff --git a/src/bot/commands/message/handlers/send.ts b/src/bot/commands/message/handlers/send.ts index 87d1189..6dbeb40 100644 --- a/src/bot/commands/message/handlers/send.ts +++ b/src/bot/commands/message/handlers/send.ts @@ -3,7 +3,7 @@ import { registerCommand } from '@commands/registry' import { MESSAGE_SEND_ID } from '@events/interaction-create/message/handlers/send-modal' import { Send } from '@events/interaction-create/message/validators/send' import { encodeSnowflake, getCustomId } from '@utils/component' -import { getAttachments, sendReply } from '@utils/discord' +import { getAttachments, getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { DUMMY } from '@utils/placeholder' @@ -37,7 +37,8 @@ registerCommand({ throw new SendError(Send.ERR.NotGuild) const channel = interaction.channel as TextChannel - Send.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Send.assertMissPerms(bot, channel) const attachments = getAttachments(interaction, Send.ATTACHMENT_COUNT) const tempToken = Send.setTempItem(attachments) diff --git a/src/bot/commands/utility/handlers/ping.ts b/src/bot/commands/utility/handlers/ping.ts index 089d94d..c4857d2 100644 --- a/src/bot/commands/utility/handlers/ping.ts +++ b/src/bot/commands/utility/handlers/ping.ts @@ -1,6 +1,6 @@ import type { ChatInputCommandInteraction, TextChannel } from 'discord.js' import { registerCommand } from '@commands/registry' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { log } from '@utils/logger' import { SlashCommandBuilder } from 'discord.js' @@ -23,7 +23,8 @@ registerCommand({ throw new PingError(Ping.ERR.NotGuild) const channel = interaction.channel as TextChannel - Ping.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Ping.assertMissPerms(bot, channel) await sendReply(interaction, 'Pong!') } diff --git a/src/bot/events/client-ready/jobs/handlers/notify-waiting-checkin.ts b/src/bot/events/client-ready/jobs/handlers/notify-waiting-checkin.ts index 98c4642..024d944 100644 --- a/src/bot/events/client-ready/jobs/handlers/notify-waiting-checkin.ts +++ b/src/bot/events/client-ready/jobs/handlers/notify-waiting-checkin.ts @@ -3,7 +3,7 @@ import process from 'node:process' import { WARDEN_DUTY_CHANNEL } from '@config/discord' import { registerClientReadyHandler } from '@events/client-ready/registry' import { EVENT_PATH } from '@events/index' -import { getChannel } from '@utils/discord' +import { getBot, getChannel } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { log } from '@utils/logger' @@ -28,7 +28,9 @@ registerClientReadyHandler({ const guild = await client.guilds.fetch(process.env.GUILD_ID!) const wardenDutyChannel = await getChannel(guild, WARDEN_DUTY_CHANNEL) as TextChannel - NotifyWaitingCheckin.assertChannel(wardenDutyChannel) + NotifyWaitingCheckin.assertTextChannel(wardenDutyChannel) + const bot = await getBot(guild) + NotifyWaitingCheckin.assertMissPerms(bot, wardenDutyChannel) const checkins = await NotifyWaitingCheckin.getTodayWaitingCheckins(client.prisma) await NotifyWaitingCheckin.sendOpening(guild.name, wardenDutyChannel) diff --git a/src/bot/events/client-ready/jobs/handlers/reset-grinder-roles.ts b/src/bot/events/client-ready/jobs/handlers/reset-grinder-roles.ts index cadb4b2..daf9485 100644 --- a/src/bot/events/client-ready/jobs/handlers/reset-grinder-roles.ts +++ b/src/bot/events/client-ready/jobs/handlers/reset-grinder-roles.ts @@ -3,7 +3,7 @@ import process from 'node:process' import { AUDIT_FLAME_CHANNEL, GRIND_ASHES_CHANNEL } from '@config/discord' import { registerClientReadyHandler } from '@events/client-ready/registry' import { EVENT_PATH } from '@events/index' -import { getChannel } from '@utils/discord' +import { getBot, getChannel } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { log } from '@utils/logger' @@ -23,14 +23,17 @@ registerClientReadyHandler({ errorTag: () => `${moduleName}: ${ResetGrinderRoles.ERR.UnexpectedResetGrinderRoles}`, exec(client: Client) { try { - cron.schedule('0 0 * * *', async () => { + cron.schedule('*/1 * * * *', async () => { log.check(ResetGrinderRoles.MSG.JobRunning) const guild = await client.guilds.fetch(process.env.GUILD_ID!) const grindAshesChannel = await getChannel(guild, GRIND_ASHES_CHANNEL) as TextChannel - ResetGrinderRoles.assertChannel(grindAshesChannel) + ResetGrinderRoles.assertTextChannel(grindAshesChannel) const auditFlameChannel = await getChannel(guild, AUDIT_FLAME_CHANNEL) as TextChannel - ResetGrinderRoles.assertChannel(auditFlameChannel) + ResetGrinderRoles.assertTextChannel(auditFlameChannel) + const bot = await getBot(guild) + ResetGrinderRoles.assertMissPerms(bot, grindAshesChannel) + ResetGrinderRoles.assertMissPerms(bot, auditFlameChannel) const users = await ResetGrinderRoles.getUsersWithLatestStreak(client.prisma) await ResetGrinderRoles.validateUsers(client.prisma, guild, grindAshesChannel, auditFlameChannel, users) diff --git a/src/bot/events/guild-member-update/grinder-role/handlers/index.ts b/src/bot/events/guild-member-update/grinder-role/handlers/index.ts index c593ffb..29dface 100644 --- a/src/bot/events/guild-member-update/grinder-role/handlers/index.ts +++ b/src/bot/events/guild-member-update/grinder-role/handlers/index.ts @@ -2,7 +2,7 @@ import type { TextChannel } from 'discord.js' import { AURA_FARMING_CHANNEL, GRINDER_ROLE } from '@config/discord' import { registerGuildMemberUpdateHandler } from '@events/guild-member-update/registry' import { EVENT_PATH } from '@events/index' -import { getChannel, sendAsBot } from '@utils/discord' +import { getBot, getChannel, sendAsBot } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { GrinderRole } from '../validators' @@ -27,7 +27,9 @@ registerGuildMemberUpdateHandler({ const oldHasGrinderRole = GrinderRole.isMemberHasRole(oldMember, GRINDER_ROLE) if (newHasGrinderRole && !oldHasGrinderRole) { const channel = await getChannel(newMember.guild, AURA_FARMING_CHANNEL) as TextChannel - GrinderRole.assertChannel(channel) + GrinderRole.assertTextChannel(channel) + const bot = await getBot(newMember.guild) + GrinderRole.assertMissPerms(bot, channel) const button = GrinderRole.generateButton(newMember.guild.id) await sendAsBot( diff --git a/src/bot/events/interaction-create/checkin/handlers/approve-button.ts b/src/bot/events/interaction-create/checkin/handlers/approve-button.ts index 0cb958a..5767bff 100644 --- a/src/bot/events/interaction-create/checkin/handlers/approve-button.ts +++ b/src/bot/events/interaction-create/checkin/handlers/approve-button.ts @@ -3,7 +3,7 @@ import { FLAMEWARDEN_ROLE } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { getMember, sendReply } from '@utils/discord' +import { getBot, getMember, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Checkin } from '../validators' @@ -34,7 +34,8 @@ registerInteractionHandler({ const { checkinId, checkinCreatedAt } = Checkin.getButtonId(interaction, interaction.customId) const channel = interaction.channel as TextChannel - Checkin.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) const flamewarden = await getMember(interaction.guild, interaction.member.id) Checkin.assertMember(flamewarden) Checkin.assertMemberHasRole(flamewarden, FLAMEWARDEN_ROLE) diff --git a/src/bot/events/interaction-create/checkin/handlers/custom-button-modal.ts b/src/bot/events/interaction-create/checkin/handlers/custom-button-modal.ts index 67dd20a..074cc0c 100644 --- a/src/bot/events/interaction-create/checkin/handlers/custom-button-modal.ts +++ b/src/bot/events/interaction-create/checkin/handlers/custom-button-modal.ts @@ -4,7 +4,7 @@ import { FLAMEWARDEN_ROLE } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { getMember, sendReply } from '@utils/discord' +import { getBot, getMember, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Checkin } from '../validators' @@ -35,7 +35,8 @@ registerInteractionHandler({ const { checkinId, checkinCreatedAt } = Checkin.getModalReviewId(interaction, interaction.customId) const channel = interaction.channel as TextChannel - Checkin.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) const flamewarden = await getMember(interaction.guild, interaction.member.id) Checkin.assertMember(flamewarden) Checkin.assertMemberHasRole(flamewarden, FLAMEWARDEN_ROLE) diff --git a/src/bot/events/interaction-create/checkin/handlers/custom-button.ts b/src/bot/events/interaction-create/checkin/handlers/custom-button.ts index c38ba00..54831cd 100644 --- a/src/bot/events/interaction-create/checkin/handlers/custom-button.ts +++ b/src/bot/events/interaction-create/checkin/handlers/custom-button.ts @@ -3,7 +3,7 @@ import { FLAMEWARDEN_ROLE } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { createCheckinReviewModal, encodeSnowflake, generateCustomId, getCustomId } from '@utils/component' -import { getMember, sendReply } from '@utils/discord' +import { getBot, getMember, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Checkin } from '../validators' @@ -31,7 +31,8 @@ registerInteractionHandler({ throw new CheckinCustomButtonError(Checkin.ERR.NotGuild) const channel = interaction.channel as TextChannel - Checkin.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) const flamewarden = await getMember(interaction.guild, interaction.member.id) Checkin.assertMember(flamewarden) Checkin.assertMemberHasRole(flamewarden, FLAMEWARDEN_ROLE) diff --git a/src/bot/events/interaction-create/checkin/handlers/detail-button.ts b/src/bot/events/interaction-create/checkin/handlers/detail-button.ts index 1cb2bc8..8704e5d 100644 --- a/src/bot/events/interaction-create/checkin/handlers/detail-button.ts +++ b/src/bot/events/interaction-create/checkin/handlers/detail-button.ts @@ -2,7 +2,7 @@ import type { GuildMember, TextChannel } from 'discord.js' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Checkin } from '../validators' @@ -32,7 +32,8 @@ registerInteractionHandler({ const channel = interaction.channel as TextChannel const member = interaction.member as GuildMember - Checkin.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) Checkin.assertMember(member) Checkin.assertMemberGrindRoles(member) diff --git a/src/bot/events/interaction-create/checkin/handlers/reject-button.ts b/src/bot/events/interaction-create/checkin/handlers/reject-button.ts index 55bdf4c..e452b0f 100644 --- a/src/bot/events/interaction-create/checkin/handlers/reject-button.ts +++ b/src/bot/events/interaction-create/checkin/handlers/reject-button.ts @@ -3,7 +3,7 @@ import { FLAMEWARDEN_ROLE } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { getMember, sendReply } from '@utils/discord' +import { getBot, getMember, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Checkin } from '../validators' @@ -34,7 +34,8 @@ registerInteractionHandler({ const { checkinId, checkinCreatedAt } = Checkin.getButtonId(interaction, interaction.customId) const channel = interaction.channel as TextChannel - Checkin.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Checkin.assertMissPerms(bot, channel) const flamewarden = await getMember(interaction.guild, interaction.member.id) Checkin.assertMember(flamewarden) Checkin.assertMemberHasRole(flamewarden, FLAMEWARDEN_ROLE) diff --git a/src/bot/events/interaction-create/checkin/validators/index.ts b/src/bot/events/interaction-create/checkin/validators/index.ts index e9ad0fd..27b6884 100644 --- a/src/bot/events/interaction-create/checkin/validators/index.ts +++ b/src/bot/events/interaction-create/checkin/validators/index.ts @@ -166,7 +166,6 @@ export class Checkin extends CheckinMessage { const hasGrindRole = this.isMemberHasRole(member, newRole.id) const channel = await getChannel(guild, AURA_FARMING_CHANNEL) as TextChannel - this.assertChannel(channel) if (!hasGrindRole) { await attachNewGrindRole(member, newRole) @@ -433,7 +432,7 @@ export class Checkin extends CheckinMessage { await this.assertSubmittedCheckinToday(client.prisma, opt) const updatedCheckin = await this.updateCheckinStatus(client.prisma, flamewarden, opt, checkinCreatedAt, checkinStatus, comment, isAudit) as CheckinType - const checkinChannel = await client.channels.fetch(CHECKIN_CHANNEL) as TextChannel + const checkinChannel = await getChannel(guild, CHECKIN_CHANNEL) as TextChannel const { messageId } = this.getMessageFromLink(updatedCheckin.link!) const message = await checkinChannel.messages.fetch(messageId) diff --git a/src/bot/events/interaction-create/embed/handlers/role-grant-create-button.ts b/src/bot/events/interaction-create/embed/handlers/role-grant-create-button.ts index bed00cd..6093cf6 100644 --- a/src/bot/events/interaction-create/embed/handlers/role-grant-create-button.ts +++ b/src/bot/events/interaction-create/embed/handlers/role-grant-create-button.ts @@ -2,7 +2,7 @@ import type { GuildMember, TextChannel } from 'discord.js' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { getRole, sendReply } from '@utils/discord' +import { getBot, getRole, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { RoleGrantCreate } from '../validators/role-grant-create' @@ -29,7 +29,8 @@ registerInteractionHandler({ throw new EmbedRoleGrantButtonError(RoleGrantCreate.ERR.NotGuild) const channel = interaction.channel as TextChannel - RoleGrantCreate.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + RoleGrantCreate.assertMissPerms(bot, channel) const { roleId } = RoleGrantCreate.getButtonId(interaction, interaction.customId) const member = interaction.member as GuildMember diff --git a/src/bot/events/interaction-create/embed/handlers/role-grant-create-modal.ts b/src/bot/events/interaction-create/embed/handlers/role-grant-create-modal.ts index 8faed0f..a6220b9 100644 --- a/src/bot/events/interaction-create/embed/handlers/role-grant-create-modal.ts +++ b/src/bot/events/interaction-create/embed/handlers/role-grant-create-modal.ts @@ -2,7 +2,7 @@ import type { TextChannel } from 'discord.js' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { createEmbed, encodeSnowflake, generateCustomId, getCustomId } from '@utils/component' -import { getChannel, getRole, sendAsBot, sendReply } from '@utils/discord' +import { getBot, getChannel, getRole, sendAsBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js' @@ -32,8 +32,9 @@ registerInteractionHandler({ const { channelId, roleId, buttonName } = RoleGrantCreate.getModalId(interaction, interaction.customId) const channel = await getChannel(interaction.guild, channelId) as TextChannel - RoleGrantCreate.assertChannel(channel) - RoleGrantCreate.assertMissPerms(interaction.client.user, channel) + RoleGrantCreate.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + RoleGrantCreate.assertMissPerms(bot, channel) const role = await getRole(interaction.guild, roleId) RoleGrantCreate.assertRole(role) diff --git a/src/bot/events/interaction-create/grinder-role/handlers/button.ts b/src/bot/events/interaction-create/grinder-role/handlers/button.ts index 98b6293..e96bf77 100644 --- a/src/bot/events/interaction-create/grinder-role/handlers/button.ts +++ b/src/bot/events/interaction-create/grinder-role/handlers/button.ts @@ -3,7 +3,7 @@ import { GrinderRole } from '@events/guild-member-update/grinder-role/validators import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' @@ -29,7 +29,9 @@ registerInteractionHandler({ throw new GrinderRoleButtonError(GrinderRole.ERR.NotGuild) const channel = interaction.channel as TextChannel - GrinderRole.assertMissPerms(interaction.client.user, channel) + GrinderRole.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + GrinderRole.assertMissPerms(bot, channel) await sendReply(interaction, GrinderRole.MSG.WelcomeNotes) } diff --git a/src/bot/events/interaction-create/jobs/handlers/reset-grinder-roles-button.ts b/src/bot/events/interaction-create/jobs/handlers/reset-grinder-roles-button.ts index 0bdd59c..aa1c6c2 100644 --- a/src/bot/events/interaction-create/jobs/handlers/reset-grinder-roles-button.ts +++ b/src/bot/events/interaction-create/jobs/handlers/reset-grinder-roles-button.ts @@ -3,7 +3,7 @@ import { ResetGrinderRoles } from '@events/client-ready/jobs/validators/reset-gr import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId } from '@utils/component' -import { sendReply } from '@utils/discord' +import { getBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' @@ -29,7 +29,9 @@ registerInteractionHandler({ throw new ResetGrinderRolesButtonError(ResetGrinderRoles.ERR.NotGuild) const channel = interaction.channel as TextChannel - ResetGrinderRoles.assertMissPerms(interaction.client.user, channel) + ResetGrinderRoles.assertTextChannel(channel) + const bot = await getBot(interaction.guild) + ResetGrinderRoles.assertMissPerms(bot, channel) const { thread } = await ResetGrinderRoles.getButtonId(interaction, interaction.customId) diff --git a/src/bot/events/interaction-create/message/handlers/send-modal.ts b/src/bot/events/interaction-create/message/handlers/send-modal.ts index 7347be4..60a0bdc 100644 --- a/src/bot/events/interaction-create/message/handlers/send-modal.ts +++ b/src/bot/events/interaction-create/message/handlers/send-modal.ts @@ -2,7 +2,7 @@ import type { Attachment, TextChannel } from 'discord.js' import { EVENT_PATH } from '@events/index' import { registerInteractionHandler } from '@events/interaction-create/registry' import { generateCustomId, tempStore } from '@utils/component' -import { getChannel, sendAsBot, sendReply } from '@utils/discord' +import { getBot, getChannel, sendAsBot, sendReply } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { Send } from '../validators/send' @@ -30,8 +30,8 @@ registerInteractionHandler({ const { channelId, tempToken } = Send.getModalId(interaction, interaction.customId) const channel = await getChannel(interaction.guild, channelId) as TextChannel - Send.assertChannel(channel) - Send.assertMissPerms(interaction.client.user, channel) + const bot = await getBot(interaction.guild) + Send.assertMissPerms(bot, channel) const attachments = tempStore.get(tempToken) as Attachment[] Send.delTempItem(attachments, tempToken) diff --git a/src/bot/events/message-create/channel/handlers/check-in.ts b/src/bot/events/message-create/channel/handlers/check-in.ts index f438c59..bb02a90 100644 --- a/src/bot/events/message-create/channel/handlers/check-in.ts +++ b/src/bot/events/message-create/channel/handlers/check-in.ts @@ -2,6 +2,7 @@ import type { TextChannel } from 'discord.js' import { CHECKIN_CHANNEL } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerMessageHandler } from '@events/message-create/registry' +import { getBot } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { log } from '@utils/logger' @@ -26,7 +27,9 @@ registerMessageHandler({ throw new CheckInError(CheckIn.ERR.NotGuild) const channel = msg.channel as TextChannel - CheckIn.assertMissPerms(msg.guild.members.me!, channel) + CheckIn.assertTextChannel(channel) + const bot = await getBot(msg.guild) + CheckIn.assertMissPerms(bot, channel) await msg.delete() log.warn(`${channel.name}: deleted unauthorized message from '${msg.author.tag}'`) diff --git a/src/bot/events/message-create/server-booster/handlers/index.ts b/src/bot/events/message-create/server-booster/handlers/index.ts index 825d115..a187ddd 100644 --- a/src/bot/events/message-create/server-booster/handlers/index.ts +++ b/src/bot/events/message-create/server-booster/handlers/index.ts @@ -2,7 +2,7 @@ import type { TextChannel } from 'discord.js' import { AURA_FARMING_CHANNEL, SYSTEM_ASHES_CHANNEL } from '@config/discord' import { EVENT_PATH } from '@events/index' import { registerMessageHandler } from '@events/message-create/registry' -import { getChannel, sendAsBot } from '@utils/discord' +import { getBot, getChannel, sendAsBot } from '@utils/discord' import { DiscordBaseError } from '@utils/discord/error' import { getModuleName } from '@utils/io' import { ServerBooster } from '../validators' @@ -25,8 +25,9 @@ registerMessageHandler({ throw new ServerBoosterError(ServerBooster.ERR.NotGuild) const auraFarmingChannel = await getChannel(msg.guild, AURA_FARMING_CHANNEL) as TextChannel - ServerBooster.assertChannel(auraFarmingChannel) - ServerBooster.assertMissPerms(msg.guild.members.me!, auraFarmingChannel) + ServerBooster.assertTextChannel(auraFarmingChannel) + const bot = await getBot(msg.guild) + ServerBooster.assertMissPerms(bot, auraFarmingChannel) const member = msg.member! ServerBooster.assertMember(member) diff --git a/src/bot/events/message-reaction-add/checkin/handlers/submitted.ts b/src/bot/events/message-reaction-add/checkin/handlers/submitted.ts index 62a9512..b82746e 100644 --- a/src/bot/events/message-reaction-add/checkin/handlers/submitted.ts +++ b/src/bot/events/message-reaction-add/checkin/handlers/submitted.ts @@ -34,7 +34,7 @@ registerReactionHandler({ const emoji = Checkin.assertEmojis(reaction.emoji.name) Checkin.assertMember(flamewarden) Checkin.assertMemberHasRole(flamewarden, FLAMEWARDEN_ROLE) - await Checkin.assertAllowedChannel(guild, message.channel.id, CHECKIN_CHANNEL) + await Checkin.assertAllowedTextChannel(guild, message.channel.id, CHECKIN_CHANNEL) await Checkin.validateCheckin( client, diff --git a/src/utils/discord/assert.ts b/src/utils/discord/assert.ts index 83bda9c..7a556be 100644 --- a/src/utils/discord/assert.ts +++ b/src/utils/discord/assert.ts @@ -1,7 +1,7 @@ import type { ClientUser, Guild, GuildMember, Message, Role, TextChannel, ThreadAutoArchiveDuration, ThreadChannel } from 'discord.js' import { getTempToken, parseMessageLink, tempStore } from '@utils/component' import { ChannelType, PermissionsBitField } from 'discord.js' -import { getBotPerms, getChannel, getMissPerms } from '.' +import { getChannel, getMissPerms, getPerms } from '.' import { DiscordBaseError } from './error' import { DiscordMessage } from './message' @@ -75,7 +75,7 @@ export class DiscordAssert extends DiscordMessage { throw new DiscordAssertError(this.ERR.MemberAboveMe) } - static assertChannel(channel: TextChannel) { + static assertTextChannel(channel: TextChannel) { if (!channel || channel.type !== ChannelType.GuildText) throw new DiscordAssertError(this.ERR.ChannelNotFound) } @@ -97,19 +97,19 @@ export class DiscordAssert extends DiscordMessage { throw new DiscordAssertError(this.ERR.RoleMissing(roleId)) } - static async assertAllowedChannel(guild: Guild, currentChannelId: string, channelId: string) { + static async assertAllowedTextChannel(guild: Guild, currentChannelId: string, channelId: string) { if (currentChannelId !== channelId) { throw new DiscordAssertError(this.ERR.AllowedChannel(channelId)) } const channel = await getChannel(guild, channelId) as TextChannel - this.assertChannel(channel) + this.assertTextChannel(channel) return channel } static assertMissPerms(user: ClientUser | GuildMember, channel: TextChannel) { - const channelPerms = getBotPerms(user, channel) + const channelPerms = getPerms(user, channel) const missedPerms = getMissPerms(channelPerms, this.BASE_PERMS) if (missedPerms.length) { diff --git a/src/utils/discord/index.ts b/src/utils/discord/index.ts index 65a0b49..74e92ad 100644 --- a/src/utils/discord/index.ts +++ b/src/utils/discord/index.ts @@ -24,7 +24,7 @@ export async function getBot(guild: Guild): Promise { return guild!.members.me as GuildMember ?? await guild!.members.fetchMe() } -export const getBotPerms = (user: ClientUser | GuildMember, channel: TextChannel): Readonly => channel.permissionsFor(user!)! +export const getPerms = (user: ClientUser | GuildMember, channel: TextChannel): Readonly => channel.permissionsFor(user!)! export function getAttachments(interaction: ChatInputCommandInteraction, fileCount: number): Attachment[] { const files: Attachment[] = []