简体   繁体   English

如何.deferReply 到指定频道?

[英]How to .deferReply to specified channel?

How can I have it so that the bot sends a message to a specific channel, while using .deferReply and .editReply ?我怎样才能让机器人在使用.deferReply.editReply时向特定频道发送消息? Currently, I'm getting an error saying that suggestionChannel.deferReply is not a function .目前,我收到一条错误消息, suggestionChannel.deferReply is not a function Here's my code:这是我的代码:

const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("suggest")
    .setDescription("Send your suggestion to the specified channel")
    .addStringOption((option) =>
      option
        .setName("suggestion")
        .setDescription("Your suggestion")
        .setRequired(true)
    ),
  async execute(interaction, client) {
    var suggestionChannelID = "900982140504793109";
    var suggestionChannel = client.channels.cache.get(suggestionChannelID);
    const embed = new MessageEmbed()
      .setColor("#0099ff")
      .setTitle(`New suggestion by ${interaction.member.displayName}`)
      .setDescription(`${interaction.options.getString("suggestion")}`);
    await suggestionChannel.deferReply();
    await suggestionChannel
      .editReply({
        embeds: [embed],
      })
      .then(function (interaction) {
        interaction.react(`👍`).then(interaction.react(`👎`));
      });
  },
};

How can I have it so that the bot sends a message to a specific channel, while using .deferReply and .editReply?我怎样才能让机器人在使用 .deferReply 和 .editReply 时向特定频道发送消息?

Well, the short answer is;嗯,简短的回答是; you can't.你不能。 But you can do this:但是你可以这样做:

As the error already says, deferReply() is not a method of the TextBasedChannels class, defined by your suggestionChannel .正如错误已经说的那样, deferReply()不是TextBasedChannels类的方法,由您的suggestionChannel TextBasedChannels定义。

Instead, try sending a message to the channel instead of replying.相反,尝试频道发送消息而不是回复。 Replies can only be executed in the interaction's channel:回复只能在交互的频道中执行:

var suggestionChannelID = "900982140504793109";
var suggestionChannel = client.channels.cache.get(suggestionChannelID);
const embed = new MessageEmbed()
    .setColor("#0099ff")
    .setTitle(`New suggestion by ${interaction.member.displayName}`)      
    .setDescription(`${interaction.options.getString("suggestion")}`);

// use this instead
await suggestionChannel.send({
    embeds: [embed],
});

PS side note, deferReply() starts a 15-minute timer before the interaction expires and triggers that 'x is thinking...' text to appear when the client is calculating stuff, so try to call it as soon as possible. PS 旁注, deferReply()在交互到期之前启动一个 15 分钟的计时器,并触发“x 正在思考...”文本以在客户端计算内容时出现,因此请尝试尽快调用它。 By default, interactions expire after 3 seconds, so if your bot fails to finish whatever it needs to accomplish within that timeframe, that interaction will fail.默认情况下,交互会在 3 秒后过期,因此如果您的机器人未能在该时间范围内完成它需要完成的任何事情,则该交互将失败。

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

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