refactoring

This commit is contained in:
Skillz
2020-08-05 15:01:40 -04:00
parent 65b5c3ac40
commit dc54405c76
3 changed files with 49 additions and 41 deletions
+7 -6
View File
@@ -235,17 +235,17 @@ export function emojiURL(id: string, animated = false) {
/** Create a new role for the guild. Requires the MANAGE_ROLES permission. */
export async function createGuildRole(
guild: Guild,
guildID: string,
options: CreateRoleOptions,
reason?: string,
) {
if (
!botHasPermission(guild.id, [Permissions.MANAGE_ROLES])
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
) {
throw new Error(Errors.MISSING_MANAGE_ROLES);
}
const role_data = await RequestManager.post(
endpoints.GUILD_ROLES(guild.id),
const result = await RequestManager.post(
endpoints.GUILD_ROLES(guildID),
{
...options,
permissions: options.permissions
@@ -257,9 +257,10 @@ export async function createGuildRole(
},
);
const roleData = role_data as RoleData;
const roleData = result as RoleData;
const role = createRole(roleData);
guild.roles.set(role.id, role);
const guild = cache.guilds.get(guildID)
guild?.roles.set(role.id, role);
return role;
}
+16 -17
View File
@@ -8,7 +8,6 @@ import {
botHasPermission,
} from "../utils/permissions.ts";
import { botID } from "../module/client.ts";
import { Guild } from "../structures/guild.ts";
import { Permissions } from "../types/permission.ts";
import { Errors } from "../types/errors.ts";
import { RequestManager } from "../module/requestManager.ts";
@@ -35,25 +34,25 @@ export function avatarURL(
/** Add a role to the member */
export function addRole(
guild: Guild,
guildID: string,
memberID: string,
roleID: string,
reason?: string,
) {
const botsHighestRole = highestRole(guild.id, botID);
const botsHighestRole = highestRole(guildID, botID);
if (
botsHighestRole &&
!higherRolePosition(guild.id, botsHighestRole.id, roleID)
!higherRolePosition(guildID, botsHighestRole.id, roleID)
) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
}
if (!botHasPermission(guild.id, [Permissions.MANAGE_ROLES])) {
if (!botHasPermission(guildID, [Permissions.MANAGE_ROLES])) {
throw new Error(Errors.MISSING_MANAGE_ROLES);
}
return RequestManager.put(
endpoints.GUILD_MEMBER_ROLE(guild.id, memberID, roleID),
endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID),
{ reason },
);
}
@@ -107,9 +106,9 @@ export async function sendDirectMessage(
}
/** Kick a member from the server */
export function kick(guild: Guild, memberID: string, reason?: string) {
const botsHighestRole = highestRole(guild.id, botID);
const membersHighestRole = highestRole(guild.id, memberID);
export function kick(guildID: string, memberID: string, reason?: string) {
const botsHighestRole = highestRole(guildID, botID);
const membersHighestRole = highestRole(guildID, memberID);
if (
botsHighestRole && membersHighestRole &&
botsHighestRole.position <= membersHighestRole.position
@@ -117,18 +116,18 @@ export function kick(guild: Guild, memberID: string, reason?: string) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
}
if (!botHasPermission(guild.id, [Permissions.KICK_MEMBERS])) {
if (!botHasPermission(guildID, [Permissions.KICK_MEMBERS])) {
throw new Error(Errors.MISSING_KICK_MEMBERS);
}
return RequestManager.delete(
endpoints.GUILD_MEMBER(guild.id, memberID),
endpoints.GUILD_MEMBER(guildID, memberID),
{ reason },
);
}
/** Edit the member */
export function editMember(
guild: Guild,
guildID: string,
memberID: string,
options: EditMemberOptions,
) {
@@ -136,14 +135,14 @@ export function editMember(
if (options.nick.length > 32) {
throw new Error(Errors.NICKNAMES_MAX_LENGTH);
}
if (!botHasPermission(guild.id, [Permissions.MANAGE_NICKNAMES])) {
if (!botHasPermission(guildID, [Permissions.MANAGE_NICKNAMES])) {
throw new Error(Errors.MISSING_MANAGE_NICKNAMES);
}
}
if (
options.roles &&
!botHasPermission(guild.id, [Permissions.MANAGE_ROLES])
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
) {
throw new Error(Errors.MISSING_MANAGE_ROLES);
}
@@ -151,7 +150,7 @@ export function editMember(
if (options.mute) {
// TODO: This should check if the member is in a voice channel
if (
!botHasPermission(guild.id, [Permissions.MUTE_MEMBERS])
!botHasPermission(guildID, [Permissions.MUTE_MEMBERS])
) {
throw new Error(Errors.MISSING_MUTE_MEMBERS);
}
@@ -159,7 +158,7 @@ export function editMember(
if (
options.deaf &&
!botHasPermission(guild.id, [Permissions.DEAFEN_MEMBERS])
!botHasPermission(guildID, [Permissions.DEAFEN_MEMBERS])
) {
throw new Error(Errors.MISSING_DEAFEN_MEMBERS);
}
@@ -167,7 +166,7 @@ export function editMember(
// TODO: if channel id is provided check if the bot has CONNECT and MOVE in channel and current channel
return RequestManager.patch(
endpoints.GUILD_MEMBER(guild.id, memberID),
endpoints.GUILD_MEMBER(guildID, memberID),
options,
);
}
+26 -18
View File
@@ -40,70 +40,78 @@ export async function deleteMessage(
}
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
export function pin(message: Message) {
export function pin(channelID: string, messageID: string) {
if (
!botHasChannelPermissions(message.channelID, [Permissions.MANAGE_MESSAGES])
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
) {
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
}
RequestManager.put(endpoints.CHANNEL_MESSAGE(message.channelID, message.id));
RequestManager.put(endpoints.CHANNEL_MESSAGE(channelID, messageID));
}
/** Unpin a message in a channel. Requires MANAGE_MESSAGES. */
export function unpin(message: Message) {
export function unpin(channelID: string, messageID: string) {
if (
!botHasChannelPermissions(message.channelID, [Permissions.MANAGE_MESSAGES])
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
) {
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
}
RequestManager.delete(
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
endpoints.CHANNEL_MESSAGE(channelID, messageID),
);
}
/** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
export function addReaction(message: Message, reaction: string) {
export function addReaction(
channelID: string,
messageID: string,
reaction: string,
) {
RequestManager.put(
endpoints.CHANNEL_MESSAGE_REACTION_ME(
message.channelID,
message.id,
channelID,
messageID,
reaction,
),
);
}
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export function removeReaction(message: Message, reaction: string) {
export function removeReaction(
channelID: string,
messageID: string,
reaction: string,
) {
RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTION_ME(
message.channelID,
message.id,
channelID,
messageID,
reaction,
),
);
}
/** Removes all reactions for all emojis on this message. */
export function removeAllReactions(message: Message) {
export function removeAllReactions(channelID: string, messageID: string) {
if (
!botHasChannelPermissions(message.channelID, [Permissions.MANAGE_MESSAGES])
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
) {
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
}
RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTIONS(message.channelID, message.id),
endpoints.CHANNEL_MESSAGE_REACTIONS(channelID, messageID),
);
}
/** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export function removeReactionEmoji(message: Message, reaction: string) {
export function removeReactionEmoji(channelID: string, messageID: string, reaction: string) {
if (
!botHasChannelPermissions(message.channelID, [Permissions.MANAGE_MESSAGES])
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
) {
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
}
RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTION(message.channelID, message.id, reaction),
endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction),
);
}