Merge pull request #871 from discordeno/auto-convert

add automatic toCamel
This commit is contained in:
ITOH
2021-04-24 23:25:48 +02:00
committed by GitHub
154 changed files with 476 additions and 675 deletions

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);

View File

@@ -1,19 +1,19 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
if (!cachedChannel) return;
if (
cachedChannel.type === DiscordChannelTypes.GUILD_VOICE && payload.guild_id
cachedChannel.type === DiscordChannelTypes.GUILD_VOICE && payload.guildId
) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (guild) {
return Promise.all(guild.voiceStates.map(async (vs, key) => {

View File

@@ -1,17 +1,17 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { ChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannelPinsUpdate;
const payload = data.d as ChannelPinsUpdate;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
const guild = payload.guild_id
? await cacheHandlers.get("guilds", payload.guild_id)
const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId)
: undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.last_pin_timestamp);
eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp);
}

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
const discordenoChannel = await structures.createDiscordenoChannel(payload);

View File

@@ -2,16 +2,12 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandCreate(
data: DiscordGatewayPayload,
) {
eventHandlers.applicationCommandCreate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}

View File

@@ -2,14 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandDelete(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandDelete?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}

View File

@@ -2,14 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandUpdate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}

View File

@@ -1,12 +1,12 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGuildEmojisUpdate } 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 { Collection } from "../../util/collection.ts";
export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedEmojis = guild.emojis;
@@ -14,7 +14,7 @@ export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
payload.emojis.map((emoji) => [emoji.id!, emoji]),
);
await cacheHandlers.set("guilds", payload.guild_id, guild);
await cacheHandlers.set("guilds", payload.guildId, guild);
eventHandlers.guildEmojisUpdate?.(
guild,

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
export async function handleGuildBanAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
export async function handleGuildBanRemove(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);

View File

@@ -2,14 +2,14 @@ import { eventHandlers } from "../../bot.ts";
import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate(
data: DiscordGatewayPayload,
shardId: number,
) {
const payload = data.d as DiscordGuild;
const payload = data.d as Guild;
// When shards resume they emit GUILD_CREATE again.
if (await cacheHandlers.has("guilds", payload.id)) return;

View File

@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordUnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete(
data: DiscordGatewayPayload,
shardId: number,
) {
const payload = data.d as DiscordUnavailableGuild;
const payload = data.d as UnavailableGuild;
const guild = await cacheHandlers.get("guilds", payload.id);
if (!guild) return;

View File

@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
import { GuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
export async function handleGuildIntegrationsUpdate(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordGuildIntegrationsUpdate;
const payload = data.d as GuildIntegrationsUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
eventHandlers.guildIntegrationsUpdate?.(guild);

View File

@@ -2,10 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
export async function handleGuildUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuild;
const payload = data.d as Guild;
const newGuild = await cacheHandlers.get("guilds", payload.id);
if (!newGuild) return;

View File

@@ -1,17 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordIntegrationCreateUpdate,
IntegrationCreateUpdate,
} from "../../types/integration/integration_create_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationCreate(
data: DiscordGatewayPayload,
) {
eventHandlers.integrationCreate?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
data.d as IntegrationCreateUpdate,
);
}

View File

@@ -1,15 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { IntegrationCreateUpdate } from "../../types/integration/integration_create_update.ts";
import {
DiscordIntegrationDelete,
} from "../../types/integration/integration_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { IntegrationDelete } from "../../types/integration/integration_delete.ts";
export function handleIntegrationDelete(data: DiscordGatewayPayload) {
eventHandlers.integrationDelete?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationDelete,
),
data.d as IntegrationDelete,
);
}

View File

@@ -1,15 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordIntegrationCreateUpdate,
IntegrationCreateUpdate,
} from "../../types/integration/integration_create_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationUpdate(data: DiscordGatewayPayload) {
eventHandlers.integrationUpdate?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
data.d as IntegrationCreateUpdate,
);
}

View File

@@ -2,15 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { DiscordInteraction } from "../../types/interactions/interaction.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Interaction } from "../../types/interactions/interaction.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordInteraction;
const discordenoMember = payload.guild_id
const payload = data.d as Interaction;
const discordenoMember = payload.guildId
? await structures.createDiscordenoMember(
payload.member as DiscordGuildMemberWithUser,
payload.guild_id,
payload.member as GuildMemberWithUser,
payload.guildId,
)
: undefined;
if (discordenoMember) {

View File

@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordInviteCreate,
InviteCreate,
} from "../../types/invites/invite_create.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { InviteCreate } from "../../types/invites/invite_create.ts";
export function handleInviteCreate(data: DiscordGatewayPayload) {
eventHandlers.inviteCreate?.(
snakeKeysToCamelCase<InviteCreate>(data.d as DiscordInviteCreate),
data.d as InviteCreate,
);
}

View File

@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordInviteDelete,
InviteDelete,
} from "../../types/invites/invite_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { InviteDelete } from "../../types/invites/invite_delete.ts";
export function handleInviteDelete(data: DiscordGatewayPayload) {
eventHandlers.inviteDelete?.(
snakeKeysToCamelCase<InviteDelete>(data.d as DiscordInviteDelete),
data.d as InviteDelete,
);
}

View File

@@ -1,17 +1,17 @@
import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { Collection } from "../../util/collection.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { GuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { Collection } from "../../util/collection.ts";
export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMembersChunk;
const payload = data.d as GuildMembersChunk;
const members = await Promise.all(
payload.members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
member,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -26,17 +26,17 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const resolve = cache.fetchAllMembersProcessingRequests.get(payload.nonce);
if (!resolve) return;
if (payload.chunk_index + 1 === payload.chunk_count) {
if (payload.chunkIndex + 1 === payload.chunkCount) {
cache.fetchAllMembersProcessingRequests.delete(payload.nonce);
// Only 1 chunk most likely is all members or users only request a small amount of users
if (payload.chunk_count === 1) {
if (payload.chunkCount === 1) {
return resolve(new Collection(members.map((m) => [m.id, m])));
}
return resolve(
await cacheHandlers.filter(
"members",
(m) => m.guilds.has(payload.guild_id),
(m) => m.guilds.has(payload.guildId),
),
);
}

View File

@@ -2,17 +2,17 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberAdd } from "../../types/members/guild_member_add.ts";
import { GuildMemberAdd } from "../../types/members/guild_member_add.ts";
export async function handleGuildMemberAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
guild.memberCount++;
const discordenoMember = await structures.createDiscordenoMember(
payload,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberRemove } from "../../types/members/guild_member_remove.ts";
import { GuildMemberRemove } from "../../types/members/guild_member_remove.ts";
export async function handleGuildMemberRemove(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
guild.memberCount--;

View File

@@ -2,22 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberUpdate } from "../../types/members/guild_member_update.ts";
import { GuildMemberUpdate } from "../../types/members/guild_member_update.ts";
export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedMember = await cacheHandlers.get("members", payload.user.id);
const guildMember = cachedMember?.guilds.get(payload.guild_id);
const guildMember = cachedMember?.guilds.get(payload.guildId);
const newMemberData = {
...payload,
// deno-lint-ignore camelcase
premium_since: payload.premium_since || undefined,
// deno-lint-ignore camelcase
joined_at: new Date(guildMember?.joinedAt || Date.now())
premiumSince: payload.premiumSince || undefined,
joinedAt: new Date(guildMember?.joinedAt || Date.now())
.toISOString(),
deaf: guildMember?.deaf || false,
mute: guildMember?.mute || false,
@@ -25,7 +23,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
};
const discordenoMember = await structures.createDiscordenoMember(
newMemberData,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);

View File

@@ -2,12 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { DiscordMessage, Message } from "../../types/messages/message.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Message } from "../../types/messages/message.ts";
export async function handleMessageCreate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase(data.d as DiscordMessage) as Message;
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (channel) channel.lastMessageId = payload.id;
@@ -18,7 +17,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
if (payload.member && guild) {
// If in a guild cache the author as a member
const discordenoMember = await structures.createDiscordenoMember(
{ ...payload.member, user: payload.author } as DiscordGuildMemberWithUser,
{ ...payload.member, user: payload.author } as GuildMemberWithUser,
guild.id,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -29,7 +28,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
// Cache the member if its a valid member
if (mention.member && guild) {
const discordenoMember = await structures.createDiscordenoMember(
{ ...mention.member, user: mention } as DiscordGuildMemberWithUser,
{ ...mention.member, user: mention } as GuildMemberWithUser,
guild.id,
);
@@ -43,7 +42,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
}
const message = await structures.createDiscordenoMessage(
data.d as DiscordMessage,
data.d as Message,
);
// Cache the message
await cacheHandlers.set("messages", payload.id, message);

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageDelete } from "../../types/messages/message_delete.ts";
import { MessageDelete } from "../../types/messages/message_delete.ts";
export async function handleMessageDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageDelete;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as MessageDelete;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
eventHandlers.messageDelete?.(

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
import { MessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as MessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
return Promise.all(payload.ids.map(async (id) => {

View File

@@ -3,14 +3,13 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionAdd,
MessageReactionAdd,
} from "../../types/messages/message_reaction_add.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message) {
const reactionExisted = message.reactions?.find(
@@ -23,7 +22,7 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
else {
const newReaction = {
count: 1,
me: payload.user_id === botId,
me: payload.userId === botId,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
@@ -31,11 +30,11 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
: [newReaction];
}
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
if (payload.member && payload.guild_id) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (payload.member && payload.guildId) {
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (guild) {
const discordenoMember = await structures.createDiscordenoMember(
payload.member,

View File

@@ -2,16 +2,14 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionRemove,
MessageReactionRemove,
} from "../../types/messages/message_reaction_remove.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemove(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message) {
const reaction = message.reactions?.find((reaction) =>
@@ -27,12 +25,12 @@ export async function handleMessageReactionRemove(
}
if (!message.reactions?.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
}
eventHandlers.reactionRemove?.(
snakeKeysToCamelCase<MessageReactionRemove>(payload),
payload,
message,
);
}

View File

@@ -2,25 +2,23 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionRemoveAll,
MessageReactionRemoveAll,
} from "../../types/messages/message_reaction_remove_all.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message?.reactions) {
message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
eventHandlers.reactionRemoveAll?.(
snakeKeysToCamelCase<MessageReactionRemoveAll>(payload),
payload,
message,
);
}

View File

@@ -1,13 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
import { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
export async function handleMessageReactionRemoveEmoji(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message?.reactions) {
message.reactions = message.reactions.filter(
@@ -21,13 +21,13 @@ export async function handleMessageReactionRemoveEmoji(
if (!message.reactions.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
eventHandlers.reactionRemoveEmoji?.(
payload.emoji,
payload.message_id,
payload.channel_id,
payload.guild_id,
payload.messageId,
payload.channelId,
payload.guildId,
);
}

View File

@@ -2,11 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessage } from "../../types/messages/message.ts";
import { Message } from "../../types/messages/message.ts";
export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessage;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
const oldMessage = await cacheHandlers.get("messages", payload.id);
@@ -14,7 +14,7 @@ export async function handleMessageUpdate(data: DiscordGatewayPayload) {
// Messages with embeds can trigger update but they wont have edited_timestamp
if (
!payload.edited_timestamp ||
!payload.editedTimestamp ||
(oldMessage.content === payload.content)
) {
return;

View File

@@ -1,16 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordPresenceUpdate,
PresenceUpdate,
} from "../../types/misc/presence_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { PresenceUpdate } from "../../types/misc/presence_update.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<PresenceUpdate>(
data.d as DiscordPresenceUpdate,
);
const payload = data.d as PresenceUpdate;
const oldPresence = await cacheHandlers.get("presences", payload.user.id);
await cacheHandlers.set("presences", payload.user.id, payload);

View File

@@ -3,9 +3,8 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { initialMemberLoadQueue } from "../../structures/guild.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordReady } from "../../types/gateway/ready.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
import { Ready } from "../../types/gateway/ready.ts";
import { GuildMemberWithUser } from "../../types/mod.ts";
import { ws } from "../../ws/ws.ts";
export function handleReady(
@@ -15,7 +14,7 @@ export function handleReady(
// The bot has already started, the last shard is resumed, however.
if (cache.isReady) return;
const payload = data.d as DiscordReady;
const payload = data.d as Ready;
setBotId(payload.user.id);
setApplicationId(payload.application.id);
@@ -44,7 +43,7 @@ export function handleReady(
// Don't pass the shard itself because unavailableGuilds won't be updated by the GUILD_CREATE event
/** This function checks if the shard is fully loaded */
async function checkReady(payload: DiscordReady, shardId: number, now: number) {
async function checkReady(payload: Ready, shardId: number, now: number) {
const shard = ws.shards.get(shardId);
if (!shard) return;
@@ -100,7 +99,7 @@ async function loaded(shardId: number) {
await Promise.allSettled(
members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(member),
member as GuildMemberWithUser,
guildId,
);

View File

@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordTypingStart,
TypingStart,
} from "../../types/misc/typing_start.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { TypingStart } from "../../types/misc/typing_start.ts";
export function handleTypingStart(data: DiscordGatewayPayload) {
eventHandlers.typingStart?.(
snakeKeysToCamelCase<TypingStart>(data.d as DiscordTypingStart),
data.d as TypingStart,
);
}

View File

@@ -1,11 +1,10 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordUser, User } from "../../types/users/user.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
import { User } from "../../types/users/user.ts";
export async function handleUserUpdate(data: DiscordGatewayPayload) {
const userData = camelKeysToSnakeCase(data.d as DiscordUser) as User;
const userData = data.d as User;
const member = await cacheHandlers.get("members", userData.id);
if (!member) return;

View File

@@ -2,16 +2,16 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleCreate } from "../../types/mod.ts";
import { GuildRoleCreate } from "../../types/mod.ts";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const role = await structures.createDiscordenoRole(payload);
guild.roles = guild.roles.set(payload.role.id, role);
await cacheHandlers.set("guilds", payload.guild_id, guild);
await cacheHandlers.set("guilds", payload.guildId, guild);
eventHandlers.roleCreate?.(guild, role);
}

View File

@@ -1,15 +1,15 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
import { GuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedRole = guild.roles.get(payload.role_id)!;
guild.roles.delete(payload.role_id);
const cachedRole = guild.roles.get(payload.roleId)!;
guild.roles.delete(payload.roleId);
if (cachedRole) eventHandlers.roleDelete?.(guild, cachedRole);
@@ -28,9 +28,9 @@ export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
`2. Running forEach loop in CHANNEL_DELTE file.`,
);
// Member does not have this role
if (!g.roles.includes(payload.role_id)) return;
if (!g.roles.includes(payload.roleId)) return;
// Remove this role from the members cache
g.roles = g.roles.filter((id) => id !== payload.role_id);
g.roles = g.roles.filter((id) => id !== payload.roleId);
cacheHandlers.set("members", member.id, member);
});
});

View File

@@ -2,11 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleUpdate } from "../../types/mod.ts";
import { GuildRoleUpdate } from "../../types/mod.ts";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedRole = guild.roles.get(payload.role.id);

View File

@@ -1,16 +1,10 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordVoiceServerUpdate,
VoiceServerUpdate,
} from "../../types/voice/voice_server_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { VoiceServerUpdate } from "../../types/voice/voice_server_update.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<VoiceServerUpdate>(
data.d as DiscordVoiceServerUpdate,
);
const payload = data.d as VoiceServerUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;

View File

@@ -2,15 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import {
DiscordVoiceState,
VoiceState,
} from "../../types/voice/voice_state.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<VoiceState>(
@@ -23,7 +19,7 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const member = payload.member
? await structures.createDiscordenoMember(
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(payload),
payload.member,
guild.id,
)
: await cacheHandlers.get("members", payload.userId);

View File

@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordWebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
import { WebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
export function handleWebhooksUpdate(data: DiscordGatewayPayload) {
const options = data.d as DiscordWebhookUpdate;
const options = data.d as WebhookUpdate;
eventHandlers.webhooksUpdate?.(
options.channel_id,
options.guild_id,
options.channelId,
options.guildId,
);
}

View File

@@ -2,7 +2,7 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import {
CreateGuildChannel,
@@ -38,7 +38,7 @@ 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(
const result = await rest.runMethod<Channel>(
"post",
endpoints.GUILD_CHANNELS(guildId),
{
@@ -51,7 +51,7 @@ export async function createChannel(
type: options?.type || DiscordChannelTypes.GUILD_TEXT,
reason,
},
)) as DiscordChannel;
);
const discordenoChannel = await structures.createDiscordenoChannel(result);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);

View File

@@ -23,11 +23,9 @@ export async function deleteChannel(
throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED);
}
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_BASE(channelId),
{ reason },
);
return result;
}

View File

@@ -10,10 +10,8 @@ export async function deleteChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
);
return result;
}

View File

@@ -1,6 +1,7 @@
import { eventHandlers } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { ModifyChannel } from "../../types/channels/modify_channel.ts";
import { Channel } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
@@ -58,7 +59,7 @@ export async function editChannel(
}),
};
const result = await rest.runMethod(
return await rest.runMethod<Channel>(
"patch",
endpoints.CHANNEL_BASE(channelId),
{
@@ -66,8 +67,6 @@ export async function editChannel(
reason,
},
);
return result;
}
interface EditChannelRequest {

View File

@@ -15,7 +15,7 @@ export async function editChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"put",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
{
@@ -24,6 +24,4 @@ export async function editChannelOverwrite(
type: options.type,
},
);
return result;
}

View File

@@ -1,5 +1,5 @@
import { rest } from "../../rest/rest.ts";
import { DiscordFollowedChannel } from "../../types/channels/followed_channel.ts";
import { FollowedChannel } from "../../types/channels/followed_channel.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -10,10 +10,13 @@ export async function followChannel(
) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
const data =
(await rest.runMethod("post", endpoints.CHANNEL_FOLLOW(sourceChannelId), {
const data = await rest.runMethod<FollowedChannel>(
"post",
endpoints.CHANNEL_FOLLOW(sourceChannelId),
{
webhook_channel_id: targetChannelId,
})) as DiscordFollowedChannel;
},
);
return data.webhook_id;
return data.webhookId;
}

View File

@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetches a single channel object from the api.
@@ -9,14 +9,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: string, addToCache = true) {
const result = (await rest.runMethod(
const result = await rest.runMethod<Channel>(
"get",
endpoints.CHANNEL_BASE(channelId),
)) as DiscordChannel;
);
const discordenoChannel = await structures.createDiscordenoChannel(
result,
result.guild_id,
result.guildId,
);
if (addToCache) {
await cacheHandlers.set(

View File

@@ -1,23 +1,19 @@
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts";
import { Webhook } from "../../types/webhooks/webhook.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
export async function getChannelWebhooks(channelId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = (await rest.runMethod(
const result = await rest.runMethod<Webhook[]>(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId),
)) as DiscordWebhook[];
);
return new Collection(
result.map((webhook) => [
webhook.id,
snakeKeysToCamelCase<Webhook>(webhook),
]),
result.map((webhook) => [webhook.id, webhook]),
);
}

View File

@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
@@ -10,10 +10,10 @@ 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: string, addToCache = true) {
const result = (await rest.runMethod(
const result = await rest.runMethod<Channel[]>(
"get",
endpoints.GUILD_CHANNELS(guildId),
)) as DiscordChannel[];
);
return new Collection(
(

View File

@@ -1,14 +1,14 @@
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordMessage } from "../../types/messages/message.ts";
import { Message } from "../../types/messages/message.ts";
import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */
export async function getPins(channelId: string) {
const result = (await rest.runMethod(
const result = await rest.runMethod<Message[]>(
"get",
endpoints.CHANNEL_PINS(channelId),
)) as DiscordMessage[];
);
return Promise.all(
result.map((res) => structures.createDiscordenoMessage(res)),

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,
* this endpoint may be called to let the user know that the bot is processing their message.
*/
export async function startTyping(channelId: string): Promise<undefined> {
export async function startTyping(channelId: string) {
const channel = await cacheHandlers.get("channels", channelId);
// If the channel is cached, we can do extra checks/safety
if (channel) {
@@ -35,10 +35,8 @@ export async function startTyping(channelId: string): Promise<undefined> {
}
}
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"post",
endpoints.CHANNEL_TYPING(channelId),
);
return result;
}

View File

@@ -6,16 +6,14 @@ import { endpoints } from "../../util/constants.ts";
export async function swapChannels(
guildId: string,
channelPositions: ModifyGuildChannelPositions[],
): Promise<undefined> {
) {
if (channelPositions.length < 2) {
throw "You must provide at least two channels to be swapped.";
}
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"patch",
endpoints.GUILD_CHANNELS(guildId),
channelPositions,
);
return result;
}

View File

@@ -1,6 +1,7 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { CreateGlobalApplicationCommand } from "../../types/interactions/create_global_application_command.ts";
import { ApplicationCommand } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import {
camelKeysToSnakeCase,
@@ -24,13 +25,11 @@ export async function createSlashCommand(
) {
validateSlashCommands([options], true);
const result = await rest.runMethod(
return await rest.runMethod<ApplicationCommand>(
"post",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
camelKeysToSnakeCase(options),
);
return result;
}

View File

@@ -6,13 +6,11 @@ import { endpoints } from "../../util/constants.ts";
export async function deleteSlashCommand(
id: string,
guildId?: string,
): Promise<undefined> {
const result = await rest.runMethod(
) {
return await rest.runMethod<undefined>(
"delete",
guildId
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id)
: endpoints.COMMANDS_ID(applicationId, id),
);
return result;
}

View File

@@ -6,8 +6,8 @@ import { endpoints } from "../../util/constants.ts";
export async function deleteSlashResponse(
token: string,
messageId?: string,
): Promise<undefined> {
const result = await rest.runMethod(
) {
return await rest.runMethod<undefined>(
"delete",
messageId
? endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(
@@ -17,6 +17,4 @@ export async function deleteSlashResponse(
)
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token),
);
return result;
}

View File

@@ -3,7 +3,6 @@ import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordenoEditWebhookMessage } from "../../types/discordeno/edit_webhook_message.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import { DiscordMessage } from "../../types/messages/message.ts";
import { Errors } from "../../types/misc/errors.ts";
import { endpoints } from "../../util/constants.ts";
@@ -69,10 +68,10 @@ export async function editSlashResponse(
);
// If the original message was edited, this will not return a message
if (!options.messageId) return result;
if (!options.messageId) return result as undefined;
const message = await structures.createDiscordenoMessage(
result as DiscordMessage,
result,
);
return message;
}

View File

@@ -5,12 +5,10 @@ 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. */
export async function getSlashCommand(commandId: string, guildId?: string) {
const result = await rest.runMethod(
return await rest.runMethod<ApplicationCommand>(
"get",
guildId
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
: endpoints.COMMANDS_ID(applicationId, commandId),
);
return result as ApplicationCommand;
}

View File

@@ -6,12 +6,12 @@ import { endpoints } from "../../util/constants.ts";
/** Fetch all of the global commands for your application. */
export async function getSlashCommands(guildId?: string) {
const result = (await rest.runMethod(
const result = await rest.runMethod<ApplicationCommand[]>(
"get",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
)) as ApplicationCommand[];
);
return new Collection(result.map((command) => [command.name, command]));
}

View File

@@ -17,9 +17,13 @@ export async function sendInteractionResponse(
) {
// If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) {
return rest.runMethod("post", endpoints.WEBHOOK(applicationId, token), {
...options,
});
return await rest.runMethod(
"post",
endpoints.WEBHOOK(applicationId, token),
{
...options,
},
);
}
// Expire in 15 minutes
@@ -45,11 +49,9 @@ export async function sendInteractionResponse(
options.data = { ...options.data, allowedMentions: { parse: [] } };
}
const result = await rest.runMethod(
return await rest.runMethod(
"post",
endpoints.INTERACTION_ID_TOKEN(id, token),
options,
);
return result;
}

View File

@@ -1,6 +1,7 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { EditGlobalApplicationCommand } from "../../types/interactions/edit_global_application_command.ts";
import { ApplicationCommand } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
@@ -14,13 +15,11 @@ export async function upsertSlashCommand(
) {
validateSlashCommands([options]);
const result = await rest.runMethod(
return await rest.runMethod<ApplicationCommand>(
"patch",
guildId
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
: endpoints.COMMANDS_ID(applicationId, commandId),
options,
);
return result;
}

View File

@@ -1,6 +1,7 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { EditGlobalApplicationCommand } from "../../types/interactions/edit_global_application_command.ts";
import { ApplicationCommand } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
@@ -15,13 +16,11 @@ export async function upsertSlashCommands(
) {
validateSlashCommands(options);
const result = await rest.runMethod(
return await rest.runMethod<ApplicationCommand[]>(
"put",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
options,
);
return result;
}

View File

@@ -1,11 +1,9 @@
import { rest } from "../../rest/rest.ts";
import {
AddGuildDiscoverySubcategory,
DiscordAddGuildDiscoverySubcategory,
} from "../../types/discovery/add_guild_discovery_subcategory.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */
export async function addDiscoverySubcategory(
@@ -14,10 +12,8 @@ export async function addDiscoverySubcategory(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod<DiscordAddGuildDiscoverySubcategory>(
return await rest.runMethod<AddGuildDiscoverySubcategory>(
"post",
endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId),
);
return snakeKeysToCamelCase<AddGuildDiscoverySubcategory>(result);
}

View File

@@ -1,15 +1,9 @@
import { rest } from "../../rest/rest.ts";
import {
DiscordDiscoveryMetadata,
DiscoveryMetadata,
} from "../../types/discovery/discovery_metadata.ts";
import { DiscoveryMetadata } from "../../types/discovery/discovery_metadata.ts";
import { ModifyGuildDiscoveryMetadata } from "../../types/discovery/modify_guild_discovery_metadata.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
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. */
export async function editDiscovery(
@@ -18,11 +12,9 @@ export async function editDiscovery(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod<DiscordDiscoveryMetadata>(
return await rest.runMethod<DiscoveryMetadata>(
"patch",
endpoints.DISCOVERY_MODIFY(guildId),
camelKeysToSnakeCase(data),
);
return snakeKeysToCamelCase<DiscoveryMetadata>(result);
}

View File

@@ -1,25 +1,18 @@
import { rest } from "../../rest/rest.ts";
import {
DiscordDiscoveryCategory,
DiscoveryCategory,
} from "../../types/discovery/discovery_category.ts";
import { DiscoveryCategory } from "../../types/discovery/discovery_category.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns an array of discovery category objects that can be used when editing guilds */
export async function getDiscoveryCategories() {
const result = await rest.runMethod<DiscordDiscoveryCategory[]>(
const result = await rest.runMethod<DiscoveryCategory[]>(
"get",
endpoints.DISCOVERY_CATEGORIES,
);
return new Collection<number, DiscoveryCategory>(
result.map(
(category) => [
category.id,
snakeKeysToCamelCase<DiscoveryCategory>(category),
],
(category) => [category.id, category],
),
);
}

View File

@@ -3,7 +3,7 @@ import { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts";
import { Emoji } from "../../types/emojis/emoji.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase, 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. */
export async function createEmoji(
@@ -18,11 +18,9 @@ export async function createEmoji(
image = await urlToBase64(image);
}
const result = await rest.runMethod("post", endpoints.GUILD_EMOJIS(guildId), {
return await rest.runMethod<Emoji>("post", endpoints.GUILD_EMOJIS(guildId), {
...options,
name,
image,
});
return snakeKeysToCamelCase<Emoji>(result);
}

View File

@@ -7,14 +7,12 @@ export async function deleteEmoji(
guildId: string,
id: string,
reason?: string,
): Promise<undefined> {
) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_EMOJI(guildId, id),
{ reason },
);
return result;
}

View File

@@ -1,5 +1,6 @@
import { rest } from "../../rest/rest.ts";
import { ModifyGuildEmoji } from "../../types/emojis/modify_guild_emoji.ts";
import { Emoji } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -11,7 +12,7 @@ export async function editEmoji(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
const result = await rest.runMethod(
return await rest.runMethod<Emoji>(
"patch",
endpoints.GUILD_EMOJI(guildId, id),
{
@@ -19,6 +20,4 @@ export async function editEmoji(
roles: options.roles,
},
);
return result;
}

View File

@@ -14,16 +14,16 @@ export async function getEmoji(
emojiId: string,
addToCache = true,
) {
const result = (await rest.runMethod(
const result = await rest.runMethod<Emoji>(
"get",
endpoints.GUILD_EMOJI(guildId, emojiId),
)) as Emoji;
);
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
guild.emojis.set(emojiId, result);
cacheHandlers.set(
await cacheHandlers.set(
"guilds",
guildId,
guild,

View File

@@ -12,8 +12,10 @@ 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: string, addToCache = true) {
const result =
(await rest.runMethod("get", endpoints.GUILD_EMOJIS(guildId))) as Emoji[];
const result = await rest.runMethod<Emoji[]>(
"get",
endpoints.GUILD_EMOJIS(guildId),
);
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
@@ -27,7 +29,7 @@ export async function getEmojis(guildId: string, addToCache = true) {
guild.emojis.set(emoji.id!, emoji);
});
cacheHandlers.set("guilds", guildId, guild);
await cacheHandlers.set("guilds", guildId, guild);
}
return new Collection(result.map((e) => [e.id!, e]));

View File

@@ -3,17 +3,17 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { CreateGuild } from "../../types/guilds/create_guild.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { endpoints } from "../../util/constants.ts";
import { getMember } from "../members/get_member.ts";
/** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */
export async function createGuild(options: CreateGuild) {
const result = (await rest.runMethod(
const result = await rest.runMethod<Guild>(
"post",
endpoints.GUILDS,
options,
)) as DiscordGuild;
);
const guild = await structures.createDiscordenoGuild(result, 0);
// MANUALLY CACHE THE GUILD

View File

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

View File

@@ -1,6 +1,6 @@
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { ModifyGuild } from "../../types/guilds/modify_guild.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -22,11 +22,12 @@ export async function editGuild(guildId: string, options: ModifyGuild) {
options.splash = await urlToBase64(options.splash);
}
const result = await rest.runMethod(
const result = await rest.runMethod<Guild>(
"patch",
endpoints.GUILDS_BASE(guildId),
options,
) as DiscordGuild;
);
// TODO: use ws.botGatewayData to calculate the shard ID
return structures.createDiscordenoGuild(result, -1);
}

View File

@@ -2,20 +2,15 @@ import { rest } from "../../rest/rest.ts";
import { ModifyGuildWelcomeScreen } from "../../types/guilds/modify_guild_welcome_screen.ts";
import { WelcomeScreen } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
export async function editWelcomeScreen(
guildId: string,
options: ModifyGuildWelcomeScreen,
) {
const result = await rest.runMethod(
return await rest.runMethod<WelcomeScreen>(
"patch",
endpoints.GUILD_WELCOME_SCREEN(guildId),
camelKeysToSnakeCase(options),
);
return snakeKeysToCamelCase<WelcomeScreen>(result);
}

View File

@@ -2,7 +2,6 @@ import { rest } from "../../rest/rest.ts";
import { GuildWidget } from "../../types/guilds/guild_widget.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
export async function editWidget(
@@ -12,7 +11,7 @@ export async function editWidget(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod(
return await rest.runMethod<GuildWidget>(
"patch",
endpoints.GUILD_WIDGET(guildId),
{
@@ -20,6 +19,4 @@ export async function editWidget(
channel_id: channelId,
},
);
return snakeKeysToCamelCase<GuildWidget>(result);
}

View File

@@ -3,10 +3,7 @@ import { AuditLog } from "../../types/audit_log/audit_log.ts";
import { GetGuildAuditLog } from "../../types/audit_log/get_guild_audit_log.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
export async function getAuditLogs(
@@ -15,7 +12,7 @@ export async function getAuditLogs(
) {
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
const result = await rest.runMethod(
return await rest.runMethod<AuditLog>(
"get",
endpoints.GUILD_AUDIT_LOGS(guildId),
camelKeysToSnakeCase({
@@ -25,6 +22,4 @@ export async function getAuditLogs(
: 50,
}),
);
return snakeKeysToCamelCase<AuditLog>(result);
}

View File

@@ -4,7 +4,5 @@ import { endpoints } from "../../util/constants.ts";
/** Returns an array of voice regions that can be used when creating servers. */
export async function getAvailableVoiceRegions() {
const result = await rest.runMethod("get", endpoints.VOICE_REGIONS);
return result as VoiceRegion;
return await rest.runMethod<VoiceRegion>("get", endpoints.VOICE_REGIONS);
}

View File

@@ -2,16 +2,13 @@ import { rest } from "../../rest/rest.ts";
import { Ban } from "../../types/guilds/ban.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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. */
export async function getBan(guildId: string, memberId: string) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const result = await rest.runMethod(
return await rest.runMethod<Ban>(
"get",
endpoints.GUILD_BAN(guildId, memberId),
);
return snakeKeysToCamelCase<Ban>(result);
}

View File

@@ -1,20 +1,19 @@
import { rest } from "../../rest/rest.ts";
import { Ban, DiscordBan } from "../../types/guilds/ban.ts";
import { Ban } from "../../types/guilds/ban.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
export async function getBans(guildId: string) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const results = (await rest.runMethod(
const results = await rest.runMethod<Ban[]>(
"get",
endpoints.GUILD_BANS(guildId),
)) as DiscordBan[];
);
return new Collection<string, Ban>(
results.map((res) => [res.user.id, snakeKeysToCamelCase<Ban>(res)]),
results.map((res) => [res.user.id, res]),
);
}

View File

@@ -1,7 +1,9 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts";
/**
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
@@ -10,10 +12,29 @@ import { snakeKeysToCamelCase } from "../../util/utils.ts";
* This function fetches a guild's data. This is not the same data as a GUILD_CREATE.
* So it does not cache the guild, you must do it manually.
* */
export async function getGuild(guildId: string, counts = true) {
const result = await rest.runMethod("get", endpoints.GUILDS_BASE(guildId), {
with_counts: counts,
});
export async function getGuild(
guildId: string,
options: { counts?: boolean; addToCache?: boolean } = {
counts: true,
addToCache: true,
},
) {
const result = await rest.runMethod<Guild>(
"get",
endpoints.GUILDS_BASE(guildId),
{
with_counts: options.counts,
},
);
return snakeKeysToCamelCase<Guild>(result);
const structure = await structures.createDiscordenoGuild(
result,
Number((BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards)),
);
if (options.addToCache) {
await cacheHandlers.set("guilds", guildId, structure);
}
return structure;
}

View File

@@ -1,11 +1,11 @@
import { rest } from "../../rest/rest.ts";
import { GuildPreview } from "../../types/guilds/guild_preview.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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: string) {
const result = await rest.runMethod("get", endpoints.GUILD_PREVIEW(guildId));
return snakeKeysToCamelCase<GuildPreview>(result);
return await rest.runMethod<GuildPreview>(
"get",
endpoints.GUILD_PREVIEW(guildId),
);
}

View File

@@ -1,18 +1,15 @@
import { rest } from "../../rest/rest.ts";
import { InviteMetadata } from "../../types/invites/invite_metadata.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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. */
export async function getVanityURL(guildId: string) {
const result = await rest.runMethod(
"get",
endpoints.GUILD_VANITY_URL(guildId),
);
return snakeKeysToCamelCase<
return await rest.runMethod<
(Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">) | {
code: null;
}
>(result);
>(
"get",
endpoints.GUILD_VANITY_URL(guildId),
);
}

View File

@@ -1,22 +1,16 @@
import { rest } from "../../rest/rest.ts";
import {
DiscordVoiceRegion,
VoiceRegion,
} from "../../types/voice/voice_region.ts";
import { VoiceRegion } from "../../types/voice/voice_region.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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: string) {
const result = await rest.runMethod(
const result = await rest.runMethod<VoiceRegion[]>(
"get",
endpoints.GUILD_REGIONS(guildId),
) as DiscordVoiceRegion[];
const convertedRegions = snakeKeysToCamelCase<VoiceRegion[]>(result);
);
return new Collection<string, VoiceRegion>(
convertedRegions.map((region) => [region.id, region]),
result.map((region) => [region.id, region]),
);
}

View File

@@ -1,13 +1,10 @@
import { rest } from "../../rest/rest.ts";
import { WelcomeScreen } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function getWelcomeScreen(guildId: string) {
const result = await rest.runMethod(
return await rest.runMethod<WelcomeScreen>(
"get",
endpoints.GUILD_WELCOME_SCREEN(guildId),
);
return snakeKeysToCamelCase<WelcomeScreen>(result);
}

View File

@@ -11,5 +11,6 @@ export async function getWidget(guildId: string, options?: { force: boolean }) {
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
}
return rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
// TODO: add return type
return await rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
}

View File

@@ -7,7 +7,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getWidgetSettings(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod("get", endpoints.GUILD_WIDGET(guildId));
return result as GuildWidget;
return await rest.runMethod<GuildWidget>(
"get",
endpoints.GUILD_WIDGET(guildId),
);
}

View File

@@ -2,8 +2,9 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Leave a guild */
export async function leaveGuild(guildId: string): Promise<undefined> {
const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId));
return result;
export async function leaveGuild(guildId: string) {
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_LEAVE(guildId),
);
}

View File

@@ -6,10 +6,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function deleteIntegration(guildId: string, id: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILD_INTEGRATION(guildId, id),
);
return result;
}

View File

@@ -1,4 +1,5 @@
import { rest } from "../../rest/rest.ts";
import { Integration } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -6,10 +7,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getIntegrations(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod(
return await rest.runMethod<Integration>(
"get",
endpoints.GUILD_INTEGRATIONS(guildId),
);
return result;
}

View File

@@ -1,5 +1,6 @@
import { rest } from "../../rest/rest.ts";
import { CreateChannelInvite } from "../../types/invites/create_channel_invite.ts";
import { Invite } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -12,11 +13,9 @@ export async function createInvite(
// TODO: add proper options validation
const result = await rest.runMethod(
return await rest.runMethod<Invite>(
"post",
endpoints.CHANNEL_INVITES(channelId),
options,
);
return result;
}

View File

@@ -1,13 +1,12 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Invite } from "../../types/invites/invite.ts";
import { Errors } from "../../types/misc/errors.ts";
import { endpoints } from "../../util/constants.ts";
import {
botHasChannelPermissions,
requireBotGuildPermissions,
} from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */
export async function deleteInvite(channelId: string, inviteCode: string) {
@@ -23,10 +22,8 @@ export async function deleteInvite(channelId: string, inviteCode: string) {
await requireBotGuildPermissions(channel!.guildId, ["MANAGE_GUILD"]);
}
const result: DiscordInvite = await rest.runMethod(
return await rest.runMethod<Invite>(
"delete",
endpoints.INVITE(inviteCode),
);
return snakeKeysToCamelCase<Invite>(result);
}

View File

@@ -1,20 +1,19 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Invite } from "../../types/invites/invite.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
export async function getChannelInvites(channelId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]);
const result = (await rest.runMethod(
const result = await rest.runMethod<Invite[]>(
"get",
endpoints.CHANNEL_INVITES(channelId),
)) as DiscordInvite[];
);
return new Collection(
result.map((invite) => [invite.code, snakeKeysToCamelCase<Invite>(invite)]),
result.map((invite) => [invite.code, invite]),
);
}

View File

@@ -1,14 +1,11 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Invite } from "../../types/invites/invite.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns an invite for the given code or throws an error if the invite doesn't exists. */
export async function getInvite(inviteCode: string) {
const result: DiscordInvite = await rest.runMethod(
return await rest.runMethod<Invite>(
"get",
endpoints.INVITE(inviteCode),
);
return snakeKeysToCamelCase<Invite>(result);
}

View File

@@ -1,20 +1,19 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Invite } from "../../types/invites/invite.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */
export async function getInvites(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = (await rest.runMethod(
const result = await rest.runMethod<Invite[]>(
"get",
endpoints.GUILD_INVITES(guildId),
)) as DiscordInvite[];
);
return new Collection(
result.map((invite) => [invite.code, snakeKeysToCamelCase<Invite>(invite)]),
result.map((invite) => [invite.code, invite]),
);
}

View File

@@ -1,5 +1,5 @@
import { CreateGuildBan } from "../../types/guilds/create_guild_ban.ts";
import { rest } from "../../rest/rest.ts";
import { CreateGuildBan } from "../../types/guilds/create_guild_ban.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
@@ -12,13 +12,11 @@ export async function ban(
) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const result = await rest.runMethod(
return await rest.runMethod<undefined>(
"put",
endpoints.GUILD_BAN(guildId, id),
camelKeysToSnakeCase(options),
);
return result;
}
// aliases

View File

@@ -9,9 +9,13 @@ export async function editBotNickname(
) {
await requireBotGuildPermissions(guildId, ["CHANGE_NICKNAME"]);
const response = await rest.runMethod("patch", endpoints.USER_NICK(guildId), {
nick: nickname,
}) as { nick: string };
const response = await rest.runMethod<{ nick: string }>(
"patch",
endpoints.USER_NICK(guildId),
{
nick: nickname,
},
);
return response.nick;
}

View File

@@ -1,5 +1,6 @@
import { rest } from "../../rest/rest.ts";
import { Errors } from "../../types/misc/errors.ts";
import { User } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { urlToBase64 } from "../../util/utils.ts";
@@ -26,10 +27,9 @@ export async function editBotProfile(username?: string, botAvatarURL?: string) {
}
const avatar = botAvatarURL ? await urlToBase64(botAvatarURL) : undefined;
const result = await rest.runMethod("patch", endpoints.USER_BOT, {
return await rest.runMethod<User>("patch", endpoints.USER_BOT, {
username: username?.trim(),
avatar,
});
return result;
}

View File

@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGuildMember } from "../../types/guilds/guild_member.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Errors } from "../../types/misc/errors.ts";
import { ModifyGuildMember } from "../../types/mod.ts";
import { PermissionStrings } from "../../types/permissions/permission_strings.ts";
@@ -10,10 +10,7 @@ import {
requireBotChannelPermissions,
requireBotGuildPermissions,
} from "../../util/permissions.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Edit the member */
export async function editMember(
@@ -72,13 +69,14 @@ export async function editMember(
await requireBotGuildPermissions(guildId, [...requiredPerms]);
const result = await rest.runMethod(
const result = await rest.runMethod<GuildMemberWithUser>(
"patch",
endpoints.GUILD_MEMBER(guildId, memberId),
camelKeysToSnakeCase(options),
) as DiscordGuildMember;
);
const member = await structures.createDiscordenoMember(
snakeKeysToCamelCase(result),
result,
guildId,
);

View File

@@ -1,7 +1,7 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns a guild member object for the specified user.
@@ -16,7 +16,7 @@ export async function getMember(
const guild = await cacheHandlers.get("guilds", guildId);
if (!guild && !options?.force) return;
const data: DiscordGuildMemberWithUser = (await rest.runMethod(
const data = (await rest.runMethod<GuildMemberWithUser>(
"get",
endpoints.GUILD_MEMBER(guildId, id),
));

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