【问题标题】:Add reaction to a specific message Discord.JS添加对特定消息的反应 Discord.JS
【发布时间】:2021-08-11 02:23:49
【问题描述】:

我正在尝试创建一个命令来添加对特定消息的反应。

命令是:/react "Channel-ID" "Message-ID" "Emoji"

但是在运行命令时出现此错误:

(node:4) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid 表单正文:channel_id:值“845345565700128788 <:xplane11:845383490286518322>" 不是雪花。

有什么简单的方法可以解决这个问题吗?

谢谢

client.on("message", message => {
        if (message.author.bot) return;
    
        let messageArray = message.content.split(" ");
        let command = messageArray[0];
        let channelid = messageArray.slice(1);
        let messageid = messageArray.slice(2);
        let emoji = messageArray.slice(3);
    
        if (message.channel.type === "dm") return;
    
        if (!message.content.startsWith('/')) return;
    
           if (command === '/react') {
    
            let memberrole = message.member.roles.cache.find(role => role.name === "CEO");
            if (!memberrole) return message.channel.send('Insufficiant Perms');
            
            message.client.channels.fetch(channelid.slice(1).join(" ")).then(channel => {
                channel.messages.fetch(messageid.slice(2).join(" ")).then(message => {
                    message.react(emoji.slice(3).join(" "));
                })
            })
          }});

对于任何想知道的人,这是有效的代码:

client.on('message', async (message) => {
    if (
      message.author.bot ||
      message.channel.type === 'dm' ||
      !message.content.startsWith(prefix)
    )
      return;
    
    const args = message.content.slice(prefix.length).split(/ +/);

    const command = args.shift().toLowerCase();
  
    if (command === 'react') {

      const [channelId, messageId, emoji] = args;
  

      if (!channelId)
        return message.channel.send(`You need to provide a channel ID`);

      const memberrole = message.member.roles.cache.find((role) => role.name === 'CEO');
      if (!memberrole) return message.channel.send('Insufficiant perms');
  
      try {
        const channel = await message.client.channels.fetch(channelId);
        if (!channel)
          return message.channel.send(`No channel found with the ID ${channelId}`);
  
        const msg = await channel.messages.fetch(messageId);
        if (!msg)
          return message.channel.send(`No message found with the ID ${messageId}`);
  
        msg.react(emoji);
      } catch (error) {
        console.log(error);
        message.channel.send('Oh-oh, there was an error...');
      }
    }
  });

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    问题是您错误地使用了Array#slice()Array#slice() 将数组的一部分的浅拷贝返回到新数组中。当您使用messageArray.slice(1) 时,实际上是通过删除命令messageArray 的第一个元素来创建一个新数组。对于messageid,您将删除messageArray 的前两个元素,留下消息ID 和表情符号。看看下面的sn-p。如果你运行它,你可以看到每个变量的值:

    const message = { content: '/react 845345565700128700 845345565700128788 <:XPLANE11:845383490286518322>' }
    let messageArray = message.content.split(' ');
    let command = messageArray[0];
    let channelid = messageArray.slice(1);
    let messageid = messageArray.slice(2);
    let emoji = messageArray.slice(3);
    
    console.log({ command, channelid, messageid, emoji })

    所以,此时,channelid 是一个由三个元素组成的数组;频道 ID、消息 ID 和表情符号。在 channels.fetch() 方法中,您再次创建一个新数组,方法是将第一个元素切掉,然后将 join 剩下的元素切掉一个空格。所以,它变成了845345565700128788 &lt;:XPLANE11:845383490286518322&gt;。查看下面的 sn-p:

    // messageArray.slice(1);
    const channelid = [
      '845345565700128700',
      '845345565700128788',
      '<:XPLANE11:845383490286518322>',
    ]
    
    console.log(channelid.slice(1).join(' '))

    如果您检查您尝试获取的值,它正是您的错误消息中的值,Value "845345565700128788 &lt;:XPLANE11:845383490286518322&gt;" is not snowflake。这不是一个有效的雪花。它实际上是单个字符串中的消息 ID 和表情符号。

    要解决这个问题,您可以简单地将messageArray 的第二个元素作为channelid,将第三个元素作为messageid,等等:

    let messageArray = message.content.split(' ');
    let command = messageArray[0];
    let channelid = messageArray[1];
    let messageid = messageArray[2];
    let emoji = messageArray[3];
    

    您也可以使用数组解构来获得相同的效果:

    let messageArray = message.content.split(' ');
    let [command, channelid, messageid, emoji] = messageArray;
    

    这是完整的代码:

    // use a prefix variable
    const prefix = '/';
    
    client.on('message', async (message) => {
      if (
        message.author.bot ||
        message.channel.type === 'dm' ||
        !message.content.startsWith(prefix)
      )
        return;
    
      // create an args variable that slices off the prefix and splits it into an array
      const args = message.content.slice(prefix.length).split(/ +/);
      // create a command variable by taking the first element in the array
      // and removing it from args
      const command = args.shift().toLowerCase();
    
      if (command === 'react') {
        // destructure args
        const [channelId, messageId, emoji] = args;
    
        // check if there is channelId, messageId, and emoji provided
        // if not, send an error message
        if (!channelId)
          return message.channel.send(`You need to provide a channel ID`);
    
        // same with messageId and emoji
        // ...
    
        const memberrole = message.member.roles.cache.find((role) => role.name === 'CEO');
        if (!memberrole) return message.channel.send('Insufficiant perms');
    
        try {
          const channel = await message.client.channels.fetch(channelId);
          if (!channel)
            return message.channel.send(`No channel found with the ID ${channelId}`);
    
          const fetchedMessage = await channel.messages.fetch(messageId);
          if (!fetchedMessage)
            return message.channel.send(`No message found with the ID ${messageId}`);
    
          fetchedMessage.react(emoji);
        } catch (error) {
          console.log(error);
          message.channel.send('Oh-oh, there was an error...');
        }
      }
    });
    

    【讨论】:

    • 嘿,非常感谢您的回复,非常感谢。当我运行命令时,我现在收到错误 ReferenceError: Cannot access 'message' before initialization.
    • 不客气。不过,这很奇怪,它应该可以工作。你能把你的代码复制粘贴到Pastebin这样的网站上,然后把链接发给我,让我看看吗?
    • srcb.in/FSrig6mSnn 在这里,我在启动机器人时也收到此消息(node:4) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added to [Client]. Use emitter.setMaxListeners() to increase limit 但这不会导致机器人崩溃。
    • 看起来client.on('message', async (message) 在您的client.on('ready', async () =&gt; { 侦听器中。
    • 谢谢,您能否更新您的代码以将const message = await channel.messages.fetch(messageId); if (!message) return message.channel.send('No message found with the ID ${messageId}'); message.react(emoji); 更改为const msg = await channel.messages.fetch(messageId); if (!msg) return message.channel.send('No message found with the ID ${messageId}'); msg.react(emoji); 问题所在:) (将字符串变量message 更改为msg
    猜你喜欢
    • 2021-01-03
    • 2021-04-15
    • 2020-09-07
    • 2020-02-21
    • 2021-05-18
    • 2021-07-27
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多