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
+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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -19,7 +19,8 @@ export async function addReaction(
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.put(
const result = await rest.runMethod(
"put",
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
);
+3 -2
View File
@@ -1,6 +1,6 @@
import { botId } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { delay } from "../../util/utils.ts";
@@ -20,7 +20,8 @@ export async function deleteMessage(
if (delayMilliseconds) await delay(delayMilliseconds);
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_MESSAGE(channelId, messageId),
{ reason },
);
+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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -20,7 +20,8 @@ export async function deleteMessages(
);
}
const result = await RequestManager.post(
const result = await rest.runMethod(
"post",
endpoints.CHANNEL_BULK_DELETE(channelId),
{
messages: ids.splice(0, 100),
+3 -2
View File
@@ -1,5 +1,5 @@
import { botId } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { Message, structures } from "../../structures/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -25,7 +25,8 @@ export async function editMessage(
throw new Error(Errors.MESSAGE_MAX_LENGTH);
}
const result = await RequestManager.patch(
const result = await rest.runMethod(
"patch",
endpoints.CHANNEL_MESSAGE(message.channelId, message.id),
content,
);
+6 -4
View File
@@ -1,4 +1,4 @@
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";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -10,9 +10,11 @@ export async function getMessage(channelId: string, id: string) {
"READ_MESSAGE_HISTORY",
]);
const result = (await RequestManager.get(
endpoints.CHANNEL_MESSAGE(channelId, id),
)) as MessageCreateOptions;
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE(channelId, id),
)) as MessageCreateOptions;
return structures.createMessageStruct(result);
}
+7 -5
View File
@@ -1,4 +1,4 @@
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";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -19,10 +19,12 @@ export async function getMessages(
if (options?.limit && options.limit > 100) return;
const result = (await RequestManager.get(
endpoints.CHANNEL_MESSAGES(channelId),
options,
)) as MessageCreateOptions[];
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGES(channelId),
options,
)) as MessageCreateOptions[];
return Promise.all(
result.map((res) => structures.createMessageStruct(res)),
+7 -5
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";
@@ -9,10 +9,12 @@ export async function getReactions(
reaction: string,
options?: DiscordGetReactionsParams,
) {
const users = (await RequestManager.get(
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
options,
)) as UserPayload[];
const users =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
options,
)) as UserPayload[];
return new Collection(users.map((user) => [user.id, user]));
}
+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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -6,7 +6,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function pin(channelId: string, messageId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
const result = await RequestManager.put(
const result = await rest.runMethod(
"put",
endpoints.CHANNEL_PIN(channelId, messageId),
);
+6 -4
View File
@@ -1,12 +1,14 @@
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";
/** Crosspost a message in a News Channel to following channels. */
export async function publishMessage(channelId: string, messageId: string) {
const data = (await RequestManager.post(
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId),
)) as MessageCreateOptions;
const data =
(await rest.runMethod(
"post",
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId),
)) as MessageCreateOptions;
return structures.createMessageStruct(data);
}
+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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -6,7 +6,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function removeAllReactions(channelId: string, messageId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_MESSAGE_REACTIONS(channelId, messageId),
);
+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";
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
@@ -13,7 +13,8 @@ export async function removeReaction(
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
);
@@ -1,4 +1,4 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -16,7 +16,8 @@ export async function removeReactionEmoji(
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
);
+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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -17,7 +17,8 @@ export async function removeUserReaction(
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelId,
messageId,
+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 { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { PermissionStrings } from "../../types/mod.ts";
@@ -70,9 +70,8 @@ export async function sendMessage(
}
}
const result = (await RequestManager.post(
endpoints.CHANNEL_MESSAGES(channelId),
{
const result =
(await rest.runMethod("post", endpoints.CHANNEL_MESSAGES(channelId), {
...content,
allowed_mentions: content.mentions
? {
@@ -88,8 +87,7 @@ export async function sendMessage(
},
}
: {}),
},
)) as MessageCreateOptions;
})) as MessageCreateOptions;
return structures.createMessageStruct(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 { requireBotChannelPermissions } from "../../util/permissions.ts";
@@ -6,7 +6,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function unpin(channelId: string, messageId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
const result = await RequestManager.delete(
const result = await rest.runMethod(
"delete",
endpoints.CHANNEL_PIN(channelId, messageId),
);