【问题标题】:Hi, how can I fix this?嗨,我该如何解决这个问题?
【发布时间】:2021-07-31 05:39:45
【问题描述】:

我为我的 discord 机器人创建了一个命令,允许对消息做出反应,但我不知道如何让它对特定消息做出反应。我的尝试没有成功。我做错了什么?

这是我的代码:

const [name, messageID] = args.join(' ').split(' ')
if (!name) return message.channel.send("Veuillez indiquer le nom de l'emoji.")
if (!messageID) return message.channel.send("Veuillez indiquer l\'ID du message.")
const reactionEmoji = message.guild.emojis.cache.find(emoji => emoji.name === name);
(messageID).react(reactionEmoji)

【问题讨论】:

  • 为什么你把.join(' ')args数组转换成一个字符串然后.split(' ')又回到一个数组呢?这真的没有意义。你想如何react() 到一个字符串?您应该首先收到带有 messageID 的消息。
  • 请不要编辑-删除您不再认为值得的问题。它使搜索该站点变得更加困难。要么直接删除您的问题,要么 - 如果您缺乏权限 - 向其中一个模块发送消息以关闭它(然后删除)。

标签: javascript node.js discord.js command bots


【解决方案1】:

您无法对用户提供的雪花做出反应,您需要先通过此ID.fetch()消息。

// you don't need to join and split the args
const [name, messageID] = args;

if (!name)
  return message.channel.send("Veuillez indiquer le nom de l'emoji.");

if (!messageID)
  return message.channel.send("Veuillez indiquer l'ID du message.");

const reactionEmoji = message.guild.emojis.cache.find(
  (emoji) => emoji.name === name,
);

if (!reactionEmoji)
  return message.channel.send(
    "Something in French that says it can't find l'emoji par son nom :)",
  );

// you need to fetch the message by the id provided
message.channel.messages
  .fetch(messageID)
  // then react on the returned message
  .then((msg) => msg.react(reactionEmoji))
  .catch(console.error);

【讨论】:

    【解决方案2】:

    您没有明确argsArray 还是String
    .join(' ').split(' ') 也没有用,因为它会返回任何内容(无变化)
    而且您不仅可以从MessageID 获取消息,还需要ChannelID
    法语:Et vous ne pouvez pas seulement récupérer le message du MessageID Vous aurez également besoin du ChannelID

    试试这样的:
    法语:Essayez quelque 选择了 comme ça

    let name, messageID, channelID;
    if(typeof(args) != string){
        //args is an array (Might be number but let's forget that part)
        //args est une array (Peut-être un numéro mais oublions cette partie)
        [name, messageID, channelID] = args;
    }else{
        //args is an String
        //French: args est une string
        [name, messageID, channelID] = args.split(' '); //No need to join()
    }
    if(!name){ return message.channel.send("Enter name") }
    if(!messageID){ return message.channel.send("Enter message ID") }
    if(!channelID){ return message.channel.send("Enter Channel ID") }
    //This line is from your code
    //French: Cette ligne provient de votre code
    const reactionEmoji = message.guild.emojis.cache.find(emoji => emoji.name === name);
    //You have to fetch the message before using it
    const reactChannel = await client.channels.cache.get(channelID);
    if(!reactChannel){ return message.channel.send("Channel not accessable") }
    let reactMessage = await reactChannel.messages.fetch("701574160211771462");
    //Then finally
    reactMessage.react(reactionEmoji);
    

    Si vous ne parvenez pas à comprendre l'anglais, essayez d'utiliser Google Translate

    【讨论】:

    • 真的谢谢,我这样做了。
    猜你喜欢
    • 2020-03-25
    • 2020-02-14
    • 2021-10-23
    • 1970-01-01
    • 2020-12-28
    • 2019-12-03
    相关资源
    最近更新 更多