简体   繁体   English

Discord.js - 从交互中获取消息

[英]Discord.js - Get Message from Interaction

client.ws.on('INTERACTION_CREATE', async (interaction) => {}

The interaction class has lots of properties like channel , member and id but it doesn't have a message property.交互类有很多属性,如channelmemberid但它没有message属性。

Is there a way to get the message from an interaction or will I have to use a event listener on message?有没有办法从交互中获取消息,还是必须对消息使用事件侦听器? And if so, how would I use this with slash commands?如果是这样,我将如何将它与斜杠命令一起使用?

Slash commands have their own type of message.斜线命令有自己的消息类型。 I don't believe they have an id, delete button, edit button or lots of things most messages do.我不相信他们有 id、删除按钮、编辑按钮或大多数消息所做的很多事情。 This means you will not be able to get the "message" from a slash command.这意味着您将无法从斜杠命令获取“消息”。 From buttons however, they do emit INTERACTION_CREATE but has a little more info.然而,从按钮中,它们确实发出INTERACTION_CREATE但有更多信息。 I don't remember for sure, but I think you can use something like interaction.components .我不太记得了,但我认为您可以使用诸如interaction.components东西。 I am not completely sure, but if you want, click a button and log the interaction into your console to see the unique button info like this我不完全确定,但如果您愿意,请单击一个按钮并将交互记录到您的控制台中,以查看像这样的唯一按钮信息

client.ws.on('INTERACTION_CREATE', async (interaction) => {
{
console.log(interaction) //will be long!
//…
})

You can get the user input by just using the base class interaction .您可以仅使用基类interaction来获取用户输入。 However the content is not visible.但是内容不可见。 You can access it by passing it through an api endpoint or something similar.您可以通过 api 端点或类似的东西传递它来访问它。

For example例如

client.on('interactionCreate', async interaction => {
  if (interaction.commandName == 'greet') {
      fetch(base_url + `api/greet?msg=${interaction}`) // the server can read the user input here
          .then(res => res.json())
          .then(async data => {await interaction.reply(data.content)}) // send back the data
  }
})

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

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