简体   繁体   English

如何删除 DiscordJS (v12) 中的现有嵌入

[英]How to delete an existing embed in DiscordJS (v12)

I'm working on a small private Discord bot (mainly for learning)我正在开发一个小型私人 Discord 机器人(主要用于学习)
I've been working on a feature where the bot sends a message, the user sends a response and the bot will delete the initial embed and follow up with another embed.我一直在研究一项功能,机器人发送消息,用户发送响应,机器人将删除初始嵌入并跟进另一个嵌入。
(creating a chain of embedded messages to collect user input and build a final result from it) (创建一系列嵌入式消息以收集用户输入并从中构建最终结果)

Thus far this is what my code consists of到目前为止,这是我的代码组成

const wait = 30000;
    let count;

    const embed = new Discord.MessageEmbed()
        .setColor('#9EFF9A')
        .setTitle('Question?')
        .setDescription('');

    message.channel.send(embed);
    message.channel.awaitMessages(m => m.author.id == message.author.id,
        { max: 1, time: `${wait}` }).then(collected => {
        message.delete(embed);
        count = collected.first().content;
            console.log(count);
    }).catch(() => {
        message.delete(embed);
        return message.reply('No reply after ' + `${wait / 1000}` + ' seconds, operation canceled.').then(m => {
            m.delete({ timeout: 15000 });
        });
    });

I have tried various iterations of message.delete();我已经尝试了message.delete();各种迭代message.delete(); with no useful results, the bot usually ends up deleting the commanding message sent by the user to start the embed chain.在没有任何有用的结果的情况下,机器人通常最终会删除用户发送的启动嵌入链的命令消息。

I got a suggestion from a friend that I also ended up seeing online a few times which was the following:我从一个朋友那里得到了一个建议,我也最终在网上看到了几次,内容如下:

.then(() => {
message.delete()
})

I can't come up with any way to implement this into my current code.我无法想出任何方法将其实现到我当前的代码中。 Is there something I am misunderstanding?我有什么误解吗? I am very new to DiscordJS and Javascript and my friend did mention that .then() statements can get pretty tricky我对 DiscordJS 和 Javascript 很陌生,我的朋友确实提到 .then() 语句可能会变得非常棘手

I appreciate any help I can get!我很感激我能得到的任何帮助!

Your code is very close to achieving what you want, except you are attempting to delete the Embed object that you created instead of the Message object of the embed.您的代码非常接近于实现您想要的,除了您试图删除您创建的Embed对象而不是EmbedMessage对象。 Here's a slight tweak that will achieve what you need:这是一个可以实现您所需要的轻微调整:

const wait = 30000;
let count;

const embed = new Discord.MessageEmbed()
    .setColor('#9EFF9A')
    .setTitle('Question?')
    .setDescription('');

message.channel.send(embed).then(embedMessage => {
    embedMessage.channel.awaitMessages(m => m.author.id == message.author.id,
    { max: 1, time: wait }).then(collected => {
        embedMessage.delete();
        count = collected.first().content;
        console.log(count);
    }).catch(() => {
        embedMessage.delete();
        return message.reply('No reply after ' + (wait / 1000) + ' seconds, operation canceled.').then(m => {
            m.delete({ timeout: 15000 });
        });
    });
})

The secret here is using .then() on the method that sends the embed.这里的秘密是在发送嵌入的方法上使用.then() This allows you to obtain the actual Message object of the embed that was sent, which you can then interact with.这允许您获取发送的嵌入的实际Message对象,然后您可以与之交互。 Now that you have the Message object for your embed, you can directly interact with the message using its methods, such as delete() and edit() .现在您拥有用于嵌入的Message对象,您可以使用其方法直接与消息交互,例如delete()edit()

.then(() => {
   message.delete()
})

Is not working because you never passed in the message as a parameter, therefor your embed does not exist in the context of .then()不起作用,因为您从未将消息作为参数传递,因此您的嵌入在.then()的上下文中不存在

You can try using .then() or await to delete a send message.您可以尝试使用.then()await删除发送消息。

then Method然后方法

// const embed = ...
message.channel.send(embed).then(msg => {
   msg.delete();
});

Await Method等待方法

// Make sure you're in an async function
//const embed = ...
const msg = await message.channel.send(msg);
msg.delete();

I am not to familiar with discordjs but from what I understand you create a message with the bot under the variable "message" which has the properties seen here: https://discord.js.org/#/docs/main/master/class/Message我不熟悉discordjs,但据我所知,您在变量“message”下与bot创建一条消息,该变量具有此处可见的属性: https : //discord.js.org/#/docs/main/master/类/消息

Then you use that message to send an embed to the message's channel.然后您使用该消息将嵌入发送到该消息的频道。 The embed ask's a question and you then await for ensuing messages.嵌入提出一个问题,然后您等待随后的消息。 You then want to take the first response and put it into the count variable.然后,您想要获取第一个响应并将其放入 count 变量中。 Then you want to delete the original embed.然后你想删除原来的嵌入。 If this is all true I would suggest deleting the original message that houses the embed itself like so:如果这一切都是真的,我建议删除包含嵌入本身的原始消息,如下所示:

message.channel.awaitMessages(m => m.author.id == message.author.id,
    { max: 1, time: `${wait}` }).then(collected => {
    message.delete();
    count = collected.first().content;
        console.log(count);
})

Or try this but I don't think this method will work:或者试试这个,但我认为这种方法行不通:

message.channel.awaitMessages(m => m.author.id == message.author.id,
    { max: 1, time: `${wait}` }).then(collected => {
    embed.delete();
    count = collected.first().content;
        console.log(count);
})

I would check out these two pages of documentation: https://discord.js.org/#/docs/main/master/class/Message?scrollTo=delete https://discord.js.org/#/docs/main/master/class/MessageEmbed我会查看这两页文档: https : //discord.js.org/#/docs/main/master/class/Message ? scrollTo = delete https://discord.js.org/#/docs/main /master/class/MessageEmbed

Welcome to Stack Overflow tell me if one of these worked.欢迎使用 Stack Overflow,告诉我其中一个是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM