This commit is contained in:
ITOH
2021-05-28 17:48:20 +02:00
parent e8081265ce
commit 1e4e5e0204

View File

@@ -1,6 +1,6 @@
import { botId } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordenoMessage } from "../../structures/message.ts";
import { structures } from "../../structures/mod.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { EditMessage } from "../../types/messages/edit_message.ts";
@@ -11,9 +11,16 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { validateComponents } from "../../util/utils.ts";
/** Edit the message. */
export async function editMessage(message: DiscordenoMessage, content: string | EditMessage) {
if (message.authorId !== botId) {
throw "You can only edit a message that was sent by the bot.";
export async function editMessage(channelId: bigint, messageId: bigint, content: string | EditMessage) {
const message = await cacheHandlers.get("messages", messageId);
if (message) {
if (message.authorId !== botId) {
throw new Error("You can only edit a message that was sent by the bot.");
}
const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"];
await requireBotChannelPermissions(message.channelId, requiredPerms);
}
if (typeof content === "string") content = { content };
@@ -22,19 +29,11 @@ export async function editMessage(message: DiscordenoMessage, content: string |
validateComponents(content.components);
}
const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"];
await requireBotChannelPermissions(message.channelId, requiredPerms);
if (content.content && content.content.length > 2000) {
throw new Error(Errors.MESSAGE_MAX_LENGTH);
}
const result = await rest.runMethod<Message>(
"patch",
endpoints.CHANNEL_MESSAGE(message.channelId, message.id),
content
);
const result = await rest.runMethod<Message>("patch", endpoints.CHANNEL_MESSAGE(channelId, messageId), content);
return await structures.createDiscordenoMessage(result);
}