Merge pull request #876 from discordeno/some-things

Some things
This commit is contained in:
ITOH
2021-04-30 19:00:23 +02:00
committed by GitHub
15 changed files with 79 additions and 142 deletions

View File

@@ -29,8 +29,8 @@ export async function cloneChannel(channelId: string, reason?: string) {
) => ({
id: overwrite.id,
type: overwrite.type,
allow: calculatePermissions(BigInt(overwrite.allow)),
deny: calculatePermissions(BigInt(overwrite.deny)),
allow: calculatePermissions(overwrite.allow),
deny: calculatePermissions(overwrite.deny),
})),
};

View File

@@ -1,9 +1,8 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event.
*/
export async function deleteServer(guildId: string) {
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event. */
export async function deleteGuild(guildId: string) {
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILDS_BASE(guildId),

View File

@@ -19,7 +19,10 @@ import { ws } from "../../ws/ws.ts";
* REST(this function): 50/s global(across all shards) rate limit with ALL requests this included
* GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m.
*/
export async function getMembers(guildId: string, options?: ListGuildMembers) {
export async function getMembers(
guildId: string,
options?: ListGuildMembers & { addToCache?: boolean },
) {
if (!(ws.identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS)) {
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
}
@@ -61,11 +64,13 @@ export async function getMembers(guildId: string, options?: ListGuildMembers) {
guildId,
);
await cacheHandlers.set(
"members",
discordenoMember.id,
discordenoMember,
);
if (options?.addToCache !== false) {
await cacheHandlers.set(
"members",
discordenoMember.id,
discordenoMember,
);
}
return discordenoMember;
}),

View File

@@ -1,12 +1,18 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
/** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeReaction(
channelId: string,
messageId: string,
reaction: string,
options?: { userId?: string },
) {
if (options?.userId) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
}
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
@@ -15,6 +21,13 @@ export async function removeReaction(
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
options?.userId
? endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelId,
messageId,
reaction,
options.userId,
)
: endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
);
}

View File

@@ -1,29 +0,0 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes a reaction from the specified user on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeUserReaction(
channelId: string,
messageId: string,
reaction: string,
userId: string,
) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelId,
messageId,
reaction,
userId,
),
);
}

View File

@@ -38,7 +38,7 @@ import { emojiURL } from "./emojis/emoji_url.ts";
import { getEmoji } from "./emojis/get_emoji.ts";
import { getEmojis } from "./emojis/get_emojis.ts";
import { createGuild } from "./guilds/create_guild.ts";
import { deleteServer } from "./guilds/delete_server.ts";
import { deleteGuild } from "./guilds/delete_guild.ts";
import { editGuild } from "./guilds/edit_guild.ts";
import { editWelcomeScreen } from "./guilds/edit_welcome_screen.ts";
import { editWidget } from "./guilds/edit_widget.ts";
@@ -93,7 +93,6 @@ import { publishMessage } from "./messages/publish_message.ts";
import { removeAllReactions } from "./messages/remove_all_reactions.ts";
import { removeReaction } from "./messages/remove_reaction.ts";
import { removeReactionEmoji } from "./messages/remove_reaction_emoji.ts";
import { removeUserReaction } from "./messages/remove_user_reaction.ts";
import { sendMessage } from "./messages/send_message.ts";
import { unpin, unpinMessage } from "./messages/unpin_message.ts";
import { editBotStatus } from "./misc/edit_bot_status.ts";
@@ -147,13 +146,13 @@ export {
deleteChannel,
deleteChannelOverwrite,
deleteEmoji,
deleteGuild,
deleteGuildTemplate,
deleteIntegration,
deleteInvite,
deleteMessage,
deleteMessages,
deleteRole,
deleteServer,
deleteSlashCommand,
deleteSlashResponse,
deleteWebhook,
@@ -240,7 +239,6 @@ export {
removeReaction,
removeReactionEmoji,
removeRole,
removeUserReaction,
sendDirectMessage,
sendInteractionResponse,
sendMessage,
@@ -295,7 +293,7 @@ export let helpers = {
// guilds
categoryChildren,
createGuild,
deleteServer,
deleteGuild,
editGuild,
editWidget,
editWelcomeScreen,
@@ -361,7 +359,6 @@ export let helpers = {
removeAllReactions,
removeReactionEmoji,
removeReaction,
removeUserReaction,
sendMessage,
unpinMessage,
// misc

View File

@@ -1,6 +1,6 @@
import { botId, eventHandlers } from "../bot.ts";
import { cache, cacheHandlers } from "../cache.ts";
import { deleteServer } from "../helpers/guilds/delete_server.ts";
import { deleteGuild } from "../helpers/guilds/delete_guild.ts";
import { editGuild } from "../helpers/guilds/edit_guild.ts";
import { getAuditLogs } from "../helpers/guilds/get_audit_logs.ts";
import { getBan } from "../helpers/guilds/get_ban.ts";
@@ -79,7 +79,7 @@ const baseGuild: Partial<DiscordenoGuild> = {
return guildSplashURL(this.id!, this.splash!, size, format);
},
delete() {
return deleteServer(this.id!);
return deleteGuild(this.id!);
},
edit(options) {
return editGuild(this.id!, options);
@@ -272,7 +272,7 @@ export interface DiscordenoGuild extends
format?: DiscordImageFormat,
): string | undefined;
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event. */
delete(): ReturnType<typeof deleteServer>;
delete(): ReturnType<typeof deleteGuild>;
/** Leave a guild */
leave(): ReturnType<typeof leaveGuild>;
/** Edit the server. Requires the MANAGE_GUILD permission. */

View File

@@ -121,8 +121,8 @@ const baseMessage: Partial<DiscordenoMessage> = {
removeReactionEmoji(reaction) {
return removeReactionEmoji(this.channelId!, this.id!, reaction);
},
removeReaction(reaction) {
return removeReaction(this.channelId!, this.id!, reaction);
removeReaction(reaction, userId) {
return removeReaction(this.channelId!, this.id!, reaction, { userId });
},
};
@@ -250,10 +250,13 @@ export interface DiscordenoMessage
timeout?: number,
reason?: string,
): Promise<unknown>;
/** Remove all reactions */
/** Removes all reactions for all emojis on this message */
removeAllReactions(): ReturnType<typeof removeAllReactions>;
/** Remove all reactions */
/** Removes all reactions for a single emoji on this message */
removeReactionEmoji(reaction: string): ReturnType<typeof removeReactionEmoji>;
/** Remove all reactions */
removeReaction(reaction: string): ReturnType<typeof removeReaction>;
/** Removes a reaction from the given user on this message, defaults to bot */
removeReaction(
reaction: string,
userId?: string,
): ReturnType<typeof removeReaction>;
}

View File

@@ -161,8 +161,6 @@ export interface EventHandlers {
) => unknown;
/** Sent before every event execution. Discordeno will not await its execution. */
raw?: (data: GatewayPayload) => unknown;
// TODO: remove this?
// rawGateway?: (data: unknown) => unknown;
/** Sent when all shards went ready. */
ready?: () => unknown;
/** Sent when a user adds a reaction to a message. */

View File

@@ -201,9 +201,9 @@ export async function getMissingGuildPermissions(
member: string | DiscordenoMember,
permissions: PermissionStrings[],
) {
// First we need the role permissino bits this member has
// First we need the role permission bits this member has
const permissionBits = await calculateBasePermissions(guild, member);
// Second returnn the members missing permissions
// Second return the members missing permissions
return missingPermissions(permissionBits, permissions);
}
@@ -268,13 +268,13 @@ export function requireBotChannelPermissions(
}
/** This function converts a bitwise string to permission strings */
export function calculatePermissions(permissionBits: bigint) {
export function calculatePermissions(permissionBits: string) {
return Object.keys(DiscordBitwisePermissionFlags).filter((permission) => {
// Since Object.keys() not only returns the permission names but also the bit values we need to return false if it is a Number
if (Number(permission)) return false;
// Check if permissionBits has this permission
return (
permissionBits &
BigInt(permissionBits) &
BigInt(DiscordBitwisePermissionFlags[permission as PermissionStrings])
);
}) as PermissionStrings[];