more refactoring

This commit is contained in:
Skillz
2020-09-16 12:18:34 -04:00
parent 5e0c4e6432
commit f07636338c
3 changed files with 6 additions and 51 deletions
+4 -48
View File
@@ -4,7 +4,7 @@ import { botHasPermission } from "../utils/permissions.ts";
import { RequestManager } from "../module/requestManager.ts"; import { RequestManager } from "../module/requestManager.ts";
import { endpoints } from "../constants/discord.ts"; import { endpoints } from "../constants/discord.ts";
import { Errors } from "../types/errors.ts"; import { Errors } from "../types/errors.ts";
import { Permissions, Permission } from "../types/permission.ts"; import { Permissions } from "../types/permission.ts";
import { import {
ChannelCreatePayload, ChannelCreatePayload,
ChannelTypes, 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. */ /** Gets an array of all the channels ids that are the children of this category. */
export function categoryChildrenIDs(guild: Guild, id: string) { export function categoryChildrenIDs(guild: Guild, id: string) {
const channelIDs: string[] = []; return guild.channels.filter((channel) => channel.parentID === id);
guild.channels.forEach((channel) => {
if (channel.parentID === id) channelIDs.push(channel.id);
});
return channelIDs;
} }
/** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ /** 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)); 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) { export function syncIntegration(guildID: string, id: string) {
if ( if (
!botHasPermission(guildID, [Permissions.MANAGE_GUILD]) !botHasPermission(guildID, [Permissions.MANAGE_GUILD])
@@ -517,7 +512,7 @@ export function getBan(guildID: string, memberID: string) {
) as Promise<BannedUser>; ) as Promise<BannedUser>;
} }
/** 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) { export function ban(guildID: string, id: string, options: BanOptions) {
if ( if (
!botHasPermission(guildID, [Permissions.BAN_MEMBERS]) !botHasPermission(guildID, [Permissions.BAN_MEMBERS])
@@ -541,45 +536,6 @@ export function unban(guildID: string, id: string) {
return RequestManager.delete(endpoints.GUILD_BAN(guildID, id)); 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. */ /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
export async function editGuild(guildID: string, options: GuildEditOptions) { export async function editGuild(guildID: string, options: GuildEditOptions) {
if ( if (
+1 -1
View File
@@ -115,7 +115,7 @@ export async function sendDirectMessage(
} }
// If it does exist try sending a message to this user // 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 */ /** Kick a member from the server */
+1 -2
View File
@@ -21,9 +21,8 @@ export async function deleteMessage(
if (message.author.id !== botID) { if (message.author.id !== botID) {
// This needs to check the channels permission not the guild permission // This needs to check the channels permission not the guild permission
if ( if (
!message.channel.guildID ||
!botHasChannelPermissions( !botHasChannelPermissions(
message.channel.id, message.channelID,
[Permissions.MANAGE_MESSAGES], [Permissions.MANAGE_MESSAGES],
) )
) { ) {