This commit is contained in:
ITOH
2021-04-24 19:38:06 +02:00
parent a1d0986edf
commit 933a5c70eb
12 changed files with 57 additions and 53 deletions
+1 -4
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);
}
+2 -7
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);
}
@@ -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);
}
+1 -4
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);
}
+4 -5
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]),
);
}
+27 -6
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,
(BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards),
);
if (options.addToCache) {
await cacheHandlers.set("guilds", guildId, structure);
}
return structure;
}
+4 -4
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),
);
}
+5 -8
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),
);
}
+1 -4
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);
}
+2 -1
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`);
}
+4 -3
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),
);
}
+5 -4
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),
);
}