mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Finish message helpers
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { cacheHandlers } from "../../cache.ts";
|
||||||
import type { Bot } from "../../bot.ts";
|
import type { Bot } from "../../bot.ts";
|
||||||
|
|
||||||
/** Delete a message with the channel id and message id only. */
|
/** Delete a message with the channel id and message id only. */
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
|
||||||
import type { GetReactions } from "../../types/messages/message_get_reactions.ts";
|
import type { GetReactions } from "../../types/messages/message_get_reactions.ts";
|
||||||
import type { User } from "../../types/users/user.ts";
|
import type { User } from "../../types/users/user.ts";
|
||||||
import { Collection } from "../../util/collection.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. */
|
/** Get a list of users that reacted with this emoji. */
|
||||||
export async function getReactions(channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) {
|
export async function getReactions(bot: Bot, channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) {
|
||||||
const users = await rest.runMethod<User[]>(
|
const users = await bot.rest.runMethod<User[]>(
|
||||||
|
bot.rest,
|
||||||
"get",
|
"get",
|
||||||
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
|
bot.constants.endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import type { Bot } from "../../bot.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. */
|
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
|
||||||
export async function pin(channelId: bigint, messageId: bigint) {
|
export async function pin(bot: Bot, channelId: bigint, messageId: bigint) {
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
|
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
|
// aliases
|
||||||
|
|||||||
@@ -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 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. */
|
/** Crosspost a message in a News Channel to following channels. */
|
||||||
export async function publishMessage(channelId: bigint, messageId: bigint) {
|
export async function publishMessage(bot: Bot, channelId: bigint, messageId: bigint) {
|
||||||
const data = await rest.runMethod<Message>("post", endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId));
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import {Bot} from "../../bot.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
|
||||||
|
|
||||||
/** Removes all reactions for all emojis on this message. */
|
/** Removes all reactions for all emojis on this message. */
|
||||||
export async function removeAllReactions(channelId: bigint, messageId: bigint) {
|
export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) {
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
|
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));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import {Bot} from "../../bot.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.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. */
|
/** 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(
|
export async function removeReaction(
|
||||||
|
bot: Bot,
|
||||||
channelId: bigint,
|
channelId: bigint,
|
||||||
messageId: bigint,
|
messageId: bigint,
|
||||||
reaction: string,
|
reaction: string,
|
||||||
options?: { userId?: bigint }
|
options?: { userId?: bigint }
|
||||||
) {
|
) {
|
||||||
if (options?.userId) {
|
if (options?.userId) {
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
|
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reaction.startsWith("<:")) {
|
if (reaction.startsWith("<:")) {
|
||||||
@@ -19,10 +18,11 @@ export async function removeReaction(
|
|||||||
reaction = reaction.substring(3, reaction.length - 1);
|
reaction = reaction.substring(3, reaction.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await rest.runMethod<undefined>(
|
return await bot.rest.runMethod<undefined>(
|
||||||
|
bot.rest,
|
||||||
"delete",
|
"delete",
|
||||||
options?.userId
|
options?.userId
|
||||||
? endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId)
|
? bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId)
|
||||||
: endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)
|
: 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. */
|
/** 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) {
|
import {Bot} from "../../bot.ts";
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
|
|
||||||
|
|
||||||
|
export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) {
|
||||||
|
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
|
||||||
|
|
||||||
if (reaction.startsWith("<:")) {
|
if (reaction.startsWith("<:")) {
|
||||||
reaction = reaction.substring(2, reaction.length - 1);
|
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);
|
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));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
import { cacheHandlers } from "../../cache.ts";
|
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 { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
|
||||||
import { Errors } from "../../types/discordeno/errors.ts";
|
import { Errors } from "../../types/discordeno/errors.ts";
|
||||||
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
||||||
import type { CreateMessage } from "../../types/messages/create_message.ts";
|
import type { CreateMessage } from "../../types/messages/create_message.ts";
|
||||||
import type { Message } from "../../types/messages/message.ts";
|
import type { Message } from "../../types/messages/message.ts";
|
||||||
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
|
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import type { Bot } from "../../bot.ts";
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
|
||||||
import { snakelize, validateComponents } from "../../util/utils.ts";
|
|
||||||
import { validateLength } from "../../util/validate_length.ts";
|
|
||||||
|
|
||||||
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
/** 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 };
|
if (typeof content === "string") content = { content };
|
||||||
|
|
||||||
const channel = await cacheHandlers.get("channels", channelId);
|
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");
|
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
|
// Use ... for content length due to unicode characters and js .length handling
|
||||||
if (content.content && !validateLength(content.content, { max: 2000 })) {
|
if (content.content && !bot.utils.validateLength(content.content, { max: 2000 })) {
|
||||||
throw new Error(Errors.MESSAGE_MAX_LENGTH);
|
throw new Error(bot.constants.Errors.MESSAGE_MAX_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content.components?.length) {
|
if (content.components?.length) {
|
||||||
validateComponents(content.components);
|
bot.utils.validateComponents(bot, content.components);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content.allowedMentions) {
|
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",
|
"post",
|
||||||
endpoints.CHANNEL_MESSAGES(channelId),
|
bot.constants.endpoints.CHANNEL_MESSAGES(channelId),
|
||||||
snakelize({
|
bot.utils.snakelize({
|
||||||
...content,
|
...content,
|
||||||
...(content.messageReference?.messageId
|
...(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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,20 @@
|
|||||||
import { botId } from "../../bot.ts";
|
import type { Bot } from "../../bot.ts";
|
||||||
import { cacheHandlers } from "../../cache.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 { Message } from "../../types/messages/message.ts";
|
||||||
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
|
import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
|
||||||
import { snakelize, validateComponents } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Suppress all the embeds in this message */
|
/** 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);
|
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",
|
"patch",
|
||||||
endpoints.CHANNEL_MESSAGE(channelId, messageId),
|
bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId),
|
||||||
snakelize({ flags: 4 })
|
bot.utils.snakelize({ flags: 4 })
|
||||||
);
|
);
|
||||||
|
|
||||||
return await structures.createDiscordenoMessage(result);
|
return await bot.transformers.message(bot, result);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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. */
|
/** Unpin a message in a channel. Requires MANAGE_MESSAGES. */
|
||||||
export async function unpin(channelId: bigint, messageId: bigint): Promise<undefined> {
|
import {Bot} from "../../bot.ts";
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
|
|
||||||
|
|
||||||
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
|
// aliases
|
||||||
|
|||||||
Reference in New Issue
Block a user