refactor(helpers): separate functions into files (#667)

* refactor(helpers): separate functions into files

* idk

* idk
This commit is contained in:
ayntee
2021-03-13 08:10:31 -05:00
committed by GitHub
parent 88ce4da555
commit e9cbbbff7c
143 changed files with 3362 additions and 2915 deletions
+27
View File
@@ -0,0 +1,27 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
export async function addReaction(
channelID: string,
messageID: string,
reaction: string,
) {
await requireBotChannelPermissions(channelID, [
"ADD_REACTIONS",
"READ_MESSAGE_HISTORY",
]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.put(
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelID, messageID, reaction),
);
return result;
}
+19
View File
@@ -0,0 +1,19 @@
import { addReaction } from "./add_reaction.ts";
/** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
export async function addReactions(
channelID: string,
messageID: string,
reactions: string[],
ordered = false,
) {
if (!ordered) {
await Promise.all(
reactions.map((reaction) => addReaction(channelID, messageID, reaction)),
);
} else {
for (const reaction of reactions) {
await addReaction(channelID, messageID, reaction);
}
}
}
+27
View File
@@ -0,0 +1,27 @@
import { botID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { Message } from "../../structures/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { delay } from "../../util/utils.ts";
/** Delete a message */
export async function deleteMessage(
message: Message,
reason?: string,
delayMilliseconds = 0,
) {
if (message.author.id !== botID) {
// This needs to check the channels permission not the guild permission
await requireBotChannelPermissions(message.channelID, ["MANAGE_MESSAGES"]);
}
if (delayMilliseconds) await delay(delayMilliseconds);
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
{ reason },
);
return result;
}
@@ -0,0 +1,25 @@
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { delay } from "../../util/utils.ts";
import { deleteMessage } from "./delete_message.ts";
/** Delete a message with the channel id and message id only. */
export async function deleteMessageByID(
channelID: string,
messageID: string,
reason?: string,
delayMilliseconds = 0,
) {
const message = await cacheHandlers.get("messages", messageID);
if (message) return deleteMessage(message, reason, delayMilliseconds);
if (delayMilliseconds) await delay(delayMilliseconds);
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE(channelID, messageID),
{ reason },
);
return result;
}
+33
View File
@@ -0,0 +1,33 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
export async function deleteMessages(
channelID: string,
ids: string[],
reason?: string,
) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
if (ids.length < 2) {
throw new Error(Errors.DELETE_MESSAGES_MIN);
}
if (ids.length > 100) {
console.warn(
`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`,
);
}
const result = await RequestManager.post(
endpoints.CHANNEL_BULK_DELETE(channelID),
{
messages: ids.splice(0, 100),
reason,
},
);
return result;
}
+40
View File
@@ -0,0 +1,40 @@
import { botID } from "../../bot.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { Message, structures } from "../../structures/mod.ts";
import {
Errors,
MessageContent,
MessageCreateOptions,
Permission,
} from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Edit the message. */
export async function editMessage(
message: Message,
content: string | MessageContent,
) {
if (message.author.id !== botID) {
throw "You can only edit a message that was sent by the bot.";
}
if (typeof content === "string") content = { content };
const requiredPerms: Permission[] = ["SEND_MESSAGES"];
if (content.tts) requiredPerms.push("SEND_TTS_MESSAGES");
await requireBotChannelPermissions(message.channelID, requiredPerms);
if (content.content && content.content.length > 2000) {
throw new Error(Errors.MESSAGE_MAX_LENGTH);
}
const result = await RequestManager.patch(
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
content,
);
return structures.createMessageStruct(result as MessageCreateOptions);
}
+19
View File
@@ -0,0 +1,19 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { structures } from "../../structures/mod.ts";
import { MessageCreateOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
export async function getMessage(channelID: string, id: string) {
await requireBotChannelPermissions(channelID, [
"VIEW_CHANNEL",
"READ_MESSAGE_HISTORY",
]);
const result = (await RequestManager.get(
endpoints.CHANNEL_MESSAGE(channelID, id),
)) as MessageCreateOptions;
return structures.createMessageStruct(result);
}
+37
View File
@@ -0,0 +1,37 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { structures } from "../../structures/mod.ts";
import {
GetMessages,
GetMessagesAfter,
GetMessagesAround,
GetMessagesBefore,
MessageCreateOptions,
} from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
export async function getMessages(
channelID: string,
options?:
| GetMessagesAfter
| GetMessagesBefore
| GetMessagesAround
| GetMessages,
) {
await requireBotChannelPermissions(channelID, [
"VIEW_CHANNEL",
"READ_MESSAGE_HISTORY",
]);
if (options?.limit && options.limit > 100) return;
const result = (await RequestManager.get(
endpoints.CHANNEL_MESSAGES(channelID),
options,
)) as MessageCreateOptions[];
return Promise.all(
result.map((res) => structures.createMessageStruct(res)),
);
}
+19
View File
@@ -0,0 +1,19 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { DiscordGetReactionsParams, UserPayload } from "../../types/mod.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/** Get a list of users that reacted with this emoji. */
export async function getReactions(
channelID: string,
messageID: string,
reaction: string,
options?: DiscordGetReactionsParams,
) {
const users = (await RequestManager.get(
endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction),
options,
)) as UserPayload[];
return new Collection(users.map((user) => [user.id, user]));
}
+17
View File
@@ -0,0 +1,17 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
export async function pin(channelID: string, messageID: string) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
const result = await RequestManager.put(
endpoints.CHANNEL_PIN(channelID, messageID),
);
return result;
}
// aliases
export { pin as pinMessage };
+13
View File
@@ -0,0 +1,13 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { structures } from "../../structures/mod.ts";
import { MessageCreateOptions } from "../../types/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;
return structures.createMessageStruct(data);
}
@@ -0,0 +1,14 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes all reactions for all emojis on this message. */
export async function removeAllReactions(channelID: string, messageID: string) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTIONS(channelID, messageID),
);
return result;
}
+21
View File
@@ -0,0 +1,21 @@
import { RequestManager } from "../../rest/request_manager.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. */
export async function removeReaction(
channelID: string,
messageID: string,
reaction: string,
) {
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelID, messageID, reaction),
);
return result;
}
@@ -0,0 +1,24 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeReactionEmoji(
channelID: string,
messageID: string,
reaction: string,
) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction),
);
return result;
}
@@ -0,0 +1,30 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes a reaction from the specified user on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeUserReaction(
channelID: string,
messageID: string,
reaction: string,
userID: string,
) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
const result = await RequestManager.delete(
endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelID,
messageID,
reaction,
userID,
),
);
return result;
}
+100
View File
@@ -0,0 +1,100 @@
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { structures } from "../../structures/mod.ts";
import {
ChannelTypes,
Errors,
MessageContent,
MessageCreateOptions,
Permission,
} from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
export async function sendMessage(
channelID: string,
content: string | MessageContent,
) {
if (typeof content === "string") content = { content };
const channel = await cacheHandlers.get("channels", channelID);
if (channel) {
if (
![
ChannelTypes.DM,
ChannelTypes.GUILD_NEWS,
ChannelTypes.GUILD_TEXT,
].includes(channel.type)
) {
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
}
const requiredPerms: Set<Permission> = new Set([
"SEND_MESSAGES",
"VIEW_CHANNEL",
]);
if (content.tts) requiredPerms.add("SEND_TTS_MESSAGES");
if (content.embed) requiredPerms.add("EMBED_LINKS");
if (content.replyMessageID || content.mentions?.repliedUser) {
requiredPerms.add("READ_MESSAGE_HISTORY");
}
await requireBotChannelPermissions(channelID, [...requiredPerms]);
}
// Use ... for content length due to unicode characters and js .length handling
if (content.content && [...content.content].length > 2000) {
throw new Error(Errors.MESSAGE_MAX_LENGTH);
}
if (content.mentions) {
if (content.mentions.users?.length) {
if (content.mentions.parse?.includes("users")) {
content.mentions.parse = content.mentions.parse.filter(
(p) => p !== "users",
);
}
if (content.mentions.users.length > 100) {
content.mentions.users = content.mentions.users.slice(0, 100);
}
}
if (content.mentions.roles?.length) {
if (content.mentions.parse?.includes("roles")) {
content.mentions.parse = content.mentions.parse.filter(
(p) => p !== "roles",
);
}
if (content.mentions.roles.length > 100) {
content.mentions.roles = content.mentions.roles.slice(0, 100);
}
}
}
const result = (await RequestManager.post(
endpoints.CHANNEL_MESSAGES(channelID),
{
...content,
allowed_mentions: content.mentions
? {
...content.mentions,
replied_user: content.mentions.repliedUser,
}
: undefined,
...(content.replyMessageID
? {
message_reference: {
message_id: content.replyMessageID,
fail_if_not_exists: content.failReplyIfNotExists === true,
},
}
: {}),
},
)) as MessageCreateOptions;
return structures.createMessageStruct(result);
}
+17
View File
@@ -0,0 +1,17 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Unpin a message in a channel. Requires MANAGE_MESSAGES. */
export async function unpin(channelID: string, messageID: string) {
await requireBotChannelPermissions(channelID, ["MANAGE_MESSAGES"]);
const result = await RequestManager.delete(
endpoints.CHANNEL_PIN(channelID, messageID),
);
return result;
}
// aliases
export { unpin as unpinMessage };