Search code examples
javascriptnode.jsdiscorddiscord.js

I'm getting an error in making avatar bot in discord.js


I wanted to make a command for my bot that returns the user avatar, but I am getting an error:

DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async TextChannel.send (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:15) {
  method: 'post',
  path: '/channels/885608990418010202/messages',
  code: 50006,
  httpStatus: 400,
  requestData: {
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    },
    files: []
  }
}

My code:

module.exports = {
    name: 'avatar',
    aliases: ['icon', 'pfp', 'profilepic'],
    permissions: [],
    cooldown: 5,
    description: '!',

    execute(message, args, client, Discord) {

        if (!message.mentions.users.size) {
            return message.channel.send(`**آواتار شما:** ${message.author.displayAvatarURL({ dynamic: true })}`);
        }

        const avatar_list = message.mentions.users.map(user => {
            return ` آواتار**<@${user.id}>** ${user.displayAvatarURL({ dynamic: true })}`;
        });

        message.channel.send(avatar_list);
    }
}

I am using Discord.js v13, node 16


Solution

  • Collection#map returns an array and you try to send that. As you can only send a string, you can join the returned array using the Array#join method:

    const avatar_list = message.mentions.users.map((user) => {
      return ` آواتار**<@${user.id}>** ${user.displayAvatarURL({ dynamic: true })}`;
    });
    
    message.channel.send(avatar_list.join('\n'));