Prettified Code!

This commit is contained in:
itohatweb
2021-05-21 15:55:49 +00:00
committed by GitHub Action
parent 71a20fb0f4
commit 179add27c9
271 changed files with 889 additions and 3067 deletions
+2 -4
View File
@@ -20,16 +20,14 @@ export async function startBot(config: BotConfig) {
ws.identifyPayload.token = `Bot ${config.token}`;
rest.token = `Bot ${config.token}`;
ws.identifyPayload.intents = config.intents.reduce(
(bits, next) =>
(bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
(bits, next) => (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
0
);
// Initial API connection to get info about bots connection
ws.botGatewayData = await getGatewayBot();
ws.maxShards = ws.maxShards || ws.botGatewayData.shards;
ws.lastShardId =
ws.lastShardId === 1 ? ws.botGatewayData.shards - 1 : ws.lastShardId;
ws.lastShardId = ws.lastShardId === 1 ? ws.botGatewayData.shards - 1 : ws.lastShardId;
// Explicitly append gateway version and encoding
ws.botGatewayData.url += `?v=${GATEWAY_VERSION}&encoding=json`;
+21 -98
View File
@@ -23,19 +23,12 @@ export const cache = {
presences: new Collection<bigint, PresenceUpdate>(),
fetchAllMembersProcessingRequests: new Collection<
string,
(
value:
| Collection<bigint, DiscordenoMember>
| PromiseLike<Collection<bigint, DiscordenoMember>>
) => void
(value: Collection<bigint, DiscordenoMember> | PromiseLike<Collection<bigint, DiscordenoMember>>) => void
>(),
executedSlashCommands: new Set<string>(),
get emojis() {
return new Collection<bigint, Emoji>(
this.guilds.reduce(
(a, b) => [...a, ...b.emojis.map((e) => [e.id, e])],
[] as any[]
)
this.guilds.reduce((a, b) => [...a, ...b.emojis.map((e) => [e.id, e])], [] as any[])
);
},
};
@@ -70,83 +63,31 @@ export let cacheHandlers = {
filter,
};
export type TableName =
| "guilds"
| "unavailableGuilds"
| "channels"
| "messages"
| "members"
| "presences";
export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences";
function set(
table: "guilds",
key: bigint,
value: DiscordenoGuild
): Promise<Collection<bigint, DiscordenoGuild>>;
function set(
table: "channels",
key: bigint,
value: DiscordenoChannel
): Promise<Collection<bigint, DiscordenoChannel>>;
function set(
table: "messages",
key: bigint,
value: DiscordenoMessage
): Promise<Collection<bigint, DiscordenoMessage>>;
function set(
table: "members",
key: bigint,
value: DiscordenoMember
): Promise<Collection<bigint, DiscordenoMember>>;
function set(
table: "presences",
key: bigint,
value: PresenceUpdate
): Promise<Collection<bigint, PresenceUpdate>>;
function set(
table: "unavailableGuilds",
key: bigint,
value: number
): Promise<Collection<bigint, number>>;
function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise<Collection<bigint, DiscordenoGuild>>;
function set(table: "channels", key: bigint, value: DiscordenoChannel): Promise<Collection<bigint, DiscordenoChannel>>;
function set(table: "messages", key: bigint, value: DiscordenoMessage): Promise<Collection<bigint, DiscordenoMessage>>;
function set(table: "members", key: bigint, value: DiscordenoMember): Promise<Collection<bigint, DiscordenoMember>>;
function set(table: "presences", key: bigint, value: PresenceUpdate): Promise<Collection<bigint, PresenceUpdate>>;
function set(table: "unavailableGuilds", key: bigint, value: number): Promise<Collection<bigint, number>>;
async function set(table: TableName, key: bigint, value: any) {
return cache[table].set(key, value);
}
function get(
table: "guilds",
key: bigint
): Promise<DiscordenoGuild | undefined>;
function get(
table: "channels",
key: bigint
): Promise<DiscordenoChannel | undefined>;
function get(
table: "messages",
key: bigint
): Promise<DiscordenoMessage | undefined>;
function get(
table: "members",
key: bigint
): Promise<DiscordenoMember | undefined>;
function get(
table: "presences",
key: bigint
): Promise<PresenceUpdate | undefined>;
function get(
table: "unavailableGuilds",
key: bigint
): Promise<number | undefined>;
function get(table: "guilds", key: bigint): Promise<DiscordenoGuild | undefined>;
function get(table: "channels", key: bigint): Promise<DiscordenoChannel | undefined>;
function get(table: "messages", key: bigint): Promise<DiscordenoMessage | undefined>;
function get(table: "members", key: bigint): Promise<DiscordenoMember | undefined>;
function get(table: "presences", key: bigint): Promise<PresenceUpdate | undefined>;
function get(table: "unavailableGuilds", key: bigint): Promise<number | undefined>;
async function get(table: TableName, key: bigint) {
return cache[table].get(key);
}
function forEach(
table: "guilds",
callback: (
value: DiscordenoGuild,
key: bigint,
map: Map<bigint, DiscordenoGuild>
) => unknown
callback: (value: DiscordenoGuild, key: bigint, map: Map<bigint, DiscordenoGuild>) => unknown
): void;
function forEach(
table: "unavailableGuilds",
@@ -154,32 +95,17 @@ function forEach(
): void;
function forEach(
table: "channels",
callback: (
value: DiscordenoChannel,
key: bigint,
map: Map<bigint, DiscordenoChannel>
) => unknown
callback: (value: DiscordenoChannel, key: bigint, map: Map<bigint, DiscordenoChannel>) => unknown
): void;
function forEach(
table: "messages",
callback: (
value: DiscordenoMessage,
key: bigint,
map: Map<bigint, DiscordenoMessage>
) => unknown
callback: (value: DiscordenoMessage, key: bigint, map: Map<bigint, DiscordenoMessage>) => unknown
): void;
function forEach(
table: "members",
callback: (
value: DiscordenoMember,
key: bigint,
map: Map<bigint, DiscordenoMember>
) => unknown
callback: (value: DiscordenoMember, key: bigint, map: Map<bigint, DiscordenoMember>) => unknown
): void;
function forEach(
table: TableName,
callback: (value: any, key: bigint, map: Map<bigint, any>) => unknown
) {
function forEach(table: TableName, callback: (value: any, key: bigint, map: Map<bigint, any>) => unknown) {
return cache[table].forEach(callback);
}
@@ -203,9 +129,6 @@ function filter(
table: "members",
callback: (value: DiscordenoMember, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoMember>>;
async function filter(
table: TableName,
callback: (value: any, key: bigint) => boolean
) {
async function filter(table: TableName, callback: (value: any, key: bigint) => boolean) {
return cache[table].filter(callback);
}
+3 -12
View File
@@ -8,16 +8,10 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const cachedChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!cachedChannel) return;
if (
cachedChannel.type === DiscordChannelTypes.GuildVoice &&
payload.guildId
) {
if (cachedChannel.type === DiscordChannelTypes.GuildVoice && payload.guildId) {
const guild = await cacheHandlers.get("guilds", cachedChannel.guildId);
if (guild) {
@@ -47,10 +41,7 @@ export async function handleChannelDelete(data: DiscordGatewayPayload) {
) {
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.(
"loop",
`Running forEach messages loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `Running forEach messages loop in CHANNEL_DELTE file.`);
if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id);
}
+2 -7
View File
@@ -7,15 +7,10 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as ChannelPinsUpdate;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId)
);
const channel = await cacheHandlers.get("channels", snowflakeToBigint(payload.channelId));
if (!channel) return;
const guild = payload.guildId
? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined;
const guild = payload.guildId ? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId)) : undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp);
}
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const cachedChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!cachedChannel) return;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
+2 -8
View File
@@ -7,18 +7,12 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const cachedChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!cachedChannel) return;
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.(
"loop",
`Running forEach messages loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `Running forEach messages loop in CHANNEL_DELTE file.`);
if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id);
}
+4 -17
View File
@@ -12,28 +12,15 @@ export async function handleThreadListSync(data: DiscordGatewayPayload) {
const discordenoChannels = await Promise.all(
payload.threads.map(async (thread) => {
const discordenoChannel = await structures.createDiscordenoChannel(
thread,
snowflakeToBigint(payload.guildId)
);
const discordenoChannel = await structures.createDiscordenoChannel(thread, snowflakeToBigint(payload.guildId));
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
return discordenoChannel;
})
);
const threads = new Collection<bigint, DiscordenoChannel>(
discordenoChannels.map((t) => [t.id, t])
);
const threads = new Collection<bigint, DiscordenoChannel>(discordenoChannels.map((t) => [t.id, t]));
eventHandlers.threadListSync?.(
threads,
payload.members,
snowflakeToBigint(payload.guildId)
);
eventHandlers.threadListSync?.(threads, payload.members, snowflakeToBigint(payload.guildId));
}
@@ -6,10 +6,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadMembersUpdate(data: DiscordGatewayPayload) {
const payload = data.d as ThreadMembersUpdate;
const thread = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const thread = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!thread) return;
thread.memberCount = payload.memberCount;
@@ -6,10 +6,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as ThreadMember;
const thread = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const thread = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!thread) return;
thread.member = payload;
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const oldChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id)
);
const oldChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
if (!oldChannel) return;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
@@ -3,7 +3,5 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { ApplicationCommandCreateUpdateDelete } from "../../types/interactions/commands/application_command_create_update_delete.ts";
export function handleApplicationCommandCreate(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandCreate?.(
data.d as ApplicationCommandCreateUpdateDelete
);
eventHandlers.applicationCommandCreate?.(data.d as ApplicationCommandCreateUpdateDelete);
}
@@ -3,7 +3,5 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { ApplicationCommandCreateUpdateDelete } from "../../types/interactions/commands/application_command_create_update_delete.ts";
export function handleApplicationCommandDelete(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandDelete?.(
data.d as ApplicationCommandCreateUpdateDelete
);
eventHandlers.applicationCommandDelete?.(data.d as ApplicationCommandCreateUpdateDelete);
}
@@ -3,7 +3,5 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { ApplicationCommandCreateUpdateDelete } from "../../types/interactions/commands/application_command_create_update_delete.ts";
export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandUpdate?.(
data.d as ApplicationCommandCreateUpdateDelete
);
eventHandlers.applicationCommandUpdate?.(data.d as ApplicationCommandCreateUpdateDelete);
}
+2 -7
View File
@@ -7,16 +7,11 @@ import { Collection } from "../../util/collection.ts";
export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildEmojisUpdate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const cachedEmojis = guild.emojis;
guild.emojis = new Collection(
payload.emojis.map((emoji) => [snowflakeToBigint(emoji.id!), emoji])
);
guild.emojis = new Collection(payload.emojis.map((emoji) => [snowflakeToBigint(emoji.id!), emoji]));
await cacheHandlers.set("guilds", guild.id, guild);
+2 -8
View File
@@ -6,15 +6,9 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id)
);
const member = await cacheHandlers.get("members", snowflakeToBigint(payload.user.id));
eventHandlers.guildBanAdd?.(guild, payload.user, member);
}
+2 -8
View File
@@ -6,15 +6,9 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id)
);
const member = await cacheHandlers.get("members", snowflakeToBigint(payload.user.id));
eventHandlers.guildBanRemove?.(guild, payload.user, member);
}
+1 -4
View File
@@ -6,10 +6,7 @@ import type { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate(
data: DiscordGatewayPayload,
shardId: number
) {
export async function handleGuildCreate(data: DiscordGatewayPayload, shardId: number) {
const payload = data.d as Guild;
// When shards resume they emit GUILD_CREATE again.
if (await cacheHandlers.has("guilds", snowflakeToBigint(payload.id))) return;
+5 -20
View File
@@ -5,16 +5,10 @@ import type { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete(
data: DiscordGatewayPayload,
shardId: number
) {
export async function handleGuildDelete(data: DiscordGatewayPayload, shardId: number) {
const payload = data.d as UnavailableGuild;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.id));
if (!guild) return;
await cacheHandlers.delete("guilds", guild.id);
@@ -30,30 +24,21 @@ export async function handleGuildDelete(
}
cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.(
"loop",
`1. Running forEach messages loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `1. Running forEach messages loop in CHANNEL_DELTE file.`);
if (message.guildId === guild.id) {
cacheHandlers.delete("messages", message.id);
}
});
cacheHandlers.forEach("channels", (channel) => {
eventHandlers.debug?.(
"loop",
`2. Running forEach channels loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `2. Running forEach channels loop in CHANNEL_DELTE file.`);
if (channel.guildId === guild.id) {
cacheHandlers.delete("channels", channel.id);
}
});
cacheHandlers.forEach("members", (member) => {
eventHandlers.debug?.(
"loop",
`3. Running forEach members loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `3. Running forEach members loop in CHANNEL_DELTE file.`);
if (!member.guilds.has(guild.id)) return;
member.guilds.delete(guild.id);
@@ -4,15 +4,10 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { GuildIntegrationsUpdate } from "../../types/integrations/guild_integrations_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildIntegrationsUpdate(
data: DiscordGatewayPayload
) {
export async function handleGuildIntegrationsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildIntegrationsUpdate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
eventHandlers.guildIntegrationsUpdate?.(guild);
+3 -16
View File
@@ -6,25 +6,12 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildUpdate(
data: DiscordGatewayPayload,
shardId: number
) {
export async function handleGuildUpdate(data: DiscordGatewayPayload, shardId: number) {
const payload = data.d as Guild;
const oldGuild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id)
);
const oldGuild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.id));
if (!oldGuild) return;
const keysToSkip = [
"id",
"roles",
"guildHashes",
"guildId",
"maxMembers",
"emojis",
];
const keysToSkip = ["id", "roles", "guildHashes", "guildId", "maxMembers", "emojis"];
const newGuild = await structures.createDiscordenoGuild(payload, shardId);
@@ -9,10 +9,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as Interaction;
const discordenoMember = payload.guildId
? await structures.createDiscordenoMember(
payload.member as GuildMemberWithUser,
snowflakeToBigint(payload.guildId)
)
? await structures.createDiscordenoMember(payload.member as GuildMemberWithUser, snowflakeToBigint(payload.guildId))
: undefined;
if (discordenoMember) {
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
+2 -7
View File
@@ -12,10 +12,7 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const members = await Promise.all(
payload.members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
member,
guildId
);
const discordenoMember = await structures.createDiscordenoMember(member, guildId);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
return discordenoMember;
@@ -34,9 +31,7 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
return resolve(new Collection(members.map((m) => [m.id, m])));
}
return resolve(
await cacheHandlers.filter("members", (m) => m.guilds.has(guildId))
);
return resolve(await cacheHandlers.filter("members", (m) => m.guilds.has(guildId)));
}
}
}
+2 -8
View File
@@ -7,17 +7,11 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberAdd;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
guild.memberCount++;
const discordenoMember = await structures.createDiscordenoMember(
payload,
guild.id
);
const discordenoMember = await structures.createDiscordenoMember(payload, guild.id);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
eventHandlers.guildMemberAdd?.(guild, discordenoMember);
+2 -8
View File
@@ -6,17 +6,11 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberRemove;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
guild.memberCount--;
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id)
);
const member = await cacheHandlers.get("members", snowflakeToBigint(payload.user.id));
eventHandlers.guildMemberRemove?.(guild, payload.user, member);
member?.guilds.delete(guild.id);
+7 -31
View File
@@ -7,16 +7,10 @@ import { bigintToSnowflake, snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberUpdate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const cachedMember = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id)
);
const cachedMember = await cacheHandlers.get("members", snowflakeToBigint(payload.user.id));
const guildMember = cachedMember?.guilds.get(guild.id);
const newMemberData = {
@@ -27,20 +21,12 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
mute: guildMember?.mute || false,
roles: payload.roles,
};
const discordenoMember = await structures.createDiscordenoMember(
newMemberData,
guild.id
);
const discordenoMember = await structures.createDiscordenoMember(newMemberData, guild.id);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
if (guildMember) {
if (guildMember.nick !== payload.nick) {
eventHandlers.nicknameUpdate?.(
guild,
discordenoMember,
payload.nick!,
guildMember.nick ?? undefined
);
eventHandlers.nicknameUpdate?.(guild, discordenoMember, payload.nick!, guildMember.nick ?? undefined);
}
if (payload.pending === false && guildMember.pending === true) {
@@ -50,26 +36,16 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const roleIds = guildMember.roles || [];
roleIds.forEach((id) => {
eventHandlers.debug?.(
"loop",
`1. Running forEach loop in GUILD_MEMBER_UPDATE file.`
);
eventHandlers.debug?.("loop", `1. Running forEach loop in GUILD_MEMBER_UPDATE file.`);
if (!payload.roles.includes(bigintToSnowflake(id))) {
eventHandlers.roleLost?.(guild, discordenoMember, id);
}
});
payload.roles.forEach((id) => {
eventHandlers.debug?.(
"loop",
`2. Running forEach loop in GUILD_MEMBER_UPDATE file.`
);
eventHandlers.debug?.("loop", `2. Running forEach loop in GUILD_MEMBER_UPDATE file.`);
if (!roleIds.includes(snowflakeToBigint(id))) {
eventHandlers.roleGained?.(
guild,
discordenoMember,
snowflakeToBigint(id)
);
eventHandlers.roleGained?.(guild, discordenoMember, snowflakeToBigint(id));
}
});
}
+3 -12
View File
@@ -8,15 +8,10 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageCreate(data: DiscordGatewayPayload) {
const payload = data.d as Message;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId)
);
const channel = await cacheHandlers.get("channels", snowflakeToBigint(payload.channelId));
if (channel) channel.lastMessageId = snowflakeToBigint(payload.id);
const guild = payload.guildId
? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined;
const guild = payload.guildId ? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId)) : undefined;
if (payload.member && guild) {
// If in a guild cache the author as a member
@@ -37,11 +32,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
guild.id
);
return cacheHandlers.set(
"members",
snowflakeToBigint(mention.id),
discordenoMember
);
return cacheHandlers.set("members", snowflakeToBigint(mention.id), discordenoMember);
}
})
);
+1 -4
View File
@@ -6,10 +6,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDelete(data: DiscordGatewayPayload) {
const payload = data.d as MessageDelete;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId)
);
const channel = await cacheHandlers.get("channels", snowflakeToBigint(payload.channelId));
if (!channel) return;
eventHandlers.messageDelete?.(
+2 -8
View File
@@ -6,18 +6,12 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) {
const payload = data.d as MessageDeleteBulk;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId)
);
const channel = await cacheHandlers.get("channels", snowflakeToBigint(payload.channelId));
if (!channel) return;
return Promise.all(
payload.ids.map(async (id) => {
eventHandlers.messageDelete?.(
{ id, channel },
await cacheHandlers.get("messages", snowflakeToBigint(id))
);
eventHandlers.messageDelete?.({ id, channel }, await cacheHandlers.get("messages", snowflakeToBigint(id)));
await cacheHandlers.delete("messages", snowflakeToBigint(id));
})
);
+6 -23
View File
@@ -7,16 +7,11 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionAdd;
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId)
);
const message = await cacheHandlers.get("messages", snowflakeToBigint(payload.messageId));
if (message) {
const reactionExisted = message.reactions?.find(
(reaction) =>
reaction.emoji.id === payload.emoji.id &&
reaction.emoji.name === payload.emoji.name
(reaction) => reaction.emoji.id === payload.emoji.id && reaction.emoji.name === payload.emoji.name
);
if (reactionExisted) reactionExisted.count++;
@@ -26,28 +21,16 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
me: snowflakeToBigint(payload.userId) === botId,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
? [...message.reactions, newReaction]
: [newReaction];
message.reactions = message.reactions ? [...message.reactions, newReaction] : [newReaction];
}
await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message
);
await cacheHandlers.set("messages", snowflakeToBigint(payload.messageId), message);
}
if (payload.member && payload.guildId) {
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (guild) {
const discordenoMember = await structures.createDiscordenoMember(
payload.member,
guild.id
);
const discordenoMember = await structures.createDiscordenoMember(payload.member, guild.id);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
}
}
@@ -6,17 +6,13 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemove(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionRemove;
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId)
);
const message = await cacheHandlers.get("messages", snowflakeToBigint(payload.messageId));
if (message) {
const reaction = message.reactions?.find(
(reaction) =>
// MUST USE == because discord sends null and we use undefined
reaction.emoji.id == payload.emoji.id &&
reaction.emoji.name === payload.emoji.name
reaction.emoji.id == payload.emoji.id && reaction.emoji.name === payload.emoji.name
);
if (reaction) {
@@ -4,23 +4,14 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { MessageReactionRemoveAll } from "../../types/messages/message_reaction_remove_all.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload
) {
export async function handleMessageReactionRemoveAll(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionRemoveAll;
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId)
);
const message = await cacheHandlers.get("messages", snowflakeToBigint(payload.messageId));
if (message?.reactions) {
message.reactions = undefined;
await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message
);
await cacheHandlers.set("messages", snowflakeToBigint(payload.messageId), message);
}
eventHandlers.reactionRemoveAll?.(payload, message);
@@ -4,24 +4,16 @@ import type { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.
import type { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveEmoji(
data: DiscordGatewayPayload
) {
export async function handleMessageReactionRemoveEmoji(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionRemoveEmoji;
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId)
);
const message = await cacheHandlers.get("messages", snowflakeToBigint(payload.messageId));
if (message?.reactions) {
message.reactions = message.reactions.filter(
(reaction) =>
!(
// MUST USE == because discord sends null and we use undefined
(
reaction.emoji.id == payload.emoji.id &&
reaction.emoji.name === payload.emoji.name
)
(reaction.emoji.id == payload.emoji.id && reaction.emoji.name === payload.emoji.name)
)
);
+2 -8
View File
@@ -7,16 +7,10 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Message;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId)
);
const channel = await cacheHandlers.get("channels", snowflakeToBigint(payload.channelId));
if (!channel) return;
const oldMessage = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.id)
);
const oldMessage = await cacheHandlers.get("messages", snowflakeToBigint(payload.id));
if (!oldMessage) return;
// Messages with embeds can trigger update but they wont have edited_timestamp
+2 -9
View File
@@ -7,15 +7,8 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = data.d as PresenceUpdate;
const oldPresence = await cacheHandlers.get(
"presences",
snowflakeToBigint(payload.user.id)
);
await cacheHandlers.set(
"presences",
snowflakeToBigint(payload.user.id),
payload
);
const oldPresence = await cacheHandlers.get("presences", snowflakeToBigint(payload.user.id));
await cacheHandlers.set("presences", snowflakeToBigint(payload.user.id), payload);
eventHandlers.presenceUpdate?.(payload, oldPresence);
}
+1 -3
View File
@@ -22,9 +22,7 @@ export function handleReady(data: DiscordGatewayPayload, shardId: number) {
// Set ready to false just to go sure
shard.ready = false;
// All guilds are unavailable at first
shard.unavailableGuildIds = new Set(
payload.guilds.map((g) => snowflakeToBigint(g.id))
);
shard.unavailableGuildIds = new Set(payload.guilds.map((g) => snowflakeToBigint(g.id)));
// Set the last available to now
shard.lastAvailable = Date.now();
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleUserUpdate(data: DiscordGatewayPayload) {
const userData = data.d as User;
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(userData.id)
);
const member = await cacheHandlers.get("members", snowflakeToBigint(userData.id));
if (!member) return;
Object.entries(userData).forEach(([key, value]) => {
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleCreate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const role = await structures.createDiscordenoRole({
+3 -12
View File
@@ -6,10 +6,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleDelete;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const roleId = snowflakeToBigint(payload.roleId);
@@ -21,18 +18,12 @@ export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
// For bots without GUILD_MEMBERS member.roles is never updated breaking permissions checking.
cacheHandlers.forEach("members", (member) => {
eventHandlers.debug?.(
"loop",
`1. Running forEach members loop in GUILD_ROLE_DELETE file.`
);
eventHandlers.debug?.("loop", `1. Running forEach members loop in GUILD_ROLE_DELETE file.`);
// Not in the relevant guild so just skip.
if (!member.guilds.has(guild.id)) return;
member.guilds.forEach((g) => {
eventHandlers.debug?.(
"loop",
`2. Running forEach loop in CHANNEL_DELTE file.`
);
eventHandlers.debug?.("loop", `2. Running forEach loop in CHANNEL_DELTE file.`);
// Member does not have this role
if (!g.roles.includes(roleId)) return;
// Remove this role from the members cache
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleUpdate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const cachedRole = guild.roles.get(snowflakeToBigint(payload.role.id));
+1 -4
View File
@@ -7,10 +7,7 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceServerUpdate;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
eventHandlers.voiceServerUpdate?.(payload, guild);
+4 -17
View File
@@ -9,10 +9,7 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceState;
if (!payload.guildId) return;
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId)
);
const guild = await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId));
if (!guild) return;
const member = payload.member
@@ -30,25 +27,15 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
await cacheHandlers.set("guilds", guild.id, guild);
if (
cachedState?.channelId !==
(payload.channelId ? snowflakeToBigint(payload.channelId) : null)
) {
if (cachedState?.channelId !== (payload.channelId ? snowflakeToBigint(payload.channelId) : null)) {
// Either joined or moved channels
if (payload.channelId) {
if (cachedState?.channelId) {
// Was in a channel before
eventHandlers.voiceChannelSwitch?.(
member,
snowflakeToBigint(payload.channelId),
cachedState.channelId
);
eventHandlers.voiceChannelSwitch?.(member, snowflakeToBigint(payload.channelId), cachedState.channelId);
} else {
// Was not in a channel before so user just joined
eventHandlers.voiceChannelJoin?.(
member,
snowflakeToBigint(payload.channelId)
);
eventHandlers.voiceChannelJoin?.(member, snowflakeToBigint(payload.channelId));
}
} // Left the channel
else if (cachedState?.channelId) {
+1 -4
View File
@@ -5,8 +5,5 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export function handleWebhooksUpdate(data: DiscordGatewayPayload) {
const options = data.d as WebhookUpdate;
eventHandlers.webhooksUpdate?.(
snowflakeToBigint(options.channelId),
snowflakeToBigint(options.guildId)
);
eventHandlers.webhooksUpdate?.(snowflakeToBigint(options.channelId), snowflakeToBigint(options.guildId));
}
+1 -4
View File
@@ -2,8 +2,5 @@ import { cacheHandlers } from "../../cache.ts";
/** Gets an array of all the channels ids that are the children of this category. */
export async function categoryChildren(id: bigint) {
return await cacheHandlers.filter(
"channels",
(channel) => channel.parentId === id
);
return await cacheHandlers.filter("channels", (channel) => channel.parentId === id);
}
@@ -13,9 +13,7 @@ export function channelOverwriteHasPermission(
})[],
permissions: PermissionStrings[]
) {
const overwrite =
overwrites.find((perm) => perm.id === id) ||
overwrites.find((perm) => perm.id === guildId);
const overwrite = overwrites.find((perm) => perm.id === id) || overwrites.find((perm) => perm.id === guildId);
if (!overwrite) return false;
+3 -12
View File
@@ -11,10 +11,7 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
if (!channelToClone) throw new Error(Errors.CHANNEL_NOT_FOUND);
//Check for DM channel
if (
channelToClone.type === DiscordChannelTypes.DM ||
channelToClone.type === DiscordChannelTypes.GroupDm
) {
if (channelToClone.type === DiscordChannelTypes.DM || channelToClone.type === DiscordChannelTypes.GroupDm) {
throw new Error(Errors.CHANNEL_NOT_IN_GUILD);
}
@@ -22,9 +19,7 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
...channelToClone,
name: channelToClone.name!,
topic: channelToClone.topic || undefined,
permissionOverwrites: channelToClone.permissionOverwrites.map((
overwrite,
) => ({
permissionOverwrites: channelToClone.permissionOverwrites.map((overwrite) => ({
id: overwrite.id.toString(),
type: overwrite.type,
allow: calculatePermissions(overwrite.allow.toString()),
@@ -33,9 +28,5 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
};
//Create the channel (also handles permissions)
return await helpers.createChannel(
channelToClone.guildId!,
createChannelOptions,
reason,
);
return await helpers.createChannel(channelToClone.guildId!, createChannelOptions, reason);
}
+13 -27
View File
@@ -3,23 +3,13 @@ import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import type { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import type {
CreateGuildChannel,
DiscordCreateGuildChannel,
} from "../../types/guilds/create_guild_channel.ts";
import type { CreateGuildChannel, DiscordCreateGuildChannel } from "../../types/guilds/create_guild_channel.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireOverwritePermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function createChannel(
guildId: bigint,
options?: CreateGuildChannel,
reason?: string
) {
export async function createChannel(guildId: bigint, options?: CreateGuildChannel, reason?: string) {
if (options?.permissionOverwrites) {
await requireOverwritePermissions(guildId, options.permissionOverwrites);
}
@@ -27,20 +17,16 @@ export async function createChannel(
// BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000;
const result = await rest.runMethod<Channel>(
"post",
endpoints.GUILD_CHANNELS(guildId),
{
...snakelize<DiscordCreateGuildChannel>(options ?? {}),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
...perm,
allow: calculateBits(perm.allow),
deny: calculateBits(perm.deny),
})),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
}
);
const result = await rest.runMethod<Channel>("post", endpoints.GUILD_CHANNELS(guildId), {
...snakelize<DiscordCreateGuildChannel>(options ?? {}),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
...perm,
allow: calculateBits(perm.allow),
deny: calculateBits(perm.deny),
})),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
});
const discordenoChannel = await structures.createDiscordenoChannel(result);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
+5 -13
View File
@@ -16,23 +16,15 @@ export async function createStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MANAGE_CHANNELS",
"MUTE_MEMBERS",
"MOVE_MEMBERS",
]);
await requireBotChannelPermissions(channel, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]);
}
if (!validateLength(topic, { max: 120, min: 1 })) {
throw new Error(Errors.INVALID_TOPIC_LENGTH);
}
return await rest.runMethod<StageInstance>(
"post",
endpoints.STAGE_INSTANCES,
{
channel_id: channelId,
topic,
}
);
return await rest.runMethod<StageInstance>("post", endpoints.STAGE_INSTANCES, {
channel_id: channelId,
topic,
});
}
+4 -10
View File
@@ -16,11 +16,9 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
// TODO(threads): check if this requires guild perms or channel is enough
await requireBotGuildPermissions(
guild,
[
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
[ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
? ["MANAGE_THREADS"]
: ["MANAGE_CHANNELS"]
);
@@ -33,9 +31,5 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
}
}
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_BASE(channelId),
{ reason }
);
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_BASE(channelId), { reason });
}
@@ -10,8 +10,5 @@ export async function deleteChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId)
);
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId));
}
@@ -14,15 +14,8 @@ export async function deleteStageInstance(channelId: bigint) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MUTE_MEMBERS",
"MANAGE_CHANNELS",
"MOVE_MEMBERS",
]);
await requireBotChannelPermissions(channel, ["MUTE_MEMBERS", "MANAGE_CHANNELS", "MOVE_MEMBERS"]);
}
return await rest.runMethod<undefined>(
"delete",
endpoints.STAGE_INSTANCE(channelId)
);
return await rest.runMethod<undefined>("delete", endpoints.STAGE_INSTANCE(channelId));
}
+10 -33
View File
@@ -9,21 +9,13 @@ import type { ModifyChannel } from "../../types/channels/modify_channel.ts";
import type { ModifyThread } from "../../types/channels/threads/modify_thread.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireBotChannelPermissions,
requireOverwritePermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireBotChannelPermissions, requireOverwritePermissions } from "../../util/permissions.ts";
import { hasOwnProperty, snakelize } from "../../util/utils.ts";
//TODO: implement DM group channel edit
//TODO(threads): check thread perms
/** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */
export async function editChannel(
channelId: bigint,
options: ModifyChannel | ModifyThread,
reason?: string
) {
export async function editChannel(channelId: bigint, options: ModifyChannel | ModifyThread, reason?: string) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
@@ -46,19 +38,11 @@ export async function editChannel(
permissions.add("MANAGE_THREADS");
}
await requireBotChannelPermissions(channel.parentId ?? 0n, [
...permissions,
]);
await requireBotChannelPermissions(channel.parentId ?? 0n, [...permissions]);
}
if (
hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") &&
Array.isArray(options.permissionOverwrites)
) {
await requireOverwritePermissions(
channel.guildId,
options.permissionOverwrites
);
if (hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") && Array.isArray(options.permissionOverwrites)) {
await requireOverwritePermissions(channel.guildId, options.permissionOverwrites);
}
}
@@ -91,10 +75,7 @@ export async function editChannel(
const payload = {
...snakelize<Record<string, unknown>>(options),
// deno-lint-ignore camelcase
permission_overwrites: hasOwnProperty<ModifyChannel>(
options,
"permissionOverwrites"
)
permission_overwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
? options.permissionOverwrites?.map((overwrite) => {
return {
...overwrite,
@@ -105,14 +86,10 @@ export async function editChannel(
: undefined,
};
const result = await rest.runMethod<Channel>(
"patch",
endpoints.CHANNEL_BASE(channelId),
{
...payload,
reason,
}
);
const result = await rest.runMethod<Channel>("patch", endpoints.CHANNEL_BASE(channelId), {
...payload,
reason,
});
return await structures.createDiscordenoChannel(result);
}
+6 -13
View File
@@ -1,10 +1,7 @@
import { rest } from "../../rest/rest.ts";
import type { Overwrite } from "../../types/channels/overwrite.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireBotGuildPermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireBotGuildPermissions } from "../../util/permissions.ts";
/** Edit the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */
export async function editChannelOverwrite(
@@ -15,13 +12,9 @@ export async function editChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
return await rest.runMethod<undefined>(
"put",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
{
allow: calculateBits(options.allow),
deny: calculateBits(options.deny),
type: options.type,
}
);
return await rest.runMethod<undefined>("put", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId), {
allow: calculateBits(options.allow),
deny: calculateBits(options.deny),
type: options.type,
});
}
+4 -11
View File
@@ -4,19 +4,12 @@ import { endpoints } from "../../util/constants.ts";
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. */
export async function followChannel(
sourceChannelId: bigint,
targetChannelId: bigint
) {
export async function followChannel(sourceChannelId: bigint, targetChannelId: bigint) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
const data = await rest.runMethod<FollowedChannel>(
"post",
endpoints.CHANNEL_FOLLOW(sourceChannelId),
{
webhook_channel_id: targetChannelId,
}
);
const data = await rest.runMethod<FollowedChannel>("post", endpoints.CHANNEL_FOLLOW(sourceChannelId), {
webhook_channel_id: targetChannelId,
});
return data.webhookId;
}
+2 -9
View File
@@ -10,21 +10,14 @@ 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.**
*/
export async function getChannel(channelId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel>(
"get",
endpoints.CHANNEL_BASE(channelId)
);
const result = await rest.runMethod<Channel>("get", endpoints.CHANNEL_BASE(channelId));
const discordenoChannel = await structures.createDiscordenoChannel(
result,
result.guildId ? snowflakeToBigint(result.guildId) : undefined
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
}
return discordenoChannel;
+1 -4
View File
@@ -8,10 +8,7 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function getChannelWebhooks(channelId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod<Webhook[]>(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId)
);
const result = await rest.runMethod<Webhook[]>("get", endpoints.CHANNEL_WEBHOOKS(channelId));
return new Collection(result.map((webhook) => [webhook.id, webhook]));
}
+3 -13
View File
@@ -10,25 +10,15 @@ 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.**
*/
export async function getChannels(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel[]>(
"get",
endpoints.GUILD_CHANNELS(guildId)
);
const result = await rest.runMethod<Channel[]>("get", endpoints.GUILD_CHANNELS(guildId));
return new Collection(
(
await Promise.all(
result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId
);
const discordenoChannel = await structures.createDiscordenoChannel(res, guildId);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
}
return discordenoChannel;
+2 -7
View File
@@ -5,12 +5,7 @@ import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */
export async function getPins(channelId: bigint) {
const result = await rest.runMethod<Message[]>(
"get",
endpoints.CHANNEL_PINS(channelId)
);
const result = await rest.runMethod<Message[]>("get", endpoints.CHANNEL_PINS(channelId));
return Promise.all(
result.map((res) => structures.createDiscordenoMessage(res))
);
return Promise.all(result.map((res) => structures.createDiscordenoMessage(res)));
}
+1 -4
View File
@@ -15,8 +15,5 @@ export async function getStageInstance(channelId: bigint) {
}
}
return await rest.runMethod<StageInstance>(
"get",
endpoints.STAGE_INSTANCE(channelId)
);
return await rest.runMethod<StageInstance>("get", endpoints.STAGE_INSTANCE(channelId));
}
+2 -6
View File
@@ -9,12 +9,8 @@ export async function isChannelSynced(channelId: bigint) {
if (!parentChannel) return false;
return channel.permissionOverwrites?.every((overwrite) => {
const permission = parentChannel.permissionOverwrites?.find(
(ow) => ow.id === overwrite.id
);
const permission = parentChannel.permissionOverwrites?.find((ow) => ow.id === overwrite.id);
if (!permission) return false;
return !(
overwrite.allow !== permission.allow || overwrite.deny !== permission.deny
);
return !(overwrite.allow !== permission.allow || overwrite.deny !== permission.deny);
});
}
+2 -7
View File
@@ -27,16 +27,11 @@ export async function startTyping(channelId: bigint) {
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
}
const hasSendMessagesPerm = await botHasChannelPermissions(channelId, [
"SEND_MESSAGES",
]);
const hasSendMessagesPerm = await botHasChannelPermissions(channelId, ["SEND_MESSAGES"]);
if (!hasSendMessagesPerm) {
throw new Error(Errors.MISSING_SEND_MESSAGES);
}
}
return await rest.runMethod<undefined>(
"post",
endpoints.CHANNEL_TYPING(channelId)
);
return await rest.runMethod<undefined>("post", endpoints.CHANNEL_TYPING(channelId));
}
+2 -9
View File
@@ -3,17 +3,10 @@ import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guil
import { endpoints } from "../../util/constants.ts";
/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
export async function swapChannels(
guildId: bigint,
channelPositions: ModifyGuildChannelPositions[]
) {
export async function swapChannels(guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) {
if (channelPositions.length < 2) {
throw "You must provide at least two channels to be swapped.";
}
return await rest.runMethod<undefined>(
"patch",
endpoints.GUILD_CHANNELS(guildId),
channelPositions
);
return await rest.runMethod<undefined>("patch", endpoints.GUILD_CHANNELS(guildId), channelPositions);
}
@@ -12,11 +12,9 @@ export async function addToThread(channelId: bigint, userId?: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -24,8 +22,6 @@ export async function addToThread(channelId: bigint, userId?: bigint) {
return await rest.runMethod(
"put",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId)
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
);
}
@@ -12,11 +12,9 @@ export async function getThreadMembers(channelId: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -10,11 +10,9 @@ export async function removeFromThread(channelId: bigint, userId?: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -22,8 +20,6 @@ export async function removeFromThread(channelId: bigint, userId?: bigint) {
return await rest.runMethod(
"delete",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId)
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
);
}
+2 -7
View File
@@ -10,16 +10,11 @@ import { snakelize } from "../../../util/utils.ts";
* Creates a new public thread from an existing message. Returns a channel on success, and a 400 BAD REQUEST on invalid parameters. Fires a Thread Create Gateway event.
* @param messageId when provided the thread will be public
*/
export async function startThread(
channelId: bigint,
options: StartThread & { messageId?: bigint }
) {
export async function startThread(channelId: bigint, options: StartThread & { messageId?: bigint }) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
// TODO(threads): perm check
if (
![ChannelTypes.GuildText, ChannelTypes.GuildNews].includes(channel.type)
) {
if (![ChannelTypes.GuildText, ChannelTypes.GuildNews].includes(channel.type)) {
throw new Error(Errors.INVALID_THREAD_PARENT_CHANNEL_TYPE);
}
+4 -12
View File
@@ -16,11 +16,7 @@ export async function updateStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MOVE_MEMBERS",
"MUTE_MEMBERS",
"MANAGE_CHANNELS",
]);
await requireBotChannelPermissions(channel, ["MOVE_MEMBERS", "MUTE_MEMBERS", "MANAGE_CHANNELS"]);
}
if (
@@ -32,11 +28,7 @@ export async function updateStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.INVALID_TOPIC_LENGTH);
}
return await rest.runMethod<StageInstance>(
"patch",
endpoints.STAGE_INSTANCE(channelId),
{
topic,
}
);
return await rest.runMethod<StageInstance>("patch", endpoints.STAGE_INSTANCE(channelId), {
topic,
});
}
+1 -4
View File
@@ -21,10 +21,7 @@ export async function updateBotVoiceState(
) {
return await rest.runMethod(
"patch",
endpoints.UPDATE_VOICE_STATE(
guildId,
hasOwnProperty(options, "userId") ? options.userId : undefined
),
endpoints.UPDATE_VOICE_STATE(guildId, hasOwnProperty(options, "userId") ? options.userId : undefined),
snakelize(options)
);
}
@@ -4,10 +4,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */
export async function addDiscoverySubcategory(
guildId: bigint,
categoryId: number
) {
export async function addDiscoverySubcategory(guildId: bigint, categoryId: number) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<AddGuildDiscoverySubcategory>(
+2 -9
View File
@@ -6,15 +6,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */
export async function editDiscovery(
guildId: bigint,
data: ModifyGuildDiscoveryMetadata
) {
export async function editDiscovery(guildId: bigint, data: ModifyGuildDiscoveryMetadata) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<DiscoveryMetadata>(
"patch",
endpoints.DISCOVERY_MODIFY(guildId),
snakelize(data)
);
return await rest.runMethod<DiscoveryMetadata>("patch", endpoints.DISCOVERY_MODIFY(guildId), snakelize(data));
}
@@ -5,12 +5,7 @@ import { endpoints } from "../../util/constants.ts";
/** Returns an array of discovery category objects that can be used when editing guilds */
export async function getDiscoveryCategories() {
const result = await rest.runMethod<DiscoveryCategory[]>(
"get",
endpoints.DISCOVERY_CATEGORIES
);
const result = await rest.runMethod<DiscoveryCategory[]>("get", endpoints.DISCOVERY_CATEGORIES);
return new Collection<number, DiscoveryCategory>(
result.map((category) => [category.id, category])
);
return new Collection<number, DiscoveryCategory>(result.map((category) => [category.id, category]));
}
@@ -3,14 +3,8 @@ import { endpoints } from "../../util/constants.ts";
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. */
export async function removeDiscoverySubcategory(
guildId: bigint,
categoryId: number
) {
export async function removeDiscoverySubcategory(guildId: bigint, categoryId: number) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId)
);
return await rest.runMethod<undefined>("delete", endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId));
}
@@ -3,11 +3,7 @@ import type { ValidateDiscoverySearchTerm } from "../../types/discovery/validate
import { endpoints } from "../../util/constants.ts";
export async function validDiscoveryTerm(term: string) {
const result = await rest.runMethod<ValidateDiscoverySearchTerm>(
"get",
endpoints.DISCOVERY_VALID_TERM,
{ term }
);
const result = await rest.runMethod<ValidateDiscoverySearchTerm>("get", endpoints.DISCOVERY_VALID_TERM, { term });
return result.valid;
}
+6 -15
View File
@@ -7,27 +7,18 @@ import { requireBotGuildPermissions } from "../../util/permissions.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. */
export async function createEmoji(
guildId: bigint,
name: string,
image: string,
options: CreateGuildEmoji
) {
export async function createEmoji(guildId: bigint, name: string, image: string, options: CreateGuildEmoji) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
if (image && !image.startsWith("data:image/")) {
image = await urlToBase64(image);
}
const emoji = await rest.runMethod<Emoji>(
"post",
endpoints.GUILD_EMOJIS(guildId),
{
...options,
name,
image,
}
);
const emoji = await rest.runMethod<Emoji>("post", endpoints.GUILD_EMOJIS(guildId), {
...options,
name,
image,
});
return {
...emoji,
+2 -10
View File
@@ -3,16 +3,8 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
export async function deleteEmoji(
guildId: bigint,
id: bigint,
reason?: string
) {
export async function deleteEmoji(guildId: bigint, id: bigint, reason?: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_EMOJI(guildId, id),
{ reason }
);
return await rest.runMethod<undefined>("delete", endpoints.GUILD_EMOJI(guildId, id), { reason });
}
+5 -13
View File
@@ -5,19 +5,11 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
export async function editEmoji(
guildId: bigint,
id: bigint,
options: ModifyGuildEmoji
) {
export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<Emoji>(
"patch",
endpoints.GUILD_EMOJI(guildId, id),
{
name: options.name,
roles: options.roles,
}
);
return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), {
name: options.name,
roles: options.roles,
});
}
+2 -9
View File
@@ -9,15 +9,8 @@ import { endpoints } from "../../util/constants.ts";
*
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/
export async function getEmoji(
guildId: bigint,
emojiId: bigint,
addToCache = true
) {
const result = await rest.runMethod<Emoji>(
"get",
endpoints.GUILD_EMOJI(guildId, emojiId)
);
export async function getEmoji(guildId: bigint, emojiId: bigint, addToCache = true) {
const result = await rest.runMethod<Emoji>("get", endpoints.GUILD_EMOJI(guildId, emojiId));
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
+1 -4
View File
@@ -13,10 +13,7 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/
export async function getEmojis(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Emoji[]>(
"get",
endpoints.GUILD_EMOJIS(guildId)
);
const result = await rest.runMethod<Emoji[]>("get", endpoints.GUILD_EMOJIS(guildId));
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
+1 -4
View File
@@ -3,8 +3,5 @@ import { endpoints } from "../../util/constants.ts";
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event. */
export async function deleteGuild(guildId: bigint) {
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILDS_BASE(guildId)
);
return await rest.runMethod<undefined>("delete", endpoints.GUILDS_BASE(guildId));
}
+2 -9
View File
@@ -24,18 +24,11 @@ export async function editGuild(guildId: bigint, options: ModifyGuild) {
options.splash = await urlToBase64(options.splash);
}
const result = await rest.runMethod<Guild>(
"patch",
endpoints.GUILDS_BASE(guildId),
options
);
const result = await rest.runMethod<Guild>("patch", endpoints.GUILDS_BASE(guildId), options);
const cached = await cacheHandlers.get("guilds", guildId);
return structures.createDiscordenoGuild(
result,
cached?.shardId ||
Number(
(BigInt(result.id) >> 22n % BigInt(ws.botGatewayData.shards)).toString()
)
cached?.shardId || Number((BigInt(result.id) >> 22n % BigInt(ws.botGatewayData.shards)).toString())
);
}
+2 -9
View File
@@ -4,13 +4,6 @@ import type { WelcomeScreen } from "../../types/guilds/welcome_screen.ts";
import { endpoints } from "../../util/constants.ts";
import { snakelize } from "../../util/utils.ts";
export async function editWelcomeScreen(
guildId: bigint,
options: ModifyGuildWelcomeScreen
) {
return await rest.runMethod<WelcomeScreen>(
"patch",
endpoints.GUILD_WELCOME_SCREEN(guildId),
snakelize(options)
);
export async function editWelcomeScreen(guildId: bigint, options: ModifyGuildWelcomeScreen) {
return await rest.runMethod<WelcomeScreen>("patch", endpoints.GUILD_WELCOME_SCREEN(guildId), snakelize(options));
}
+5 -13
View File
@@ -4,19 +4,11 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
export async function editWidget(
guildId: bigint,
enabled: boolean,
channelId?: string | null
) {
export async function editWidget(guildId: bigint, enabled: boolean, channelId?: string | null) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<GuildWidget>(
"patch",
endpoints.GUILD_WIDGET(guildId),
{
enabled,
channel_id: channelId,
}
);
return await rest.runMethod<GuildWidget>("patch", endpoints.GUILD_WIDGET(guildId), {
enabled,
channel_id: channelId,
});
}
+1 -4
View File
@@ -14,10 +14,7 @@ export async function getAuditLogs(guildId: bigint, options: GetGuildAuditLog) {
endpoints.GUILD_AUDIT_LOGS(guildId),
snakelize({
...options,
limit:
options.limit && options.limit >= 1 && options.limit <= 100
? options.limit
: 50,
limit: options.limit && options.limit >= 1 && options.limit <= 100 ? options.limit : 50,
})
);
}
+1 -4
View File
@@ -7,8 +7,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getBan(guildId: bigint, memberId: bigint) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
return await rest.runMethod<Ban>(
"get",
endpoints.GUILD_BAN(guildId, memberId)
);
return await rest.runMethod<Ban>("get", endpoints.GUILD_BAN(guildId, memberId));
}
+2 -7
View File
@@ -9,12 +9,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getBans(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const results = await rest.runMethod<Ban[]>(
"get",
endpoints.GUILD_BANS(guildId)
);
const results = await rest.runMethod<Ban[]>("get", endpoints.GUILD_BANS(guildId));
return new Collection<bigint, Ban>(
results.map((res) => [snowflakeToBigint(res.user.id), res])
);
return new Collection<bigint, Ban>(results.map((res) => [snowflakeToBigint(res.user.id), res]));
}
+3 -7
View File
@@ -19,13 +19,9 @@ export async function getGuild(
addToCache: true,
}
) {
const result = await rest.runMethod<Guild>(
"get",
endpoints.GUILDS_BASE(guildId),
{
with_counts: options.counts,
}
);
const result = await rest.runMethod<Guild>("get", endpoints.GUILDS_BASE(guildId), {
with_counts: options.counts,
});
const guild = await structures.createDiscordenoGuild(
result,
+1 -4
View File
@@ -4,8 +4,5 @@ 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. */
export async function getGuildPreview(guildId: bigint) {
return await rest.runMethod<GuildPreview>(
"get",
endpoints.GUILD_PREVIEW(guildId)
);
return await rest.runMethod<GuildPreview>("get", endpoints.GUILD_PREVIEW(guildId));
}
+2 -9
View File
@@ -6,10 +6,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */
export async function getPruneCount(
guildId: bigint,
options?: GetGuildPruneCountQuery
) {
export async function getPruneCount(guildId: bigint, options?: GetGuildPruneCountQuery) {
if (options?.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS);
if (options?.days && options.days > 30) {
throw new Error(Errors.PRUNE_MAX_DAYS);
@@ -17,11 +14,7 @@ export async function getPruneCount(
await requireBotGuildPermissions(guildId, ["KICK_MEMBERS"]);
const result = await rest.runMethod(
"get",
endpoints.GUILD_PRUNE(guildId),
snakelize(options ?? {})
);
const result = await rest.runMethod("get", endpoints.GUILD_PRUNE(guildId), snakelize(options ?? {}));
return result.pruned as number;
}
+2 -7
View File
@@ -5,12 +5,7 @@ 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. */
export async function getVoiceRegions(guildId: bigint) {
const result = await rest.runMethod<VoiceRegion[]>(
"get",
endpoints.GUILD_REGIONS(guildId)
);
const result = await rest.runMethod<VoiceRegion[]>("get", endpoints.GUILD_REGIONS(guildId));
return new Collection<string, VoiceRegion>(
result.map((region) => [region.id, region])
);
return new Collection<string, VoiceRegion>(result.map((region) => [region.id, region]));
}
+1 -4
View File
@@ -3,8 +3,5 @@ import type { WelcomeScreen } from "../../types/guilds/welcome_screen.ts";
import { endpoints } from "../../util/constants.ts";
export async function getWelcomeScreen(guildId: bigint) {
return await rest.runMethod<WelcomeScreen>(
"get",
endpoints.GUILD_WELCOME_SCREEN(guildId)
);
return await rest.runMethod<WelcomeScreen>("get", endpoints.GUILD_WELCOME_SCREEN(guildId));
}
+1 -4
View File
@@ -12,8 +12,5 @@ export async function getWidget(guildId: bigint, options?: { force: boolean }) {
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
}
return await rest.runMethod<GuildWidgetDetails>(
"get",
`${endpoints.GUILD_WIDGET(guildId)}.json`
);
return await rest.runMethod<GuildWidgetDetails>("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
}
+2 -7
View File
@@ -4,17 +4,12 @@ import { Errors } from "../../types/discordeno/errors.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns the widget image URL for the guild. */
export async function getWidgetImageURL(
guildId: bigint,
options?: GetGuildWidgetImageQuery & { force?: boolean }
) {
export async function getWidgetImageURL(guildId: bigint, options?: GetGuildWidgetImageQuery & { force?: boolean }) {
if (!options?.force) {
const guild = await cacheHandlers.get("guilds", guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
if (!guild.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
}
return `${endpoints.GUILD_WIDGET(guildId)}.png?style=${
options?.style ?? "shield"
}`;
return `${endpoints.GUILD_WIDGET(guildId)}.png?style=${options?.style ?? "shield"}`;
}
+1 -4
View File
@@ -7,8 +7,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getWidgetSettings(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<GuildWidget>(
"get",
endpoints.GUILD_WIDGET(guildId)
);
return await rest.runMethod<GuildWidget>("get", endpoints.GUILD_WIDGET(guildId));
}
+1 -3
View File
@@ -18,9 +18,7 @@ export function guildIconURL(
? formatImageURL(
endpoints.GUILD_ICON(
id,
typeof options.icon === "string"
? options.icon
: iconBigintToHash(options.icon, options.animated ?? true)
typeof options.icon === "string" ? options.icon : iconBigintToHash(options.icon, options.animated ?? true)
),
options.size || 128,
options.format
+1 -4
View File
@@ -3,8 +3,5 @@ import { endpoints } from "../../util/constants.ts";
/** Leave a guild */
export async function leaveGuild(guildId: bigint) {
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_LEAVE(guildId)
);
return await rest.runMethod<undefined>("delete", endpoints.GUILD_LEAVE(guildId));
}
@@ -6,8 +6,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function deleteIntegration(guildId: bigint, id: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_INTEGRATION(guildId, id)
);
return await rest.runMethod<undefined>("delete", endpoints.GUILD_INTEGRATION(guildId, id));
}
+1 -4
View File
@@ -7,8 +7,5 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getIntegrations(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<Integration>(
"get",
endpoints.GUILD_INTEGRATIONS(guildId)
);
return await rest.runMethod<Integration>("get", endpoints.GUILD_INTEGRATIONS(guildId));
}
@@ -9,9 +9,5 @@ export async function batchEditSlashCommandPermissions(
guildId: bigint,
options: { id: string; permissions: ApplicationCommandPermissions[] }[]
) {
return await rest.runMethod(
"put",
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
snakelize(options)
);
return await rest.runMethod("put", endpoints.COMMANDS_PERMISSIONS(applicationId, guildId), snakelize(options));
}
@@ -16,17 +16,12 @@ import { snakelize, validateSlashCommands } from "../../../util/utils.ts";
* Global commands are cached for **1 hour**. That means that new global commands will fan out slowly across all guilds, and will be guaranteed to be updated in an hour.
* Guild commands update **instantly**. We recommend you use guild commands for quick testing, and global commands when they're ready for public use.
*/
export async function createSlashCommand(
options: CreateGlobalApplicationCommand,
guildId?: bigint
) {
export async function createSlashCommand(options: CreateGlobalApplicationCommand, guildId?: bigint) {
validateSlashCommands([options], true);
return await rest.runMethod<ApplicationCommand>(
"post",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
guildId ? endpoints.COMMANDS_GUILD(applicationId, guildId) : endpoints.COMMANDS(applicationId),
snakelize(options)
);
}
@@ -6,8 +6,6 @@ import { endpoints } from "../../../util/constants.ts";
export async function deleteSlashCommand(id: bigint, guildId?: bigint) {
return await rest.runMethod<undefined>(
"delete",
guildId
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id)
: endpoints.COMMANDS_ID(applicationId, id)
guildId ? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id) : endpoints.COMMANDS_ID(applicationId, id)
);
}
@@ -7,11 +7,7 @@ export async function deleteSlashResponse(token: string, messageId?: bigint) {
return await rest.runMethod<undefined>(
"delete",
messageId
? endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(
applicationId,
token,
messageId
)
? endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(applicationId, token, messageId)
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token)
);
}

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