简体   繁体   English

添加对特定消息的反应 Discord.JS

[英]Add reaction to a specific message Discord.JS

I am trying to create a command to add a reaction to a certain message.我正在尝试创建一个命令来添加对某个消息的反应。

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

But when running the command I get this error:但是在运行命令时出现此错误:

(node:4) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body:channel_id: Value "845345565700128788 <:XPLANE11:845383490286518322>" is not snowflake. (node:4) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body:channel_id: Value "845345565700128788 <:XPLANE11:845383490286518322>" 不是雪花。

Is there any simple way of fixing this?有什么简单的方法可以解决这个问题吗?

Thanks谢谢

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(" "));
                })
            })
          }});

For anyone wondering, this is the code that worked:对于任何想知道的人,这是有效的代码:

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...');
      }
    }
  });

The problem is you're using Array#slice() incorrectly.问题是您错误地使用了Array#slice() Array#slice() returns a shallow copy of a portion of an array into a new array. Array#slice()将数组的一部分的浅拷贝返回到新数组中。 When you use messageArray.slice(1) you actually create a new array by chopping off the first element of messageArray , the command.当您使用messageArray.slice(1)时,您实际上是通过切断messageArray的第一个元素(命令)来创建一个新数组。 For messageid you're chopping off the first two elements of messageArray , leaving you the message ID and the emoji.对于messageid ,您将删除messageArray的前两个元素,留下消息 ID 和表情符号。 Check out the snippet below.看看下面的片段。 If you run it, you can see the values of each variables:如果你运行它,你可以看到每个变量的值:

 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 })

So, at this moment, channelid is an array of three elements;所以,此时, channelid是三个元素的数组; the channel ID, the message ID, and the emoji.频道 ID、消息 ID 和表情符号。 Inside the channels.fetch() method you once again create a new array by chopping off the first element and then join the rest by a single space.channels.fetch()方法中,您通过切掉第一个元素再次创建一个新数组,然后将 rest join一个空格。 So, it becomes 845345565700128788 <:XPLANE11:845383490286518322> .所以,它变成845345565700128788 <:XPLANE11:845383490286518322> Check out the snippet below:看看下面的片段:

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

If you check the value you try to fetch, it's exactly the one in your error message, Value "845345565700128788 <:XPLANE11:845383490286518322>" is not snowflake .如果您检查您尝试获取的值,它正是您的错误消息中的那个, Value "845345565700128788 <:XPLANE11:845383490286518322>" is not snowflake It's not a valid snowflake.这不是一个有效的雪花。 It's actually the message ID and the emoji in a single string.它实际上是单个字符串中的消息 ID 和表情符号。

To solve this, you could simply get the second element of the messageArray as the channelid , the third one as the messageid , etc:为了解决这个问题,您可以简单地将messageArray的第二个元素作为channelid ,将第三个元素作为messageid ,依此类推:

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

You could also use array destructuring to get the same:您还可以使用数组解构来获得相同的效果:

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

And here is the full code:这是完整的代码:

// 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...');
    }
  }
});

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

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