首页 > 解决方案 > 如果第一种失败,则选择第二种响应方式

问题描述

基本上,我使用一个以“新方式”响应的文件,更多称为内联回复,只有一个问题,如果在机器人发送消息之前删除了usuary的消息,它会message_reference报错,我创建的表单它使用 message.channel.send 是这样的:

const msg = await message.inlineReply(embed).catch(async() => { message.channel.send(embed) })

但是如果你使用带有反应的异步是错误的,并且如果代码中有错误,则捕获受限于 message.channel.send 的捕获。有没有更有效的方法?也许改变 inlinereply.js 中的某些内容?

内联回复.js:

const { APIMessage, Structures } = require("discord.js");

class Message extends Structures.get("Message") {
    async inlineReply(content, options) {
        const mentionRepliedUser = typeof ((options || content || {}).allowedMentions || {}).repliedUser === "undefined" ? true : ((options || content).allowedMentions).repliedUser;
        delete ((options || content || {}).allowedMentions || {}).repliedUser;

        const apiMessage = content instanceof APIMessage ? content.resolveData() : APIMessage.create(this.channel, content, options).resolveData();
        Object.assign(apiMessage.data, { message_reference: { message_id: this.id } });
    
        if (!apiMessage.data.allowed_mentions || Object.keys(apiMessage.data.allowed_mentions).length === 0)
            apiMessage.data.allowed_mentions = { parse: ["users", "roles", "everyone"] };
        if (typeof apiMessage.data.allowed_mentions.replied_user === "undefined")
            Object.assign(apiMessage.data.allowed_mentions, { replied_user: mentionRepliedUser });

        if (Array.isArray(apiMessage.data.content)) {
            return Promise.all(apiMessage.split().map(x => {
                x.data.allowed_mentions = apiMessage.data.allowed_mentions;
                return x;
            }).map(this.inlineReply.bind(this)));
        }

        const { data, files } = await apiMessage.resolveFiles();
        return this.client.api.channels[this.channel.id].messages
            .post({ data, files })
            .then(d => this.client.actions.MessageCreate.handle(d).message);
    }
}

Structures.extend("Message", () => Message);

标签: node.jsdiscorddiscord.js

解决方案


推荐阅读