fix: return types [] => Collection

This commit is contained in:
Skillz4Killz
2021-04-13 17:53:49 +00:00
committed by GitHub
parent f8d6a74948
commit c2f2afb211
8 changed files with 91 additions and 34 deletions

View File

@@ -1,16 +1,22 @@
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
import { DiscordWebhook, 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(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId),
);
endpoints.CHANNEL_WEBHOOKS(channelId)
)) as DiscordWebhook[];
return result as DiscordWebhook[];
return new Collection(
result
.map((webhook) => snakeKeysToCamelCase<Webhook>(webhook))
.map((webhook) => [webhook.id, webhook])
);
}

View File

@@ -2,6 +2,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 { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns a list of guild channel objects.
@@ -11,22 +12,28 @@ import { endpoints } from "../../util/constants.ts";
export async function getChannels(guildId: string, addToCache = true) {
const result = (await rest.runMethod(
"get",
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
endpoints.GUILD_CHANNELS(guildId)
)) as DiscordChannel[];
return Promise.all(result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId,
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel,
);
}
return new Collection(
(
await Promise.all(
result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
}
return discordenoChannel;
}));
return discordenoChannel;
})
)
).map((c) => [c.id, c])
);
}

View File

@@ -3,6 +3,7 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { Emoji } from "../../types/emojis/emoji.ts";
import { Errors } from "../../types/misc/errors.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/**
@@ -29,5 +30,5 @@ export async function getEmojis(guildId: string, addToCache = true) {
cacheHandlers.set("guilds", guildId, guild);
}
return result;
return new Collection(result.map(e => [e.id!, e]));
}

View File

@@ -1,15 +1,22 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, 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(
"get",
endpoints.CHANNEL_INVITES(channelId),
);
endpoints.CHANNEL_INVITES(channelId)
)) as DiscordInvite[];
return result;
return new Collection(
result
.map((invite) => snakeKeysToCamelCase<Invite>(invite))
.map((invite) => [invite.code, invite])
);
}

View File

@@ -1,12 +1,22 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, 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("get", endpoints.GUILD_INVITES(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_INVITES(guildId)
)) as DiscordInvite[];
return result;
return new Collection(
result
.map((invite) => snakeKeysToCamelCase<Invite>(invite))
.map((invite) => [invite.code, invite])
);
}

View File

@@ -1,6 +1,9 @@
import { rest } from "../../rest/rest.ts";
import { DiscordRole, Role } from "../../types/permissions/role.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 role objects for the guild.
*
@@ -9,7 +12,14 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getRoles(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await rest.runMethod("get", endpoints.GUILD_ROLES(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_ROLES(guildId)
)) as DiscordRole[];
return result;
return new Collection(
result
.map((role) => snakeKeysToCamelCase<Role>(role))
.map((role) => [role.id, role])
);
}

View File

@@ -3,6 +3,7 @@ import { structures } from "../../structures/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { DiscordTemplate } from "../../types/templates/template.ts";
import { Collection } from "../../util/collection.ts";
/**
* Returns an array of templates.
@@ -13,8 +14,13 @@ export async function getGuildTemplates(guildId: string) {
const templates = (await rest.runMethod(
"get",
endpoints.GUILD_TEMPLATES(guildId),
endpoints.GUILD_TEMPLATES(guildId)
)) as DiscordTemplate[];
return templates.map((template) => structures.createTemplateStruct(template));
return new Collection(
templates.map((template) => [
template.code,
structures.createTemplateStruct(template),
])
);
}

View File

@@ -1,12 +1,22 @@
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.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 guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
export async function getWebhooks(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod("get", endpoints.GUILD_WEBHOOKS(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_WEBHOOKS(guildId)
)) as DiscordWebhook[];
return result;
return new Collection(
result
.map((webhook) => snakeKeysToCamelCase<Webhook>(webhook))
.map((webhook) => [webhook.id, webhook])
);
}