refactor: remove RequestManager and use runMethod() (#732)

* fix(rest/process_request): use DiscordHTTPResponseCodes

* refactor: remove RequestManager

* refactor: remove RequestManager and use runMethod()
This commit is contained in:
ayntee
2021-04-02 23:18:51 +04:00
committed by GitHub
parent ec9ceaab04
commit 5f1b82a4e8
106 changed files with 418 additions and 326 deletions
+7 -5
View File
@@ -1,13 +1,15 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { endpoints } from "../../util/constants.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: CreateServerOptions) {
const guild = (await RequestManager.post(
endpoints.GUILDS,
options,
)) as CreateGuildPayload;
const guild =
(await rest.runMethod(
"post",
endpoints.GUILDS,
options,
)) as CreateGuildPayload;
return structures.createGuildStruct(guild, 0);
}
+2 -2
View File
@@ -1,10 +1,10 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event.
*/
export async function deleteServer(guildId: string) {
const result = await RequestManager.delete(endpoints.GUILDS_BASE(guildId));
const result = await rest.runMethod("delete", endpoints.GUILDS_BASE(guildId));
return result;
}
+3 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { urlToBase64 } from "../../util/utils.ts";
@@ -19,7 +19,8 @@ export async function editGuild(guildId: string, options: GuildEditOptions) {
options.splash = await urlToBase64(options.splash);
}
const result = await RequestManager.patch(
const result = await rest.runMethod(
"patch",
endpoints.GUILDS_BASE(guildId),
options,
);
+9 -5
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -10,10 +10,14 @@ export async function editWidget(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await RequestManager.patch(endpoints.GUILD_WIDGET(guildId), {
enabled,
channel_id: channelId,
});
const result = await rest.runMethod(
"patch",
endpoints.GUILD_WIDGET(guildId),
{
enabled,
channel_id: channelId,
},
);
return result;
}
+13 -10
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -9,15 +9,18 @@ export async function getAuditLogs(
) {
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
const result = await RequestManager.get(endpoints.GUILD_AUDIT_LOGS(guildId), {
...options,
action_type: options.action_type
? AuditLogs[options.action_type]
: undefined,
limit: options.limit && options.limit >= 1 && options.limit <= 100
? options.limit
: 50,
});
const result = await rest.runMethod(
"get",
endpoints.GUILD_AUDIT_LOGS(guildId),
{
...options,
action_type: options.action_type
? AuditLogs[options.action_type]
: undefined,
limit: options.limit && options.limit >= 1 && options.limit <= 100
? options.limit : 50,
},
);
return result;
}
@@ -1,9 +1,9 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
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 RequestManager.get(endpoints.VOICE_REGIONS);
const result = await rest.runMethod("get", endpoints.VOICE_REGIONS);
return result;
}
+3 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -6,7 +6,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getBan(guildId: string, memberId: string) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const result = await RequestManager.get(
const result = await rest.runMethod(
"get",
endpoints.GUILD_BAN(guildId, memberId),
);
+6 -4
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -7,9 +7,11 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getBans(guildId: string) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const results = (await RequestManager.get(
endpoints.GUILD_BANS(guildId),
)) as BannedUser[];
const results =
(await rest.runMethod(
"get",
endpoints.GUILD_BANS(guildId),
)) as BannedUser[];
return new Collection<string, BannedUser>(
results.map((res) => [res.user.id, res]),
+2 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/**
@@ -9,7 +9,7 @@ import { endpoints } from "../../util/constants.ts";
* So it does not cache the guild, you must do it manually.
* */
export async function getGuild(guildId: string, counts = true) {
const result = await RequestManager.get(endpoints.GUILDS_BASE(guildId), {
const result = await rest.runMethod("get", endpoints.GUILDS_BASE(guildId), {
with_counts: counts,
});
+2 -2
View File
@@ -1,9 +1,9 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */
export async function getGuildPreview(guildId: string) {
const result = await RequestManager.get(endpoints.GUILD_PREVIEW(guildId));
const result = await rest.runMethod("get", endpoints.GUILD_PREVIEW(guildId));
return result;
}
+3 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
@@ -12,7 +12,8 @@ export async function getPruneCount(guildId: string, options?: PruneOptions) {
await requireBotGuildPermissions(guildId, ["KICK_MEMBERS"]);
const result = await RequestManager.get(
const result = await rest.runMethod(
"get",
endpoints.GUILD_PRUNE(guildId),
camelKeysToSnakeCase(options ?? {}),
) as PrunePayload;
+5 -2
View File
@@ -1,9 +1,12 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */
export async function getVanityURL(guildId: string) {
const result = await RequestManager.get(endpoints.GUILD_VANITY_URL(guildId));
const result = await rest.runMethod(
"get",
endpoints.GUILD_VANITY_URL(guildId),
);
return result;
}
+2 -2
View File
@@ -1,9 +1,9 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */
export async function getVoiceRegions(guildId: string) {
const result = await RequestManager.get(endpoints.GUILD_REGIONS(guildId));
const result = await rest.runMethod("get", endpoints.GUILD_REGIONS(guildId));
return result;
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns the widget for the guild. */
@@ -10,5 +10,5 @@ export async function getWidget(guildId: string, options?: { force: boolean }) {
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
}
return RequestManager.get(`${endpoints.GUILD_WIDGET(guildId)}.json`);
return rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -6,7 +6,7 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getWidgetSettings(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await RequestManager.get(endpoints.GUILD_WIDGET(guildId));
const result = await rest.runMethod("get", endpoints.GUILD_WIDGET(guildId));
return result;
}
+2 -2
View File
@@ -1,9 +1,9 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Leave a guild */
export async function leaveGuild(guildId: string) {
const result = await RequestManager.delete(endpoints.GUILD_LEAVE(guildId));
const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId));
return result;
}