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
+4 -6
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 { structures } from "../../structures/mod.ts";
import {
CreateGuildChannel,
@@ -28,9 +28,8 @@ export async function createChannel(
await requireBotGuildPermissions(guildId, [...requiredPerms]);
const result = (await RequestManager.post(
endpoints.GUILD_CHANNELS(guildId),
{
const result =
(await rest.runMethod("post", endpoints.GUILD_CHANNELS(guildId), {
...options,
name,
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
@@ -40,8 +39,7 @@ export async function createChannel(
deny: calculateBits(perm.deny),
})),
type: options?.type || DiscordChannelTypes.GUILD_TEXT,
},
)) as DiscordChannel;
})) as DiscordChannel;
const channelStruct = await structures.createChannelStruct(result);
await cacheHandlers.set("channels", channelStruct.id, channelStruct);
+3 -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 { Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
@@ -23,7 +23,8 @@ export async function deleteChannel(
throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED);
}
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_BASE(channelId),
{ reason },
);
@@ -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,7 +10,8 @@ export async function deleteChannelOverwrite(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
);
+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 {
calculateBits,
@@ -56,10 +56,14 @@ export async function editChannel(
}),
};
const result = await RequestManager.patch(endpoints.CHANNEL_BASE(channelId), {
...payload,
reason,
});
const result = await rest.runMethod(
"patch",
endpoints.CHANNEL_BASE(channelId),
{
...payload,
reason,
},
);
return result;
}
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { Overwrite } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import {
@@ -15,7 +15,8 @@ export async function editChannelOverwrite(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await RequestManager.put(
const result = await rest.runMethod(
"put",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
{
allow: calculateBits(options.allow),
+4 -6
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordFollowedChannel } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -10,12 +10,10 @@ export async function followChannel(
) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
const data = (await RequestManager.post(
endpoints.CHANNEL_FOLLOW(sourceChannelId),
{
const data =
(await rest.runMethod("post", endpoints.CHANNEL_FOLLOW(sourceChannelId), {
webhook_channel_id: targetChannelId,
},
)) as DiscordFollowedChannel;
})) as DiscordFollowedChannel;
return data.webhook_id;
}
+6 -4
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 { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
@@ -9,9 +9,11 @@ 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 RequestManager.get(
endpoints.CHANNEL_BASE(channelId),
)) as DiscordChannel;
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_BASE(channelId),
)) as DiscordChannel;
const channelStruct = await structures.createChannelStruct(
result,
+3 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -7,7 +7,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function getChannelWebhooks(channelId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = await RequestManager.get(
const result = await rest.runMethod(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId),
);
+6 -4
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 { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
@@ -9,9 +9,11 @@ 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 RequestManager.get(
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
const result =
(await rest.runMethod(
"get",
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
return Promise.all(result.map(async (res) => {
const channelStruct = await structures.createChannelStruct(res, guildId);
+6 -4
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 { DiscordMessage } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */
export async function getPins(channelId: string) {
const result = (await RequestManager.get(
endpoints.CHANNEL_PINS(channelId),
)) as DiscordMessage[];
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_PINS(channelId),
)) as DiscordMessage[];
return Promise.all(
result.map((res) => structures.createMessageStruct(res)),
+5 -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 { DiscordChannelTypes, Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { botHasChannelPermissions } from "../../util/permissions.ts";
@@ -34,7 +34,10 @@ export async function startTyping(channelId: string) {
}
}
const result = await RequestManager.post(endpoints.CHANNEL_TYPING(channelId));
const result = await rest.runMethod(
"post",
endpoints.CHANNEL_TYPING(channelId),
);
return result;
}
+3 -2
View File
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { ModifyGuildChannelPositions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
@@ -11,7 +11,8 @@ export async function swapChannels(
throw "You must provide at least two channels to be swapped.";
}
const result = await RequestManager.patch(
const result = await rest.runMethod(
"patch",
endpoints.GUILD_CHANNELS(guildId),
channelPositions,
);