mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 08:50:07 +00:00
@@ -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),
|
||||
})),
|
||||
};
|
||||
|
||||
|
||||
@@ -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),
|
||||
@@ -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;
|
||||
}),
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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[];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { cache } from "../../src/cache.ts";
|
||||
import { deleteServer } from "../../src/helpers/guilds/delete_server.ts";
|
||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||
import { deleteGuild } from "../../src/helpers/guilds/delete_guild.ts";
|
||||
import { delayUntil } from "../util/delay_until.ts";
|
||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "[guild] delete a guild",
|
||||
@@ -13,7 +13,7 @@ Deno.test({
|
||||
throw new Error("The guild was not cached so impossible to delete.");
|
||||
}
|
||||
|
||||
await deleteServer(tempData.guildId);
|
||||
await deleteGuild(tempData.guildId);
|
||||
await delayUntil(10000, () => !cache.guilds.has(tempData.guildId));
|
||||
|
||||
if (cache.guilds.has(tempData.guildId)) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { addReaction, cache, removeReaction, sendMessage } from "../../mod.ts";
|
||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||
import { assertEquals, assertExists } from "../deps.ts";
|
||||
import { delayUntil } from "../util/delay_until.ts";
|
||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||
|
||||
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
async function ifItFailsBlameWolf(type: "getter" | "raw", user = false) {
|
||||
const message = await sendMessage(tempData.channelId, "Hello World!");
|
||||
|
||||
// Assertions
|
||||
@@ -27,9 +27,14 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1);
|
||||
|
||||
if (type === "raw") {
|
||||
await removeReaction(message.channelId, message.id, "❤");
|
||||
await removeReaction(
|
||||
message.channelId,
|
||||
message.id,
|
||||
"❤",
|
||||
user ? { userId: message.author.id } : undefined,
|
||||
);
|
||||
} else {
|
||||
await message.removeReaction("❤");
|
||||
await message.removeReaction("❤", user ? message.author.id : undefined);
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
@@ -57,3 +62,19 @@ Deno.test({
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[message] remove a user reaction",
|
||||
async fn() {
|
||||
await ifItFailsBlameWolf("raw", true);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[message] message.removeReaction with user",
|
||||
async fn() {
|
||||
await ifItFailsBlameWolf("getter", true);
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import {
|
||||
addReaction,
|
||||
cache,
|
||||
removeUserReaction,
|
||||
sendMessage,
|
||||
} from "../../mod.ts";
|
||||
import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
|
||||
import { assertEquals, assertExists } from "../deps.ts";
|
||||
import { delayUntil } from "../util/delay_until.ts";
|
||||
|
||||
async function ifItFailsBlameWolf(type: "getter" | "raw") {
|
||||
const message = await sendMessage(tempData.channelId, "Hello World!");
|
||||
|
||||
// Assertions
|
||||
assertExists(message);
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed
|
||||
await delayUntil(10000, () => cache.messages.has(message.id));
|
||||
// Make sure the message was created.
|
||||
if (!cache.messages.has(message.id)) {
|
||||
throw new Error("The message seemed to be sent but it was not cached.");
|
||||
}
|
||||
|
||||
// Add reactions to the message
|
||||
await addReaction(message.channelId, message.id, "❤");
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions?.length === 1,
|
||||
);
|
||||
|
||||
// Be sure that the message has the reactions
|
||||
assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1);
|
||||
|
||||
if (type === "raw") {
|
||||
await removeUserReaction(
|
||||
message.channelId,
|
||||
message.id,
|
||||
"❤",
|
||||
message.author.id,
|
||||
);
|
||||
} else {
|
||||
//await message.removeUserReaction("❤", message.author.id);
|
||||
}
|
||||
|
||||
// Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed
|
||||
await delayUntil(
|
||||
10000,
|
||||
() => cache.messages.get(message.id)?.reactions === undefined,
|
||||
);
|
||||
|
||||
// Check if the reactions has been deleted
|
||||
assertEquals(await cache.messages.get(message.id)?.reactions, undefined);
|
||||
}
|
||||
|
||||
Deno.test({
|
||||
name: "[message] remove a user reaction",
|
||||
async fn() {
|
||||
await ifItFailsBlameWolf("raw");
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});
|
||||
/*
|
||||
Deno.test({
|
||||
name: "[message] message.removeReaction()",
|
||||
async fn() {
|
||||
await ifItFailsBlameWolf("getter");
|
||||
},
|
||||
...defaultTestOptions,
|
||||
});*/
|
||||
@@ -48,7 +48,6 @@ import "./messages/add_reactions.ts";
|
||||
import "./messages/remove_all_reactions.ts";
|
||||
import "./messages/remove_reaction.ts";
|
||||
import "./messages/remove_reaction_emoji.ts";
|
||||
import "./messages/remove_user_reaction.ts";
|
||||
import "./messages/create_message.ts";
|
||||
import "./messages/delete_message.ts";
|
||||
import "./messages/delete_messages.ts";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { botId, startBot } from "../../src/bot.ts";
|
||||
import { cache } from "../../src/cache.ts";
|
||||
import { deleteServer } from "../../src/helpers/guilds/delete_server.ts";
|
||||
import { deleteGuild } from "../../src/helpers/guilds/delete_guild.ts";
|
||||
import { delay } from "../../src/util/utils.ts";
|
||||
import { ws } from "../../src/ws/ws.ts";
|
||||
import { assertExists } from "../deps.ts";
|
||||
@@ -49,7 +49,7 @@ Deno.test({
|
||||
|
||||
// DELETE GUILDS IF LESS THAN 10 SERVERS AS SAFETY MEASURE
|
||||
if (cache.guilds.size <= 10) {
|
||||
for (const guild of cache.guilds.values()) await deleteServer(guild.id);
|
||||
for (const guild of cache.guilds.values()) await deleteGuild(guild.id);
|
||||
}
|
||||
|
||||
// Assertions
|
||||
|
||||
Reference in New Issue
Block a user