Merge branch 'main' into threads

This commit is contained in:
ITOH
2021-05-03 20:50:53 +02:00
192 changed files with 1364 additions and 623 deletions
+1 -4
View File
@@ -10,8 +10,5 @@ export * from "./src/structures/message.ts";
export * from "./src/structures/mod.ts"; export * from "./src/structures/mod.ts";
export * from "./src/structures/role.ts"; export * from "./src/structures/role.ts";
export * from "./src/types/mod.ts"; export * from "./src/types/mod.ts";
export * from "./src/util/collection.ts"; export * from "./src/util/mod.ts";
export * from "./src/util/constants.ts";
export * from "./src/util/permissions.ts";
export * from "./src/util/utils.ts";
export * from "./src/ws/mod.ts"; export * from "./src/ws/mod.ts";
+5 -4
View File
@@ -2,12 +2,13 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts";
import { rest } from "./rest/rest.ts"; import { rest } from "./rest/rest.ts";
import { EventHandlers } from "./types/discordeno/eventHandlers.ts"; import { EventHandlers } from "./types/discordeno/eventHandlers.ts";
import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts";
import { snowflakeToBigint } from "./util/bigint.ts";
import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts"; import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts";
import { ws } from "./ws/ws.ts"; import { ws } from "./ws/ws.ts";
export let secretKey = ""; export let secretKey = "";
export let botId = ""; export let botId = 0n;
export let applicationId = ""; export let applicationId = 0n;
export let eventHandlers: EventHandlers = {}; export let eventHandlers: EventHandlers = {};
@@ -49,12 +50,12 @@ export function updateEventHandlers(newEventHandlers: EventHandlers) {
/** INTERNAL LIB function used to set the bot Id once the READY event is sent by Discord. */ /** INTERNAL LIB function used to set the bot Id once the READY event is sent by Discord. */
export function setBotId(id: string) { export function setBotId(id: string) {
if (botId !== id) botId = id; botId = snowflakeToBigint(id);
} }
/** INTERNAL LIB function used to set the application Id once the READY event is sent by Discord. */ /** INTERNAL LIB function used to set the application Id once the READY event is sent by Discord. */
export function setApplicationId(id: string) { export function setApplicationId(id: string) {
if (applicationId !== id) applicationId = id; applicationId = snowflakeToBigint(id);
} }
// BIG BRAIN BOT STUFF ONLY BELOW THIS // BIG BRAIN BOT STUFF ONLY BELOW THIS
+52 -52
View File
@@ -10,26 +10,26 @@ import { Collection } from "./util/collection.ts";
export const cache = { export const cache = {
isReady: false, isReady: false,
/** All of the guild objects the bot has access to, mapped by their Ids */ /** All of the guild objects the bot has access to, mapped by their Ids */
guilds: new Collection<string, DiscordenoGuild>(), guilds: new Collection<bigint, DiscordenoGuild>(),
/** All of the channel objects the bot has access to, mapped by their Ids */ /** All of the channel objects the bot has access to, mapped by their Ids */
channels: new Collection<string, DiscordenoChannel>(), channels: new Collection<bigint, DiscordenoChannel>(),
/** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */ /** All of the message objects the bot has cached since the bot acquired `READY` state, mapped by their Ids */
messages: new Collection<string, DiscordenoMessage>(), messages: new Collection<bigint, DiscordenoMessage>(),
/** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */ /** All of the member objects that have been cached since the bot acquired `READY` state, mapped by their Ids */
members: new Collection<string, DiscordenoMember>(), members: new Collection<bigint, DiscordenoMember>(),
/** All of the unavailable guilds, mapped by their Ids (id, timestamp) */ /** All of the unavailable guilds, mapped by their Ids (id, timestamp) */
unavailableGuilds: new Collection<string, number>(), unavailableGuilds: new Collection<bigint, number>(),
/** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */ /** All of the presence update objects received in PRESENCE_UPDATE gateway event, mapped by their user Id */
presences: new Collection<string, PresenceUpdate>(), presences: new Collection<bigint, PresenceUpdate>(),
fetchAllMembersProcessingRequests: new Collection< fetchAllMembersProcessingRequests: new Collection<
string, string,
( (
value: value:
| Collection<string, DiscordenoMember> | Collection<bigint, DiscordenoMember>
| PromiseLike<Collection<string, DiscordenoMember>>, | PromiseLike<Collection<bigint, DiscordenoMember>>,
) => void ) => void
>(), >(),
executedSlashCommands: new Collection<string, string>(), executedSlashCommands: new Set<string>(),
get emojis() { get emojis() {
return new Collection<string, Emoji>( return new Collection<string, Emoji>(
this.guilds.reduce( this.guilds.reduce(
@@ -46,11 +46,11 @@ export let cacheHandlers = {
return cache[table].clear(); return cache[table].clear();
}, },
/** Deletes 1 item from cache using the key */ /** Deletes 1 item from cache using the key */
async delete(table: TableName, key: string) { async delete(table: TableName, key: bigint) {
return cache[table].delete(key); return cache[table].delete(key);
}, },
/** Check if something exists in cache with a key */ /** Check if something exists in cache with a key */
async has(table: TableName, key: string) { async has(table: TableName, key: bigint) {
return cache[table].has(key); return cache[table].has(key);
}, },
@@ -80,63 +80,63 @@ export type TableName =
function set( function set(
table: "guilds", table: "guilds",
key: string, key: bigint,
value: DiscordenoGuild, value: DiscordenoGuild,
): Promise<Collection<string, DiscordenoGuild>>; ): Promise<Collection<bigint, DiscordenoGuild>>;
function set( function set(
table: "channels", table: "channels",
key: string, key: bigint,
value: DiscordenoChannel, value: DiscordenoChannel,
): Promise<Collection<string, DiscordenoChannel>>; ): Promise<Collection<bigint, DiscordenoChannel>>;
function set( function set(
table: "messages", table: "messages",
key: string, key: bigint,
value: DiscordenoMessage, value: DiscordenoMessage,
): Promise<Collection<string, DiscordenoMessage>>; ): Promise<Collection<bigint, DiscordenoMessage>>;
function set( function set(
table: "members", table: "members",
key: string, key: bigint,
value: DiscordenoMember, value: DiscordenoMember,
): Promise<Collection<string, DiscordenoMember>>; ): Promise<Collection<bigint, DiscordenoMember>>;
function set( function set(
table: "presences", table: "presences",
key: string, key: bigint,
value: PresenceUpdate, value: PresenceUpdate,
): Promise<Collection<string, PresenceUpdate>>; ): Promise<Collection<bigint, PresenceUpdate>>;
function set( function set(
table: "unavailableGuilds", table: "unavailableGuilds",
key: string, key: bigint,
value: number, value: number,
): Promise<Collection<string, number>>; ): Promise<Collection<bigint, number>>;
async function set(table: TableName, key: string, value: any) { async function set(table: TableName, key: bigint, value: any) {
return cache[table].set(key, value); return cache[table].set(key, value);
} }
function get( function get(
table: "guilds", table: "guilds",
key: string, key: bigint,
): Promise<DiscordenoGuild | undefined>; ): Promise<DiscordenoGuild | undefined>;
function get( function get(
table: "channels", table: "channels",
key: string, key: bigint,
): Promise<DiscordenoChannel | undefined>; ): Promise<DiscordenoChannel | undefined>;
function get( function get(
table: "messages", table: "messages",
key: string, key: bigint,
): Promise<DiscordenoMessage | undefined>; ): Promise<DiscordenoMessage | undefined>;
function get( function get(
table: "members", table: "members",
key: string, key: bigint,
): Promise<DiscordenoMember | undefined>; ): Promise<DiscordenoMember | undefined>;
function get( function get(
table: "presences", table: "presences",
key: string, key: bigint,
): Promise<PresenceUpdate | undefined>; ): Promise<PresenceUpdate | undefined>;
function get( function get(
table: "unavailableGuilds", table: "unavailableGuilds",
key: string, key: bigint,
): Promise<number | undefined>; ): Promise<number | undefined>;
async function get(table: TableName, key: string) { async function get(table: TableName, key: bigint) {
return cache[table].get(key); return cache[table].get(key);
} }
@@ -144,68 +144,68 @@ function forEach(
table: "guilds", table: "guilds",
callback: ( callback: (
value: DiscordenoGuild, value: DiscordenoGuild,
key: string, key: bigint,
map: Map<string, DiscordenoGuild>, map: Map<bigint, DiscordenoGuild>,
) => unknown, ) => unknown,
): void; ): void;
function forEach( function forEach(
table: "unavailableGuilds", table: "unavailableGuilds",
callback: (value: number, key: string, map: Map<string, number>) => unknown, callback: (value: number, key: bigint, map: Map<bigint, number>) => unknown,
): void; ): void;
function forEach( function forEach(
table: "channels", table: "channels",
callback: ( callback: (
value: DiscordenoChannel, value: DiscordenoChannel,
key: string, key: bigint,
map: Map<string, DiscordenoChannel>, map: Map<bigint, DiscordenoChannel>,
) => unknown, ) => unknown,
): void; ): void;
function forEach( function forEach(
table: "messages", table: "messages",
callback: ( callback: (
value: DiscordenoMessage, value: DiscordenoMessage,
key: string, key: bigint,
map: Map<string, DiscordenoMessage>, map: Map<bigint, DiscordenoMessage>,
) => unknown, ) => unknown,
): void; ): void;
function forEach( function forEach(
table: "members", table: "members",
callback: ( callback: (
value: DiscordenoMember, value: DiscordenoMember,
key: string, key: bigint,
map: Map<string, DiscordenoMember>, map: Map<bigint, DiscordenoMember>,
) => unknown, ) => unknown,
): void; ): void;
function forEach( function forEach(
table: TableName, table: TableName,
callback: (value: any, key: string, map: Map<string, any>) => unknown, callback: (value: any, key: bigint, map: Map<bigint, any>) => unknown,
) { ) {
return cache[table].forEach(callback); return cache[table].forEach(callback);
} }
function filter( function filter(
table: "guilds", table: "guilds",
callback: (value: DiscordenoGuild, key: string) => boolean, callback: (value: DiscordenoGuild, key: bigint) => boolean,
): Promise<Collection<string, DiscordenoGuild>>; ): Promise<Collection<bigint, DiscordenoGuild>>;
function filter( function filter(
table: "unavailableGuilds", table: "unavailableGuilds",
callback: (value: number, key: string) => boolean, callback: (value: number, key: bigint) => boolean,
): Promise<Collection<string, number>>; ): Promise<Collection<bigint, number>>;
function filter( function filter(
table: "channels", table: "channels",
callback: (value: DiscordenoChannel, key: string) => boolean, callback: (value: DiscordenoChannel, key: bigint) => boolean,
): Promise<Collection<string, DiscordenoChannel>>; ): Promise<Collection<bigint, DiscordenoChannel>>;
function filter( function filter(
table: "messages", table: "messages",
callback: (value: DiscordenoMessage, key: string) => boolean, callback: (value: DiscordenoMessage, key: bigint) => boolean,
): Promise<Collection<string, DiscordenoMessage>>; ): Promise<Collection<bigint, DiscordenoMessage>>;
function filter( function filter(
table: "members", table: "members",
callback: (value: DiscordenoMember, key: string) => boolean, callback: (value: DiscordenoMember, key: bigint) => boolean,
): Promise<Collection<string, DiscordenoMember>>; ): Promise<Collection<bigint, DiscordenoMember>>;
async function filter( async function filter(
table: TableName, table: TableName,
callback: (value: any, key: string) => boolean, callback: (value: any, key: bigint) => boolean,
) { ) {
return cache[table].filter(callback); return cache[table].filter(callback);
} }
+10 -5
View File
@@ -3,21 +3,25 @@ import { cacheHandlers } from "../../cache.ts";
import { Channel } from "../../types/channels/channel.ts"; import { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelDelete(data: DiscordGatewayPayload) { export async function handleChannelDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel; const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id); const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id),
);
if (!cachedChannel) return; if (!cachedChannel) return;
if ( if (
cachedChannel.type === DiscordChannelTypes.GuildVoice && payload.guildId cachedChannel.type === DiscordChannelTypes.GuildVoice && payload.guildId
) { ) {
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get("guilds", cachedChannel.guildId);
if (guild) { if (guild) {
return Promise.all(guild.voiceStates.map(async (vs, key) => { return Promise.all(guild.voiceStates.map(async (vs, key) => {
if (vs.channelId !== payload.id) return; if (vs.channelId !== cachedChannel.id) return;
// Since this channel was deleted all voice states for this channel should be deleted // Since this channel was deleted all voice states for this channel should be deleted
guild.voiceStates.delete(key); guild.voiceStates.delete(key);
@@ -38,18 +42,19 @@ export async function handleChannelDelete(data: DiscordGatewayPayload) {
DiscordChannelTypes.GuildNews, DiscordChannelTypes.GuildNews,
].includes(payload.type) ].includes(payload.type)
) { ) {
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => { cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.( eventHandlers.debug?.(
"loop", "loop",
`Running forEach messages loop in CHANNEL_DELTE file.`, `Running forEach messages loop in CHANNEL_DELTE file.`,
); );
if (message.channelId === payload.id) { if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id); cacheHandlers.delete("messages", message.id);
} }
}); });
} }
await cacheHandlers.delete("channels", payload.id); await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
eventHandlers.channelDelete?.(cachedChannel); eventHandlers.channelDelete?.(cachedChannel);
} }
+6 -2
View File
@@ -2,15 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { ChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts"; import { ChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) { export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as ChannelPinsUpdate; const payload = data.d as ChannelPinsUpdate;
const channel = await cacheHandlers.get("channels", payload.channelId); const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return; if (!channel) return;
const guild = payload.guildId const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId) ? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined; : undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp); eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp);
+5 -1
View File
@@ -3,10 +3,14 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { Channel } from "../../types/channels/channel.ts"; import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelUpdate(data: DiscordGatewayPayload) { export async function handleChannelUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Channel; const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id); const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id),
);
if (!cachedChannel) return; if (!cachedChannel) return;
const discordenoChannel = await structures.createDiscordenoChannel(payload); const discordenoChannel = await structures.createDiscordenoChannel(payload);
+7 -3
View File
@@ -2,19 +2,23 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { GuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts"; import { GuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts"; import { Collection } from "../../util/collection.ts";
export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) { export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildEmojisUpdate; const payload = data.d as GuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const cachedEmojis = guild.emojis; const cachedEmojis = guild.emojis;
guild.emojis = new Collection( guild.emojis = new Collection(
payload.emojis.map((emoji) => [emoji.id!, emoji]), payload.emojis.map((emoji) => [snowflakeToBigint(emoji.id!), emoji]),
); );
await cacheHandlers.set("guilds", payload.guildId, guild); await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.guildEmojisUpdate?.( eventHandlers.guildEmojisUpdate?.(
guild, guild,
+9 -2
View File
@@ -2,12 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts"; import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanAdd(data: DiscordGatewayPayload) { export async function handleGuildBanAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove; const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id); const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildBanAdd?.(guild, payload.user, member); eventHandlers.guildBanAdd?.(guild, payload.user, member);
} }
+9 -2
View File
@@ -2,12 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts"; import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanRemove(data: DiscordGatewayPayload) { export async function handleGuildBanRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove; const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id); const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildBanRemove?.(guild, payload.user, member); eventHandlers.guildBanRemove?.(guild, payload.user, member);
} }
+10 -9
View File
@@ -3,6 +3,7 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Guild } from "../../types/guilds/guild.ts"; import { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate( export async function handleGuildCreate(
@@ -11,24 +12,24 @@ export async function handleGuildCreate(
) { ) {
const payload = data.d as Guild; const payload = data.d as Guild;
// When shards resume they emit GUILD_CREATE again. // When shards resume they emit GUILD_CREATE again.
if (await cacheHandlers.has("guilds", payload.id)) return; if (await cacheHandlers.has("guilds", snowflakeToBigint(payload.id))) return;
const discordenoGuild = await structures.createDiscordenoGuild( const guild = await structures.createDiscordenoGuild(
payload, payload,
shardId, shardId,
); );
await cacheHandlers.set("guilds", discordenoGuild.id, discordenoGuild); await cacheHandlers.set("guilds", guild.id, guild);
const shard = ws.shards.get(shardId); const shard = ws.shards.get(shardId);
if (shard?.unavailableGuildIds.has(payload.id)) { if (shard?.unavailableGuildIds.has(guild.id)) {
await cacheHandlers.delete("unavailableGuilds", payload.id); await cacheHandlers.delete("unavailableGuilds", guild.id);
shard.unavailableGuildIds.delete(payload.id); shard.unavailableGuildIds.delete(guild.id);
return eventHandlers.guildAvailable?.(discordenoGuild); return eventHandlers.guildAvailable?.(guild);
} }
if (!cache.isReady) return eventHandlers.guildLoaded?.(discordenoGuild); if (!cache.isReady) return eventHandlers.guildLoaded?.(guild);
eventHandlers.guildCreate?.(discordenoGuild); eventHandlers.guildCreate?.(guild);
} }
+12 -8
View File
@@ -2,6 +2,7 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts"; import { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete( export async function handleGuildDelete(
@@ -10,16 +11,19 @@ export async function handleGuildDelete(
) { ) {
const payload = data.d as UnavailableGuild; const payload = data.d as UnavailableGuild;
const guild = await cacheHandlers.get("guilds", payload.id); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id),
);
if (!guild) return; if (!guild) return;
await cacheHandlers.delete("guilds", payload.id); await cacheHandlers.delete("guilds", guild.id);
if (payload.unavailable) { if (payload.unavailable) {
const shard = ws.shards.get(shardId); const shard = ws.shards.get(shardId);
if (shard) shard.unavailableGuildIds.add(payload.id); if (shard) shard.unavailableGuildIds.add(guild.id);
await cacheHandlers.set("unavailableGuilds", payload.id, Date.now()); await cacheHandlers.set("unavailableGuilds", guild.id, Date.now());
eventHandlers.guildUnavailable?.(guild); eventHandlers.guildUnavailable?.(guild);
} else { } else {
@@ -31,7 +35,7 @@ export async function handleGuildDelete(
"loop", "loop",
`1. Running forEach messages loop in CHANNEL_DELTE file.`, `1. Running forEach messages loop in CHANNEL_DELTE file.`,
); );
if (message.guildId === payload.id) { if (message.guildId === guild.id) {
cacheHandlers.delete("messages", message.id); cacheHandlers.delete("messages", message.id);
} }
}); });
@@ -41,7 +45,7 @@ export async function handleGuildDelete(
"loop", "loop",
`2. Running forEach channels loop in CHANNEL_DELTE file.`, `2. Running forEach channels loop in CHANNEL_DELTE file.`,
); );
if (channel.guildId === payload.id) { if (channel.guildId === guild.id) {
cacheHandlers.delete("channels", channel.id); cacheHandlers.delete("channels", channel.id);
} }
}); });
@@ -51,9 +55,9 @@ export async function handleGuildDelete(
"loop", "loop",
`3. Running forEach members loop in CHANNEL_DELTE file.`, `3. Running forEach members loop in CHANNEL_DELTE file.`,
); );
if (!member.guilds.has(payload.id)) return; if (!member.guilds.has(guild.id)) return;
member.guilds.delete(payload.id); member.guilds.delete(guild.id);
if (!member.guilds.size) { if (!member.guilds.size) {
return cacheHandlers.delete("members", member.id); return cacheHandlers.delete("members", member.id);
@@ -2,13 +2,17 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts"; import { GuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildIntegrationsUpdate( export async function handleGuildIntegrationsUpdate(
data: DiscordGatewayPayload, data: DiscordGatewayPayload,
) { ) {
const payload = data.d as GuildIntegrationsUpdate; const payload = data.d as GuildIntegrationsUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
eventHandlers.guildIntegrationsUpdate?.(guild); eventHandlers.guildIntegrationsUpdate?.(guild);
+7 -2
View File
@@ -3,13 +3,18 @@ import { cacheHandlers } from "../../cache.ts";
import { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts"; import { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Guild } from "../../types/guilds/guild.ts"; import { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildUpdate(data: DiscordGatewayPayload) { export async function handleGuildUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Guild; const payload = data.d as Guild;
const newGuild = await cacheHandlers.get("guilds", payload.id); const newGuild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id),
);
if (!newGuild) return; if (!newGuild) return;
const keysToSkip = [ const keysToSkip = [
"id",
"roles", "roles",
"guildHashes", "guildHashes",
"guildId", "guildId",
@@ -40,7 +45,7 @@ export async function handleGuildUpdate(data: DiscordGatewayPayload) {
} }
}).filter((change) => change) as GuildUpdateChange[]; }).filter((change) => change) as GuildUpdateChange[];
await cacheHandlers.set("guilds", payload.id, newGuild); await cacheHandlers.set("guilds", newGuild.id, newGuild);
eventHandlers.guildUpdate?.(newGuild, changes); eventHandlers.guildUpdate?.(newGuild, changes);
} }
@@ -4,13 +4,14 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts"; import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Interaction } from "../../types/interactions/interaction.ts"; import { Interaction } from "../../types/interactions/interaction.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) { export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as Interaction; const payload = data.d as Interaction;
const discordenoMember = payload.guildId const discordenoMember = payload.guildId
? await structures.createDiscordenoMember( ? await structures.createDiscordenoMember(
payload.member as GuildMemberWithUser, payload.member as GuildMemberWithUser,
payload.guildId, snowflakeToBigint(payload.guildId),
) )
: undefined; : undefined;
if (discordenoMember) { if (discordenoMember) {
+5 -2
View File
@@ -2,16 +2,19 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMembersChunk } from "../../types/members/guild_members_chunk.ts"; import { GuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts"; import { Collection } from "../../util/collection.ts";
export async function handleGuildMembersChunk(data: DiscordGatewayPayload) { export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const payload = data.d as GuildMembersChunk; const payload = data.d as GuildMembersChunk;
const guildId = snowflakeToBigint(payload.guildId);
const members = await Promise.all( const members = await Promise.all(
payload.members.map(async (member) => { payload.members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember( const discordenoMember = await structures.createDiscordenoMember(
member, member,
payload.guildId, guildId,
); );
await cacheHandlers.set("members", discordenoMember.id, discordenoMember); await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -36,7 +39,7 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
return resolve( return resolve(
await cacheHandlers.filter( await cacheHandlers.filter(
"members", "members",
(m) => m.guilds.has(payload.guildId), (m) => m.guilds.has(guildId),
), ),
); );
} }
+6 -2
View File
@@ -3,16 +3,20 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberAdd } from "../../types/members/guild_member_add.ts"; import { GuildMemberAdd } from "../../types/members/guild_member_add.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberAdd(data: DiscordGatewayPayload) { export async function handleGuildMemberAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberAdd; const payload = data.d as GuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
guild.memberCount++; guild.memberCount++;
const discordenoMember = await structures.createDiscordenoMember( const discordenoMember = await structures.createDiscordenoMember(
payload, payload,
payload.guildId, guild.id,
); );
await cacheHandlers.set("members", discordenoMember.id, discordenoMember); await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
+9 -2
View File
@@ -2,14 +2,21 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberRemove } from "../../types/members/guild_member_remove.ts"; import { GuildMemberRemove } from "../../types/members/guild_member_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberRemove(data: DiscordGatewayPayload) { export async function handleGuildMemberRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberRemove; const payload = data.d as GuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
guild.memberCount--; guild.memberCount--;
const member = await cacheHandlers.get("members", payload.user.id); const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildMemberRemove?.(guild, payload.user, member); eventHandlers.guildMemberRemove?.(guild, payload.user, member);
member?.guilds.delete(guild.id); member?.guilds.delete(guild.id);
+18 -7
View File
@@ -3,14 +3,21 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberUpdate } from "../../types/members/guild_member_update.ts"; import { GuildMemberUpdate } from "../../types/members/guild_member_update.ts";
import { bigintToSnowflake, snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) { export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberUpdate; const payload = data.d as GuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const cachedMember = await cacheHandlers.get("members", payload.user.id); const cachedMember = await cacheHandlers.get(
const guildMember = cachedMember?.guilds.get(payload.guildId); "members",
snowflakeToBigint(payload.user.id),
);
const guildMember = cachedMember?.guilds.get(guild.id);
const newMemberData = { const newMemberData = {
...payload, ...payload,
@@ -23,7 +30,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
}; };
const discordenoMember = await structures.createDiscordenoMember( const discordenoMember = await structures.createDiscordenoMember(
newMemberData, newMemberData,
payload.guildId, guild.id,
); );
await cacheHandlers.set("members", discordenoMember.id, discordenoMember); await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -48,7 +55,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
"loop", "loop",
`1. Running forEach loop in GUILD_MEMBER_UPDATE file.`, `1. Running forEach loop in GUILD_MEMBER_UPDATE file.`,
); );
if (!payload.roles.includes(id)) { if (!payload.roles.includes(bigintToSnowflake(id))) {
eventHandlers.roleLost?.(guild, discordenoMember, id); eventHandlers.roleLost?.(guild, discordenoMember, id);
} }
}); });
@@ -58,8 +65,12 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
"loop", "loop",
`2. Running forEach loop in GUILD_MEMBER_UPDATE file.`, `2. Running forEach loop in GUILD_MEMBER_UPDATE file.`,
); );
if (!roleIds.includes(id)) { if (!roleIds.includes(snowflakeToBigint(id))) {
eventHandlers.roleGained?.(guild, discordenoMember, id); eventHandlers.roleGained?.(
guild,
discordenoMember,
snowflakeToBigint(id),
);
} }
}); });
} }
+9 -5
View File
@@ -4,14 +4,18 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts"; import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Message } from "../../types/messages/message.ts"; import { Message } from "../../types/messages/message.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageCreate(data: DiscordGatewayPayload) { export async function handleMessageCreate(data: DiscordGatewayPayload) {
const payload = data.d as Message; const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId); const channel = await cacheHandlers.get(
if (channel) channel.lastMessageId = payload.id; "channels",
snowflakeToBigint(payload.channelId),
);
if (channel) channel.lastMessageId = snowflakeToBigint(payload.id);
const guild = payload.guildId const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId) ? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined; : undefined;
if (payload.member && guild) { if (payload.member && guild) {
@@ -34,7 +38,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
return cacheHandlers.set( return cacheHandlers.set(
"members", "members",
mention.id, snowflakeToBigint(mention.id),
discordenoMember, discordenoMember,
); );
} }
@@ -45,7 +49,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
data.d as Message, data.d as Message,
); );
// Cache the message // Cache the message
await cacheHandlers.set("messages", payload.id, message); await cacheHandlers.set("messages", snowflakeToBigint(payload.id), message);
eventHandlers.messageCreate?.(message); eventHandlers.messageCreate?.(message);
} }
+7 -3
View File
@@ -2,16 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageDelete } from "../../types/messages/message_delete.ts"; import { MessageDelete } from "../../types/messages/message_delete.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDelete(data: DiscordGatewayPayload) { export async function handleMessageDelete(data: DiscordGatewayPayload) {
const payload = data.d as MessageDelete; const payload = data.d as MessageDelete;
const channel = await cacheHandlers.get("channels", payload.channelId); const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return; if (!channel) return;
eventHandlers.messageDelete?.( eventHandlers.messageDelete?.(
{ id: payload.id, channel }, { id: payload.id, channel },
await cacheHandlers.get("messages", payload.id), await cacheHandlers.get("messages", snowflakeToBigint(payload.id)),
); );
await cacheHandlers.delete("messages", payload.id); await cacheHandlers.delete("messages", snowflakeToBigint(payload.id));
} }
+7 -3
View File
@@ -2,17 +2,21 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts"; import { MessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) { export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) {
const payload = data.d as MessageDeleteBulk; const payload = data.d as MessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channelId); const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return; if (!channel) return;
return Promise.all(payload.ids.map(async (id) => { return Promise.all(payload.ids.map(async (id) => {
eventHandlers.messageDelete?.( eventHandlers.messageDelete?.(
{ id, channel }, { id, channel },
await cacheHandlers.get("messages", id), await cacheHandlers.get("messages", snowflakeToBigint(id)),
); );
await cacheHandlers.delete("messages", id); await cacheHandlers.delete("messages", snowflakeToBigint(id));
})); }));
} }
+15 -4
View File
@@ -5,11 +5,15 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { import {
MessageReactionAdd, MessageReactionAdd,
} from "../../types/messages/message_reaction_add.ts"; } from "../../types/messages/message_reaction_add.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts"; import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionAdd(data: DiscordGatewayPayload) { export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionAdd; const payload = data.d as MessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.messageId); const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message) { if (message) {
const reactionExisted = message.reactions?.find( const reactionExisted = message.reactions?.find(
@@ -22,7 +26,7 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
else { else {
const newReaction = { const newReaction = {
count: 1, count: 1,
me: payload.userId === botId, me: snowflakeToBigint(payload.userId) === botId,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined }, emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
}; };
message.reactions = message.reactions message.reactions = message.reactions
@@ -30,11 +34,18 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
: [newReaction]; : [newReaction];
} }
await cacheHandlers.set("messages", payload.messageId, message); await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message,
);
} }
if (payload.member && payload.guildId) { if (payload.member && payload.guildId) {
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (guild) { if (guild) {
const discordenoMember = await structures.createDiscordenoMember( const discordenoMember = await structures.createDiscordenoMember(
payload.member, payload.member,
@@ -4,12 +4,16 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { import {
MessageReactionRemove, MessageReactionRemove,
} from "../../types/messages/message_reaction_remove.ts"; } from "../../types/messages/message_reaction_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemove( export async function handleMessageReactionRemove(
data: DiscordGatewayPayload, data: DiscordGatewayPayload,
) { ) {
const payload = data.d as MessageReactionRemove; const payload = data.d as MessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.messageId); const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message) { if (message) {
const reaction = message.reactions?.find((reaction) => const reaction = message.reactions?.find((reaction) =>
@@ -25,7 +29,7 @@ export async function handleMessageReactionRemove(
} }
if (!message.reactions?.length) message.reactions = undefined; if (!message.reactions?.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message); await cacheHandlers.set("messages", message.id, message);
} }
} }
@@ -4,17 +4,25 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { import {
MessageReactionRemoveAll, MessageReactionRemoveAll,
} from "../../types/messages/message_reaction_remove_all.ts"; } from "../../types/messages/message_reaction_remove_all.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveAll( export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload, data: DiscordGatewayPayload,
) { ) {
const payload = data.d as MessageReactionRemoveAll; const payload = data.d as MessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.messageId); const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message?.reactions) { if (message?.reactions) {
message.reactions = undefined; message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message); await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message,
);
} }
eventHandlers.reactionRemoveAll?.( eventHandlers.reactionRemoveAll?.(
@@ -2,12 +2,16 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts"; import { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveEmoji( export async function handleMessageReactionRemoveEmoji(
data: DiscordGatewayPayload, data: DiscordGatewayPayload,
) { ) {
const payload = data.d as MessageReactionRemoveEmoji; const payload = data.d as MessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.messageId); const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message?.reactions) { if (message?.reactions) {
message.reactions = message.reactions.filter( message.reactions = message.reactions.filter(
@@ -21,13 +25,13 @@ export async function handleMessageReactionRemoveEmoji(
if (!message.reactions.length) message.reactions = undefined; if (!message.reactions.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message); await cacheHandlers.set("messages", message.id, message);
} }
eventHandlers.reactionRemoveEmoji?.( eventHandlers.reactionRemoveEmoji?.(
payload.emoji, payload.emoji,
payload.messageId, snowflakeToBigint(payload.messageId),
payload.channelId, snowflakeToBigint(payload.channelId),
payload.guildId, payload.guildId ? snowflakeToBigint(payload.guildId) : undefined,
); );
} }
+10 -3
View File
@@ -3,13 +3,20 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Message } from "../../types/messages/message.ts"; import { Message } from "../../types/messages/message.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageUpdate(data: DiscordGatewayPayload) { export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Message; const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId); const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return; if (!channel) return;
const oldMessage = await cacheHandlers.get("messages", payload.id); const oldMessage = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.id),
);
if (!oldMessage) return; if (!oldMessage) return;
// Messages with embeds can trigger update but they wont have edited_timestamp // Messages with embeds can trigger update but they wont have edited_timestamp
@@ -22,7 +29,7 @@ export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const message = await structures.createDiscordenoMessage(payload); const message = await structures.createDiscordenoMessage(payload);
await cacheHandlers.set("messages", payload.id, message); await cacheHandlers.set("messages", snowflakeToBigint(payload.id), message);
eventHandlers.messageUpdate?.(message, oldMessage); eventHandlers.messageUpdate?.(message, oldMessage);
} }
+10 -2
View File
@@ -2,12 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { PresenceUpdate } from "../../types/misc/presence_update.ts"; import { PresenceUpdate } from "../../types/misc/presence_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) { export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = data.d as PresenceUpdate; const payload = data.d as PresenceUpdate;
const oldPresence = await cacheHandlers.get("presences", payload.user.id); const oldPresence = await cacheHandlers.get(
await cacheHandlers.set("presences", payload.user.id, payload); "presences",
snowflakeToBigint(payload.user.id),
);
await cacheHandlers.set(
"presences",
snowflakeToBigint(payload.user.id),
payload,
);
eventHandlers.presenceUpdate?.(payload, oldPresence); eventHandlers.presenceUpdate?.(payload, oldPresence);
} }
+4 -1
View File
@@ -5,6 +5,7 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Ready } from "../../types/gateway/ready.ts"; import { Ready } from "../../types/gateway/ready.ts";
import { GuildMemberWithUser } from "../../types/mod.ts"; import { GuildMemberWithUser } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
export function handleReady( export function handleReady(
@@ -29,7 +30,9 @@ export function handleReady(
// Set ready to false just to go sure // Set ready to false just to go sure
shard.ready = false; shard.ready = false;
// All guilds are unavailable at first // All guilds are unavailable at first
shard.unavailableGuildIds = new Set(payload.guilds.map((g) => g.id)); shard.unavailableGuildIds = new Set(
payload.guilds.map((g) => snowflakeToBigint(g.id)),
);
// Start ready check in 2 seconds // Start ready check in 2 seconds
setTimeout(async () => { setTimeout(async () => {
+6 -2
View File
@@ -2,11 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { User } from "../../types/users/user.ts"; import { User } from "../../types/users/user.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleUserUpdate(data: DiscordGatewayPayload) { export async function handleUserUpdate(data: DiscordGatewayPayload) {
const userData = data.d as User; const userData = data.d as User;
const member = await cacheHandlers.get("members", userData.id); const member = await cacheHandlers.get(
"members",
snowflakeToBigint(userData.id),
);
if (!member) return; if (!member) return;
Object.entries(userData).forEach(([key, value]) => { Object.entries(userData).forEach(([key, value]) => {
@@ -18,7 +22,7 @@ export async function handleUserUpdate(data: DiscordGatewayPayload) {
if (member[key] !== value) return member[key] = value; if (member[key] !== value) return member[key] = value;
}); });
await cacheHandlers.set("members", userData.id, member); await cacheHandlers.set("members", snowflakeToBigint(userData.id), member);
eventHandlers.botUpdate?.(userData); eventHandlers.botUpdate?.(userData);
} }
+11 -4
View File
@@ -3,15 +3,22 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleCreate } from "../../types/mod.ts"; import { GuildRoleCreate } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) { export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleCreate; const payload = data.d as GuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const role = await structures.createDiscordenoRole(payload); const role = await structures.createDiscordenoRole({
guild.roles = guild.roles.set(payload.role.id, role); ...payload,
await cacheHandlers.set("guilds", payload.guildId, guild); guildId: guild.id,
});
guild.roles = guild.roles.set(snowflakeToBigint(payload.role.id), role);
await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.roleCreate?.(guild, role); eventHandlers.roleCreate?.(guild, role);
} }
+11 -5
View File
@@ -2,14 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleDelete } from "../../types/guilds/guild_role_delete.ts"; import { GuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleDelete(data: DiscordGatewayPayload) { export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleDelete; const payload = data.d as GuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const cachedRole = guild.roles.get(payload.roleId)!; const roleId = snowflakeToBigint(payload.roleId);
guild.roles.delete(payload.roleId);
const cachedRole = guild.roles.get(roleId)!;
guild.roles.delete(roleId);
if (cachedRole) eventHandlers.roleDelete?.(guild, cachedRole); if (cachedRole) eventHandlers.roleDelete?.(guild, cachedRole);
@@ -28,9 +34,9 @@ export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
`2. Running forEach loop in CHANNEL_DELTE file.`, `2. Running forEach loop in CHANNEL_DELTE file.`,
); );
// Member does not have this role // Member does not have this role
if (!g.roles.includes(payload.roleId)) return; if (!g.roles.includes(roleId)) return;
// Remove this role from the members cache // Remove this role from the members cache
g.roles = g.roles.filter((id) => id !== payload.roleId); g.roles = g.roles.filter((id) => id !== roleId);
cacheHandlers.set("members", member.id, member); cacheHandlers.set("members", member.id, member);
}); });
}); });
+11 -4
View File
@@ -3,17 +3,24 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleUpdate } from "../../types/mod.ts"; import { GuildRoleUpdate } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) { export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleUpdate; const payload = data.d as GuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const cachedRole = guild.roles.get(payload.role.id); const cachedRole = guild.roles.get(snowflakeToBigint(payload.role.id));
if (!cachedRole) return; if (!cachedRole) return;
const role = await structures.createDiscordenoRole(payload); const role = await structures.createDiscordenoRole({
guild.roles.set(payload.role.id, role); ...payload,
guildId: guild.id,
});
guild.roles.set(snowflakeToBigint(payload.role.id), role);
await cacheHandlers.set("guilds", guild.id, guild); await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.roleUpdate?.(guild, role, cachedRole); eventHandlers.roleUpdate?.(guild, role, cachedRole);
+5 -1
View File
@@ -2,11 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { VoiceServerUpdate } from "../../types/voice/voice_server_update.ts"; import { VoiceServerUpdate } from "../../types/voice/voice_server_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) { export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceServerUpdate; const payload = data.d as VoiceServerUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
eventHandlers.voiceServerUpdate?.(payload, guild); eventHandlers.voiceServerUpdate?.(payload, guild);
+20 -11
View File
@@ -3,13 +3,16 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { VoiceState } from "../../types/voice/voice_state.ts"; import { VoiceState } from "../../types/voice/voice_state.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) { export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceState; const payload = data.d as VoiceState;
if (!payload.guildId) return; if (!payload.guildId) return;
const guild = await cacheHandlers.get("guilds", payload.guildId); const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return; if (!guild) return;
const member = payload.member const member = payload.member
@@ -17,34 +20,40 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
payload.member, payload.member,
guild.id, guild.id,
) )
: await cacheHandlers.get("members", payload.userId); : await cacheHandlers.get("members", snowflakeToBigint(payload.userId));
if (!member) return; if (!member) return;
// No cached state before so lets make one for em // No cached state before so lets make one for em
const cachedState = guild.voiceStates.get(payload.userId); const cachedState = guild.voiceStates.get(snowflakeToBigint(payload.userId));
guild.voiceStates.set( guild.voiceStates.set(
payload.userId, snowflakeToBigint(payload.userId),
payload, await structures.createDiscordenoVoiceState(guild.id, payload),
); );
await cacheHandlers.set("guilds", payload.guildId, guild); await cacheHandlers.set("guilds", guild.id, guild);
if (cachedState?.channelId !== payload.channelId) { if (
cachedState?.channelId !==
(payload.channelId ? snowflakeToBigint(payload.channelId) : null)
) {
// Either joined or moved channels // Either joined or moved channels
if (payload.channelId) { if (payload.channelId) {
if (cachedState?.channelId) { // Was in a channel before if (cachedState?.channelId) { // Was in a channel before
eventHandlers.voiceChannelSwitch?.( eventHandlers.voiceChannelSwitch?.(
member, member,
payload.channelId, snowflakeToBigint(payload.channelId),
cachedState.channelId, cachedState.channelId,
); );
} else { // Was not in a channel before so user just joined } else { // Was not in a channel before so user just joined
eventHandlers.voiceChannelJoin?.(member, payload.channelId); eventHandlers.voiceChannelJoin?.(
member,
snowflakeToBigint(payload.channelId),
);
} }
} // Left the channel } // Left the channel
else if (cachedState?.channelId) { else if (cachedState?.channelId) {
guild.voiceStates.delete(payload.userId); guild.voiceStates.delete(snowflakeToBigint(payload.userId));
eventHandlers.voiceChannelLeave?.(member, cachedState.channelId); eventHandlers.voiceChannelLeave?.(member, cachedState.channelId);
} }
} }
+3 -2
View File
@@ -1,11 +1,12 @@
import { eventHandlers } from "../../bot.ts"; import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { WebhookUpdate } from "../../types/webhooks/webhooks_update.ts"; import { WebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export function handleWebhooksUpdate(data: DiscordGatewayPayload) { export function handleWebhooksUpdate(data: DiscordGatewayPayload) {
const options = data.d as WebhookUpdate; const options = data.d as WebhookUpdate;
eventHandlers.webhooksUpdate?.( eventHandlers.webhooksUpdate?.(
options.channelId, snowflakeToBigint(options.channelId),
options.guildId, snowflakeToBigint(options.guildId),
); );
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../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 categoryChildren(id: string) { export function categoryChildren(id: bigint) {
return cacheHandlers.filter( return cacheHandlers.filter(
"channels", "channels",
(channel) => channel.parentId === id, (channel) => channel.parentId === id,
@@ -4,9 +4,13 @@ import { PermissionStrings } from "../../types/permissions/permission_strings.ts
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */ /** Checks if a channel overwrite for a user id or a role id has permission in this channel */
export function channelOverwriteHasPermission( export function channelOverwriteHasPermission(
guildId: string, guildId: bigint,
id: string, id: bigint,
overwrites: DiscordOverwrite[], overwrites: (Omit<DiscordOverwrite, "id" | "allow" | "deny"> & {
id: bigint;
allow: bigint;
deny: bigint;
})[],
permissions: PermissionStrings[], permissions: PermissionStrings[],
) { ) {
const overwrite = overwrites.find((perm) => perm.id === id) || const overwrite = overwrites.find((perm) => perm.id === id) ||
+9 -6
View File
@@ -2,11 +2,12 @@ import { cacheHandlers } from "../../cache.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts"; import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts";
import { Errors } from "../../types/misc/errors.ts"; import { Errors } from "../../types/misc/errors.ts";
import { bigintToSnowflake } from "../../util/bigint.ts";
import { calculatePermissions } from "../../util/permissions.ts"; import { calculatePermissions } from "../../util/permissions.ts";
import { createChannel } from "./create_channel.ts"; import { createChannel } from "./create_channel.ts";
/** Create a copy of a channel */ /** Create a copy of a channel */
export async function cloneChannel(channelId: string, reason?: string) { export async function cloneChannel(channelId: bigint, reason?: string) {
const channelToClone = await cacheHandlers.get("channels", channelId); const channelToClone = await cacheHandlers.get("channels", channelId);
//Return undefined if channel is not cached //Return undefined if channel is not cached
if (!channelToClone) throw new Error(Errors.CHANNEL_NOT_FOUND); if (!channelToClone) throw new Error(Errors.CHANNEL_NOT_FOUND);
@@ -23,14 +24,16 @@ export async function cloneChannel(channelId: string, reason?: string) {
...channelToClone, ...channelToClone,
name: channelToClone.name!, name: channelToClone.name!,
topic: channelToClone.topic || undefined, topic: channelToClone.topic || undefined,
parentId: channelToClone.parentId || undefined, parentId: channelToClone.parentId
permissionOverwrites: channelToClone.permissionOverwrites?.map(( ? bigintToSnowflake(channelToClone.parentId)
: undefined,
permissionOverwrites: channelToClone.permissionOverwrites.map((
overwrite, overwrite,
) => ({ ) => ({
id: overwrite.id, id: overwrite.id.toString(),
type: overwrite.type, type: overwrite.type,
allow: calculatePermissions(overwrite.allow), allow: calculatePermissions(overwrite.allow.toString()),
deny: calculatePermissions(overwrite.deny), deny: calculatePermissions(overwrite.deny.toString()),
})), })),
}; };
+1 -1
View File
@@ -16,7 +16,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ /** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function createChannel( export async function createChannel(
guildId: string, guildId: bigint,
options?: CreateGuildChannel, options?: CreateGuildChannel,
reason?: string, reason?: string,
) { ) {
+1 -1
View File
@@ -7,7 +7,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ /** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function deleteChannel( export async function deleteChannel(
channelId: string, channelId: bigint,
reason?: string, reason?: string,
) { ) {
const channel = await cacheHandlers.get("channels", channelId); const channel = await cacheHandlers.get("channels", channelId);
@@ -4,9 +4,9 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */ /** Delete the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */
export async function deleteChannelOverwrite( export async function deleteChannelOverwrite(
guildId: string, guildId: bigint,
channelId: string, channelId: bigint,
overwriteId: string, overwriteId: bigint,
): Promise<undefined> { ): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
+5 -6
View File
@@ -20,7 +20,7 @@ import { camelKeysToSnakeCase, hasOwnProperty } from "../../util/utils.ts";
//TODO(threads): check thread perms //TODO(threads): check thread perms
/** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ /** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */
export async function editChannel( export async function editChannel(
channelId: string, channelId: bigint,
options: ModifyChannel | ModifyThread, options: ModifyChannel | ModifyThread,
reason?: string, reason?: string,
) { ) {
@@ -46,7 +46,7 @@ export async function editChannel(
permissions.add("MANAGE_THREADS"); permissions.add("MANAGE_THREADS");
} }
await requireBotChannelPermissions(channel.parentId ?? "", [ await requireBotChannelPermissions(channel.parentId ?? 0n, [
...permissions, ...permissions,
]); ]);
} }
@@ -117,15 +117,14 @@ export async function editChannel(
interface EditChannelRequest { interface EditChannelRequest {
amount: number; amount: number;
timestamp: number; timestamp: number;
channelId: string; channelId: bigint;
items: { items: {
channelId: string; channelId: bigint;
options: ModifyChannel; options: ModifyChannel;
}[]; }[];
} }
const editChannelNameTopicQueue = new Map<string, EditChannelRequest>(); const editChannelNameTopicQueue = new Map<bigint, EditChannelRequest>();
let editChannelProcessing = false; let editChannelProcessing = false;
function processEditChannelQueue() { function processEditChannelQueue() {
@@ -8,9 +8,9 @@ import {
/** Edit the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */ /** Edit the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */
export async function editChannelOverwrite( export async function editChannelOverwrite(
guildId: string, guildId: bigint,
channelId: string, channelId: bigint,
overwriteId: string, overwriteId: bigint,
options: Omit<Overwrite, "id">, options: Omit<Overwrite, "id">,
): Promise<undefined> { ): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
+2 -2
View File
@@ -5,8 +5,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */ /** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */
export async function followChannel( export async function followChannel(
sourceChannelId: string, sourceChannelId: bigint,
targetChannelId: string, targetChannelId: bigint,
) { ) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]); await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
+3 -2
View File
@@ -2,13 +2,14 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { Channel } from "../../types/channels/channel.ts"; import { Channel } from "../../types/channels/channel.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Fetches a single channel object from the api. /** Fetches a single channel object from the api.
* *
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/ */
export async function getChannel(channelId: string, addToCache = true) { export async function getChannel(channelId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel>( const result = await rest.runMethod<Channel>(
"get", "get",
endpoints.CHANNEL_BASE(channelId), endpoints.CHANNEL_BASE(channelId),
@@ -16,7 +17,7 @@ export async function getChannel(channelId: string, addToCache = true) {
const discordenoChannel = await structures.createDiscordenoChannel( const discordenoChannel = await structures.createDiscordenoChannel(
result, result,
result.guildId, result.guildId ? snowflakeToBigint(result.guildId) : undefined,
); );
if (addToCache) { if (addToCache) {
await cacheHandlers.set( await cacheHandlers.set(
+1 -1
View File
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
export async function getChannelWebhooks(channelId: string) { export async function getChannelWebhooks(channelId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]); await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod<Webhook[]>( const result = await rest.runMethod<Webhook[]>(
+1 -1
View File
@@ -9,7 +9,7 @@ import { endpoints } from "../../util/constants.ts";
* *
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/ */
export async function getChannels(guildId: string, addToCache = true) { export async function getChannels(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel[]>( const result = await rest.runMethod<Channel[]>(
"get", "get",
endpoints.GUILD_CHANNELS(guildId), endpoints.GUILD_CHANNELS(guildId),
+1 -1
View File
@@ -4,7 +4,7 @@ import { Message } from "../../types/messages/message.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */ /** Get pinned messages in this channel. */
export async function getPins(channelId: string) { export async function getPins(channelId: bigint) {
const result = await rest.runMethod<Message[]>( const result = await rest.runMethod<Message[]>(
"get", "get",
endpoints.CHANNEL_PINS(channelId), endpoints.CHANNEL_PINS(channelId),
+1 -1
View File
@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts"; import { cacheHandlers } from "../../cache.ts";
/** Checks whether a channel is synchronized with its parent/category channel or not. */ /** Checks whether a channel is synchronized with its parent/category channel or not. */
export async function isChannelSynced(channelId: string) { export async function isChannelSynced(channelId: bigint) {
const channel = await cacheHandlers.get("channels", channelId); const channel = await cacheHandlers.get("channels", channelId);
if (!channel?.parentId) return false; if (!channel?.parentId) return false;
+1 -1
View File
@@ -10,7 +10,7 @@ import { botHasChannelPermissions } from "../../util/permissions.ts";
* However, if a bot is responding to a command and expects the computation to take a few seconds, * However, if a bot is responding to a command and expects the computation to take a few seconds,
* this endpoint may be called to let the user know that the bot is processing their message. * this endpoint may be called to let the user know that the bot is processing their message.
*/ */
export async function startTyping(channelId: string) { export async function startTyping(channelId: bigint) {
const channel = await cacheHandlers.get("channels", channelId); const channel = await cacheHandlers.get("channels", channelId);
// If the channel is cached, we can do extra checks/safety // If the channel is cached, we can do extra checks/safety
if (channel) { if (channel) {
+1 -1
View File
@@ -4,7 +4,7 @@ import { endpoints } from "../../util/constants.ts";
/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */ /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
export async function swapChannels( export async function swapChannels(
guildId: string, guildId: bigint,
channelPositions: ModifyGuildChannelPositions[], channelPositions: ModifyGuildChannelPositions[],
) { ) {
if (channelPositions.length < 2) { if (channelPositions.length < 2) {
@@ -6,7 +6,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */ /** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */
export async function batchEditSlashCommandPermissions( export async function batchEditSlashCommandPermissions(
guildId: string, guildId: bigint,
options: { id: string; permissions: ApplicationCommandPermissions[] }[], options: { id: string; permissions: ApplicationCommandPermissions[] }[],
) { ) {
return await rest.runMethod( return await rest.runMethod(
+1 -1
View File
@@ -21,7 +21,7 @@ import {
*/ */
export async function createSlashCommand( export async function createSlashCommand(
options: CreateGlobalApplicationCommand, options: CreateGlobalApplicationCommand,
guildId?: string, guildId?: bigint,
) { ) {
validateSlashCommands([options], true); validateSlashCommands([options], true);
+2 -2
View File
@@ -4,8 +4,8 @@ import { endpoints } from "../../util/constants.ts";
/** Deletes a slash command. */ /** Deletes a slash command. */
export async function deleteSlashCommand( export async function deleteSlashCommand(
id: string, id: bigint,
guildId?: string, guildId?: bigint,
) { ) {
return await rest.runMethod<undefined>( return await rest.runMethod<undefined>(
"delete", "delete",
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
/** To delete your response to a slash command. If a message id is not provided, it will default to deleting the original response. */ /** To delete your response to a slash command. If a message id is not provided, it will default to deleting the original response. */
export async function deleteSlashResponse( export async function deleteSlashResponse(
token: string, token: string,
messageId?: string, messageId?: bigint,
) { ) {
return await rest.runMethod<undefined>( return await rest.runMethod<undefined>(
"delete", "delete",
@@ -6,8 +6,8 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Edits command permissions for a specific command for your application in a guild. */ /** Edits command permissions for a specific command for your application in a guild. */
export async function editSlashCommandPermissions( export async function editSlashCommandPermissions(
guildId: string, guildId: bigint,
commandId: string, commandId: bigint,
options: ApplicationCommandPermissions[], options: ApplicationCommandPermissions[],
) { ) {
return await rest.runMethod( return await rest.runMethod(
+1 -1
View File
@@ -4,7 +4,7 @@ import { ApplicationCommand } from "../../types/interactions/application_command
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Fetchs the global command for the given Id. If a guildId is provided, the guild command will be fetched. */ /** Fetchs the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
export async function getSlashCommand(commandId: string, guildId?: string) { export async function getSlashCommand(commandId: bigint, guildId?: bigint) {
return await rest.runMethod<ApplicationCommand>( return await rest.runMethod<ApplicationCommand>(
"get", "get",
guildId guildId
@@ -5,8 +5,8 @@ import { endpoints } from "../../util/constants.ts";
/** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */ /** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */
export async function getSlashCommandPermission( export async function getSlashCommandPermission(
guildId: string, guildId: bigint,
commandId: string, commandId: bigint,
) { ) {
return await rest.runMethod<GuildApplicationCommandPermissions>( return await rest.runMethod<GuildApplicationCommandPermissions>(
"get", "get",
@@ -4,7 +4,7 @@ import { GuildApplicationCommandPermissions } from "../../types/interactions/gui
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */ /** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */
export async function getSlashCommandPermissions(guildId: string) { export async function getSlashCommandPermissions(guildId: bigint) {
return await rest.runMethod<GuildApplicationCommandPermissions[]>( return await rest.runMethod<GuildApplicationCommandPermissions[]>(
"get", "get",
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId), endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
+1 -1
View File
@@ -5,7 +5,7 @@ import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Fetch all of the global commands for your application. */ /** Fetch all of the global commands for your application. */
export async function getSlashCommands(guildId?: string) { export async function getSlashCommands(guildId?: bigint) {
const result = await rest.runMethod<ApplicationCommand[]>( const result = await rest.runMethod<ApplicationCommand[]>(
"get", "get",
guildId guildId
@@ -11,7 +11,7 @@ import { endpoints } from "../../util/constants.ts";
* NOTE: By default we will suppress mentions. To enable mentions, just pass any mentions object. * NOTE: By default we will suppress mentions. To enable mentions, just pass any mentions object.
*/ */
export async function sendInteractionResponse( export async function sendInteractionResponse(
id: string, id: bigint,
token: string, token: string,
options: DiscordenoInteractionResponse, options: DiscordenoInteractionResponse,
) { ) {
@@ -27,7 +27,7 @@ export async function sendInteractionResponse(
} }
// Expire in 15 minutes // Expire in 15 minutes
cache.executedSlashCommands.set(token, id); cache.executedSlashCommands.add(token);
setTimeout( setTimeout(
() => { () => {
eventHandlers.debug?.( eventHandlers.debug?.(
+2 -2
View File
@@ -9,9 +9,9 @@ import { validateSlashCommands } from "../../util/utils.ts";
* Edit an existing slash command. If this command did not exist, it will create it. * Edit an existing slash command. If this command did not exist, it will create it.
*/ */
export async function upsertSlashCommand( export async function upsertSlashCommand(
commandId: string, commandId: bigint,
options: EditGlobalApplicationCommand, options: EditGlobalApplicationCommand,
guildId?: string, guildId?: bigint,
) { ) {
validateSlashCommands([options]); validateSlashCommands([options]);
@@ -12,7 +12,7 @@ import { validateSlashCommands } from "../../util/utils.ts";
*/ */
export async function upsertSlashCommands( export async function upsertSlashCommands(
options: EditGlobalApplicationCommand[], options: EditGlobalApplicationCommand[],
guildId?: string, guildId?: bigint,
) { ) {
validateSlashCommands(options); validateSlashCommands(options);
@@ -7,7 +7,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */ /** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */
export async function addDiscoverySubcategory( export async function addDiscoverySubcategory(
guildId: string, guildId: bigint,
categoryId: number, categoryId: number,
) { ) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
+1 -1
View File
@@ -7,7 +7,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */ /** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */
export async function editDiscovery( export async function editDiscovery(
guildId: string, guildId: bigint,
data: ModifyGuildDiscoveryMetadata, data: ModifyGuildDiscoveryMetadata,
) { ) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
@@ -4,7 +4,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Removes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */ /** Removes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */
export async function removeDiscoverySubcategory( export async function removeDiscoverySubcategory(
guildId: string, guildId: bigint,
categoryId: number, categoryId: number,
) { ) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
+16 -6
View File
@@ -1,13 +1,14 @@
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts"; import { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts";
import { Emoji } from "../../types/emojis/emoji.ts"; import { Emoji } from "../../types/emojis/emoji.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { urlToBase64 } from "../../util/utils.ts"; import { urlToBase64 } from "../../util/utils.ts";
/** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */ /** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */
export async function createEmoji( export async function createEmoji(
guildId: string, guildId: bigint,
name: string, name: string,
image: string, image: string,
options: CreateGuildEmoji, options: CreateGuildEmoji,
@@ -18,9 +19,18 @@ export async function createEmoji(
image = await urlToBase64(image); image = await urlToBase64(image);
} }
return await rest.runMethod<Emoji>("post", endpoints.GUILD_EMOJIS(guildId), { const emoji = await rest.runMethod<Emoji>(
...options, "post",
name, endpoints.GUILD_EMOJIS(guildId),
image, {
}); ...options,
name,
image,
},
);
return {
...emoji,
id: snowflakeToBigint(emoji.id!),
};
} }
+2 -2
View File
@@ -4,8 +4,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */ /** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
export async function deleteEmoji( export async function deleteEmoji(
guildId: string, guildId: bigint,
id: string, id: bigint,
reason?: string, reason?: string,
) { ) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
+2 -2
View File
@@ -6,8 +6,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
export async function editEmoji( export async function editEmoji(
guildId: string, guildId: bigint,
id: string, id: bigint,
options: ModifyGuildEmoji, options: ModifyGuildEmoji,
) { ) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
+1 -1
View File
@@ -1,4 +1,4 @@
/** Creates a url to the emoji from the Discord CDN. */ /** Creates a url to the emoji from the Discord CDN. */
export function emojiURL(id: string, animated = false) { export function emojiURL(id: bigint, animated = false) {
return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`; return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`;
} }
+2 -2
View File
@@ -10,8 +10,8 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/ */
export async function getEmoji( export async function getEmoji(
guildId: string, guildId: bigint,
emojiId: string, emojiId: bigint,
addToCache = true, addToCache = true,
) { ) {
const result = await rest.runMethod<Emoji>( const result = await rest.runMethod<Emoji>(
+3 -2
View File
@@ -3,6 +3,7 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { Emoji } from "../../types/emojis/emoji.ts"; import { Emoji } from "../../types/emojis/emoji.ts";
import { Errors } from "../../types/misc/errors.ts"; import { Errors } from "../../types/misc/errors.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts"; import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
@@ -11,7 +12,7 @@ import { endpoints } from "../../util/constants.ts";
* *
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/ */
export async function getEmojis(guildId: string, addToCache = true) { export async function getEmojis(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Emoji[]>( const result = await rest.runMethod<Emoji[]>(
"get", "get",
endpoints.GUILD_EMOJIS(guildId), endpoints.GUILD_EMOJIS(guildId),
@@ -26,7 +27,7 @@ export async function getEmojis(guildId: string, addToCache = true) {
"loop", "loop",
`Running forEach loop in get_emojis file.`, `Running forEach loop in get_emojis file.`,
); );
guild.emojis.set(emoji.id!, emoji); guild.emojis.set(snowflakeToBigint(emoji.id!), emoji);
}); });
await cacheHandlers.set("guilds", guildId, guild); await cacheHandlers.set("guilds", guildId, guild);
+1 -1
View File
@@ -2,7 +2,7 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.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. */ /** 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) { export async function deleteGuild(guildId: bigint) {
return await rest.runMethod<undefined>( return await rest.runMethod<undefined>(
"delete", "delete",
endpoints.GUILDS_BASE(guildId), endpoints.GUILDS_BASE(guildId),
+1 -1
View File
@@ -9,7 +9,7 @@ import { urlToBase64 } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
/** 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: ModifyGuild) { export async function editGuild(guildId: bigint, options: ModifyGuild) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
if (options.icon && !options.icon.startsWith("data:image/")) { if (options.icon && !options.icon.startsWith("data:image/")) {
+1 -1
View File
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts";
export async function editWelcomeScreen( export async function editWelcomeScreen(
guildId: string, guildId: bigint,
options: ModifyGuildWelcomeScreen, options: ModifyGuildWelcomeScreen,
) { ) {
return await rest.runMethod<WelcomeScreen>( return await rest.runMethod<WelcomeScreen>(
+1 -1
View File
@@ -5,7 +5,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */ /** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
export async function editWidget( export async function editWidget(
guildId: string, guildId: bigint,
enabled: boolean, enabled: boolean,
channelId?: string | null, channelId?: string | null,
) { ) {
+1 -1
View File
@@ -7,7 +7,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */ /** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
export async function getAuditLogs( export async function getAuditLogs(
guildId: string, guildId: bigint,
options: GetGuildAuditLog, options: GetGuildAuditLog,
) { ) {
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]); await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
+1 -1
View File
@@ -4,7 +4,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */ /** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */
export async function getBan(guildId: string, memberId: string) { export async function getBan(guildId: bigint, memberId: bigint) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]); await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
return await rest.runMethod<Ban>( return await rest.runMethod<Ban>(
+4 -3
View File
@@ -1,11 +1,12 @@
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { Ban } from "../../types/guilds/ban.ts"; import { Ban } from "../../types/guilds/ban.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts"; import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */ /** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
export async function getBans(guildId: string) { export async function getBans(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]); await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const results = await rest.runMethod<Ban[]>( const results = await rest.runMethod<Ban[]>(
@@ -13,7 +14,7 @@ export async function getBans(guildId: string) {
endpoints.GUILD_BANS(guildId), endpoints.GUILD_BANS(guildId),
); );
return new Collection<string, Ban>( return new Collection<bigint, Ban>(
results.map((res) => [res.user.id, res]), results.map((res) => [snowflakeToBigint(res.user.id), res]),
); );
} }
+4 -4
View File
@@ -13,7 +13,7 @@ import { ws } from "../../ws/ws.ts";
* So it does not cache the guild, you must do it manually. * So it does not cache the guild, you must do it manually.
* */ * */
export async function getGuild( export async function getGuild(
guildId: string, guildId: bigint,
options: { counts?: boolean; addToCache?: boolean } = { options: { counts?: boolean; addToCache?: boolean } = {
counts: true, counts: true,
addToCache: true, addToCache: true,
@@ -27,14 +27,14 @@ export async function getGuild(
}, },
); );
const structure = await structures.createDiscordenoGuild( const guild = await structures.createDiscordenoGuild(
result, result,
Number((BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards)), Number((BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards)),
); );
if (options.addToCache) { if (options.addToCache) {
await cacheHandlers.set("guilds", guildId, structure); await cacheHandlers.set("guilds", guild.id, guild);
} }
return structure; return guild;
} }
+1 -1
View File
@@ -3,7 +3,7 @@ import { GuildPreview } from "../../types/guilds/guild_preview.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */ /** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */
export async function getGuildPreview(guildId: string) { export async function getGuildPreview(guildId: bigint) {
return await rest.runMethod<GuildPreview>( return await rest.runMethod<GuildPreview>(
"get", "get",
endpoints.GUILD_PREVIEW(guildId), endpoints.GUILD_PREVIEW(guildId),
+1 -1
View File
@@ -7,7 +7,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */ /** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */
export async function getPruneCount( export async function getPruneCount(
guildId: string, guildId: bigint,
options?: GetGuildPruneCountQuery, options?: GetGuildPruneCountQuery,
) { ) {
if (options?.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS); if (options?.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS);
+1 -1
View File
@@ -3,7 +3,7 @@ import { InviteMetadata } from "../../types/invites/invite_metadata.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Returns the code and uses of the vanity url for this server if it is enabled else `code` will be null. Requires the `MANAGE_GUILD` permission. */ /** Returns the code and uses of the vanity url for this server if it is enabled else `code` will be null. Requires the `MANAGE_GUILD` permission. */
export async function getVanityURL(guildId: string) { export async function getVanityURL(guildId: bigint) {
return await rest.runMethod< return await rest.runMethod<
(Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">) | { (Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">) | {
code: null; code: null;
+1 -1
View File
@@ -4,7 +4,7 @@ import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */ /** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */
export async function getVoiceRegions(guildId: string) { export async function getVoiceRegions(guildId: bigint) {
const result = await rest.runMethod<VoiceRegion[]>( const result = await rest.runMethod<VoiceRegion[]>(
"get", "get",
endpoints.GUILD_REGIONS(guildId), endpoints.GUILD_REGIONS(guildId),
+1 -1
View File
@@ -2,7 +2,7 @@ import { rest } from "../../rest/rest.ts";
import { WelcomeScreen } from "../../types/mod.ts"; import { WelcomeScreen } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
export async function getWelcomeScreen(guildId: string) { export async function getWelcomeScreen(guildId: bigint) {
return await rest.runMethod<WelcomeScreen>( return await rest.runMethod<WelcomeScreen>(
"get", "get",
endpoints.GUILD_WELCOME_SCREEN(guildId), endpoints.GUILD_WELCOME_SCREEN(guildId),
+1 -1
View File
@@ -5,7 +5,7 @@ import { Errors } from "../../types/misc/errors.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Returns the widget for the guild. */ /** Returns the widget for the guild. */
export async function getWidget(guildId: string, options?: { force: boolean }) { export async function getWidget(guildId: bigint, options?: { force: boolean }) {
if (!options?.force) { if (!options?.force) {
const guild = await cacheHandlers.get("guilds", guildId); const guild = await cacheHandlers.get("guilds", guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
+1 -1
View File
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
/** Returns the widget image URL for the guild. */ /** Returns the widget image URL for the guild. */
export async function getWidgetImageURL( export async function getWidgetImageURL(
guildId: string, guildId: bigint,
options?: GetGuildWidgetImageQuery & { force?: boolean }, options?: GetGuildWidgetImageQuery & { force?: boolean },
) { ) {
if (!options?.force) { if (!options?.force) {
+1 -1
View File
@@ -4,7 +4,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Returns the guild widget object. Requires the MANAGE_GUILD permission. */ /** Returns the guild widget object. Requires the MANAGE_GUILD permission. */
export async function getWidgetSettings(guildId: string) { export async function getWidgetSettings(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<GuildWidget>( return await rest.runMethod<GuildWidget>(
+1 -1
View File
@@ -5,7 +5,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** The full URL of the banner from Discords CDN. Undefined if no banner is set. */ /** The full URL of the banner from Discords CDN. Undefined if no banner is set. */
export function guildBannerURL( export function guildBannerURL(
id: string, id: bigint,
banner?: string, banner?: string,
size: DiscordImageSize = 128, size: DiscordImageSize = 128,
format?: DiscordImageFormat, format?: DiscordImageFormat,
+1 -1
View File
@@ -5,7 +5,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** 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. */
export function guildIconURL( export function guildIconURL(
id: string, id: bigint,
icon?: string, icon?: string,
size: DiscordImageSize = 128, size: DiscordImageSize = 128,
format?: DiscordImageFormat, format?: DiscordImageFormat,
+1 -1
View File
@@ -5,7 +5,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** The full URL of the splash from Discords CDN. Undefined if no splash is set. */ /** The full URL of the splash from Discords CDN. Undefined if no splash is set. */
export function guildSplashURL( export function guildSplashURL(
id: string, id: bigint,
splash?: string, splash?: string,
size: DiscordImageSize = 128, size: DiscordImageSize = 128,
format?: DiscordImageFormat, format?: DiscordImageFormat,
+1 -1
View File
@@ -2,7 +2,7 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
/** Leave a guild */ /** Leave a guild */
export async function leaveGuild(guildId: string) { export async function leaveGuild(guildId: bigint) {
return await rest.runMethod<undefined>( return await rest.runMethod<undefined>(
"delete", "delete",
endpoints.GUILD_LEAVE(guildId), endpoints.GUILD_LEAVE(guildId),
+1 -1
View File
@@ -16,7 +16,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
* - You are able to set `request_to_speak_timestamp` to any present or future time. * - You are able to set `request_to_speak_timestamp` to any present or future time.
*/ */
export function updateBotVoiceState( export function updateBotVoiceState(
guildId: string, guildId: bigint,
data: UpdateSelfVoiceState, data: UpdateSelfVoiceState,
) { ) {
const payload = camelKeysToSnakeCase<DiscordUpdateSelfVoiceState>(data); const payload = camelKeysToSnakeCase<DiscordUpdateSelfVoiceState>(data);
@@ -16,8 +16,8 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts";
* - When suppressed, the user will have their `request_to_speak_timestamp` removed. * - When suppressed, the user will have their `request_to_speak_timestamp` removed.
*/ */
export function updateVoiceState( export function updateVoiceState(
guildId: string, guildId: bigint,
userId: string, userId: bigint,
data: UpdateOthersVoiceState, data: UpdateOthersVoiceState,
) { ) {
const payload = camelKeysToSnakeCase<DiscordUpdateOthersVoiceState>(data); const payload = camelKeysToSnakeCase<DiscordUpdateOthersVoiceState>(data);
@@ -3,7 +3,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */ /** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
export async function deleteIntegration(guildId: string, id: string) { export async function deleteIntegration(guildId: bigint, id: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<undefined>( return await rest.runMethod<undefined>(
+1 -1
View File
@@ -4,7 +4,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */ /** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
export async function getIntegrations(guildId: string) { export async function getIntegrations(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<Integration>( return await rest.runMethod<Integration>(
+1 -1
View File
@@ -7,7 +7,7 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
export async function createInvite( export async function createInvite(
channelId: string, channelId: bigint,
options: CreateChannelInvite, options: CreateChannelInvite,
) { ) {
await requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]); await requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]);
+1 -1
View File
@@ -9,7 +9,7 @@ import {
} from "../../util/permissions.ts"; } from "../../util/permissions.ts";
/** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */ /** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */
export async function deleteInvite(channelId: string, inviteCode: string) { export async function deleteInvite(channelId: bigint, inviteCode: string) {
const channel = await cacheHandlers.get("channels", channelId); const channel = await cacheHandlers.get("channels", channelId);
if (!channel) throw new Error(Errors.CHANNEL_NOT_FOUND); if (!channel) throw new Error(Errors.CHANNEL_NOT_FOUND);
+1 -1
View File
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */ /** Gets the invites for this channel. Requires MANAGE_CHANNEL */
export async function getChannelInvites(channelId: string) { export async function getChannelInvites(channelId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]); await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]);
const result = await rest.runMethod<Invite[]>( const result = await rest.runMethod<Invite[]>(
+1 -1
View File
@@ -5,7 +5,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */ /** Get all the invites for this guild. Requires MANAGE_GUILD permission */
export async function getInvites(guildId: string) { export async function getInvites(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod<Invite[]>( const result = await rest.runMethod<Invite[]>(

Some files were not shown because too many files have changed in this diff Show More