diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 9a8851d7e..d021ab284 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -4,7 +4,7 @@ import { botHasPermission } from "../utils/permissions.ts"; import { RequestManager } from "../module/requestManager.ts"; import { endpoints } from "../constants/discord.ts"; import { Errors } from "../types/errors.ts"; -import { Permissions, Permission } from "../types/permission.ts"; +import { Permissions } from "../types/permission.ts"; import { ChannelCreatePayload, ChannelTypes, @@ -39,12 +39,7 @@ import { cacheHandlers } from "../controllers/cache.ts"; /** Gets an array of all the channels ids that are the children of this category. */ export function categoryChildrenIDs(guild: Guild, id: string) { - const channelIDs: string[] = []; - guild.channels.forEach((channel) => { - if (channel.parentID === id) channelIDs.push(channel.id); - }); - - return channelIDs; + return guild.channels.filter((channel) => channel.parentID === id); } /** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ @@ -477,7 +472,7 @@ export function deleteIntegration(guildID: string, id: string) { return RequestManager.delete(endpoints.GUILD_INTEGRATION(guildID, id)); } -/** Sync an integration. Requires teh MANAGE_GUILD permission. */ +/** Sync an integration. Requires the MANAGE_GUILD permission. */ export function syncIntegration(guildID: string, id: string) { if ( !botHasPermission(guildID, [Permissions.MANAGE_GUILD]) @@ -517,7 +512,7 @@ export function getBan(guildID: string, memberID: string) { ) as Promise; } -/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires teh BAN_MEMBERS permission. */ +/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */ export function ban(guildID: string, id: string, options: BanOptions) { if ( !botHasPermission(guildID, [Permissions.BAN_MEMBERS]) @@ -541,45 +536,6 @@ export function unban(guildID: string, id: string) { return RequestManager.delete(endpoints.GUILD_BAN(guildID, id)); } -/** Check whether a member has certain permissions in this channel. */ -export function channelHasPermissions( - guild: Guild, - channelID: string, - memberID: string, - permissions: Permission[], -) { - if (memberID === guild.ownerID) return true; - - const member = guild.members.get(memberID); - if (!member) { - throw new Error( - "Invalid member id provided. This member was not found in the cache. Please fetch them with getMember on guild.", - ); - } - - const channel = guild.channels.get(channelID); - if (!channel) { - throw new Error( - "Invalid channel id provided. This channel was not found in the cache.", - ); - } - - let permissionBits = member.roles.reduce((bits, roleID) => { - const role = guild.roles.get(roleID); - if (!role) return bits; - - bits |= BigInt(role.permissions_new); - - return bits; - }, BigInt(0)); - - if (permissionBits & BigInt(Permissions.ADMINISTRATOR)) return true; - - return permissions.every((permission) => - permissionBits & BigInt(Permissions[permission]) - ); -} - /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ export async function editGuild(guildID: string, options: GuildEditOptions) { if ( diff --git a/src/handlers/member.ts b/src/handlers/member.ts index 597a62dba..a7f2b9586 100644 --- a/src/handlers/member.ts +++ b/src/handlers/member.ts @@ -115,7 +115,7 @@ export async function sendDirectMessage( } // If it does exist try sending a message to this user - return sendMessage(dmChannel, content); + return sendMessage(dmChannel.id, content); } /** Kick a member from the server */ diff --git a/src/handlers/message.ts b/src/handlers/message.ts index 79352f301..6d6758cd6 100644 --- a/src/handlers/message.ts +++ b/src/handlers/message.ts @@ -21,9 +21,8 @@ export async function deleteMessage( if (message.author.id !== botID) { // This needs to check the channels permission not the guild permission if ( - !message.channel.guildID || !botHasChannelPermissions( - message.channel.id, + message.channelID, [Permissions.MANAGE_MESSAGES], ) ) {