Finish message helpers

This commit is contained in:
TriForMine
2021-10-15 15:17:19 +02:00
parent 597f061581
commit a97b01846d
10 changed files with 58 additions and 74 deletions
+1
View File
@@ -1,3 +1,4 @@
import { cacheHandlers } from "../../cache.ts";
import type { Bot } from "../../bot.ts";
/** Delete a message with the channel id and message id only. */
+5 -5
View File
@@ -1,14 +1,14 @@
import { rest } from "../../rest/rest.ts";
import type { GetReactions } from "../../types/messages/message_get_reactions.ts";
import type { User } from "../../types/users/user.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import type { Bot } from "../../bot.ts";
/** Get a list of users that reacted with this emoji. */
export async function getReactions(channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) {
const users = await rest.runMethod<User[]>(
export async function getReactions(bot: Bot, channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) {
const users = await bot.rest.runMethod<User[]>(
bot.rest,
"get",
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
bot.constants.endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
options
);
+4 -6
View File
@@ -1,12 +1,10 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import type { Bot } from "../../bot.ts";
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
export async function pin(channelId: bigint, messageId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
export async function pin(bot: Bot, channelId: bigint, messageId: bigint) {
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
return await rest.runMethod<undefined>("put", endpoints.CHANNEL_PIN(channelId, messageId));
return await bot.rest.runMethod<undefined>(bot.rest,"put", bot.constants.endpoints.CHANNEL_PIN(channelId, messageId));
}
// aliases
+5 -6
View File
@@ -1,11 +1,10 @@
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import type { Message } from "../../types/messages/message.ts";
import { endpoints } from "../../util/constants.ts";
import {Bot} from "../../bot.ts";
import {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** Crosspost a message in a News Channel to following channels. */
export async function publishMessage(channelId: bigint, messageId: bigint) {
const data = await rest.runMethod<Message>("post", endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId));
export async function publishMessage(bot: Bot, channelId: bigint, messageId: bigint) {
const data = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Message>>(bot.rest, "post", bot.constants.endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId));
return await structures.createDiscordenoMessage(data);
return await bot.transformers.message(bot, data);
}
+4 -6
View File
@@ -1,10 +1,8 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import {Bot} from "../../bot.ts";
/** Removes all reactions for all emojis on this message. */
export async function removeAllReactions(channelId: bigint, messageId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) {
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_MESSAGE_REACTIONS(channelId, messageId));
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE_REACTIONS(channelId, messageId));
}
+7 -7
View File
@@ -1,16 +1,15 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import {Bot} from "../../bot.ts";
/** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeReaction(
bot: Bot,
channelId: bigint,
messageId: bigint,
reaction: string,
options?: { userId?: bigint }
) {
if (options?.userId) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
}
if (reaction.startsWith("<:")) {
@@ -19,10 +18,11 @@ export async function removeReaction(
reaction = reaction.substring(3, reaction.length - 1);
}
return await rest.runMethod<undefined>(
return await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
options?.userId
? endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId)
: endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)
? bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId)
: bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)
);
}
@@ -1,10 +1,9 @@
import { rest } from "../../rest/rest.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: bigint, messageId: bigint, reaction: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
import {Bot} from "../../bot.ts";
export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) {
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
@@ -12,5 +11,5 @@ export async function removeReactionEmoji(channelId: bigint, messageId: bigint,
reaction = reaction.substring(3, reaction.length - 1);
}
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction));
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction));
}
+12 -15
View File
@@ -1,19 +1,15 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import type { CreateMessage } from "../../types/messages/create_message.ts";
import type { Message } from "../../types/messages/message.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakelize, validateComponents } from "../../util/utils.ts";
import { validateLength } from "../../util/validate_length.ts";
import type { Bot } from "../../bot.ts";
import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
export async function sendMessage(channelId: bigint, content: string | CreateMessage) {
export async function sendMessage(bot: Bot, channelId: bigint, content: string | CreateMessage) {
if (typeof content === "string") content = { content };
const channel = await cacheHandlers.get("channels", channelId);
@@ -43,16 +39,16 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes
requiredPerms.add("READ_MESSAGE_HISTORY");
}
await requireBotChannelPermissions(channelId, [...requiredPerms]);
await bot.utils.requireBotChannelPermissions(bot, channelId, [...requiredPerms]);
}
// Use ... for content length due to unicode characters and js .length handling
if (content.content && !validateLength(content.content, { max: 2000 })) {
throw new Error(Errors.MESSAGE_MAX_LENGTH);
if (content.content && !bot.utils.validateLength(content.content, { max: 2000 })) {
throw new Error(bot.constants.Errors.MESSAGE_MAX_LENGTH);
}
if (content.components?.length) {
validateComponents(content.components);
bot.utils.validateComponents(bot, content.components);
}
if (content.allowedMentions) {
@@ -77,10 +73,11 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes
}
}
const result = await rest.runMethod<Message>(
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Message>>(
bot.rest,
"post",
endpoints.CHANNEL_MESSAGES(channelId),
snakelize({
bot.constants.endpoints.CHANNEL_MESSAGES(channelId),
bot.utils.snakelize({
...content,
...(content.messageReference?.messageId
? {
@@ -93,5 +90,5 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes
})
);
return structures.createDiscordenoMessage(result);
return bot.transformers.message(bot, result);
}
+9 -15
View File
@@ -1,26 +1,20 @@
import { botId } from "../../bot.ts";
import type { Bot } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { EditMessage } from "../../types/messages/edit_message.ts";
import type { Message } from "../../types/messages/message.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakelize, validateComponents } from "../../util/utils.ts";
import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
/** Suppress all the embeds in this message */
export async function suppressEmbeds(channelId: bigint, messageId: bigint) {
export async function suppressEmbeds(bot: Bot, channelId: bigint, messageId: bigint) {
const message = await cacheHandlers.get("messages", messageId);
await requireBotChannelPermissions(channelId, message ? ["MANAGE_MESSAGES"] : ["SEND_MESSAGES"]);
await bot.utils.requireBotChannelPermissions(bot, channelId, message ? ["MANAGE_MESSAGES"] : ["SEND_MESSAGES"]);
const result = await rest.runMethod<Message>(
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Message>>(
bot.rest,
"patch",
endpoints.CHANNEL_MESSAGE(channelId, messageId),
snakelize({ flags: 4 })
bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId),
bot.utils.snakelize({ flags: 4 })
);
return await structures.createDiscordenoMessage(result);
return await bot.transformers.message(bot, result);
}
+5 -7
View File
@@ -1,12 +1,10 @@
import { rest } from "../../rest/rest.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: bigint, messageId: bigint): Promise<undefined> {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
import {Bot} from "../../bot.ts";
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_PIN(channelId, messageId));
export async function unpin(bot: Bot, channelId: bigint, messageId: bigint): Promise<undefined> {
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.CHANNEL_PIN(channelId, messageId));
}
// aliases