【问题标题】:discord.js Cant use command with user IDdiscord.js 无法使用带有用户 ID 的命令
【发布时间】:2021-07-08 17:23:12
【问题描述】:

我正在发出一个命令,通过向他们发送一个 dm 来对提到的用户进行 rickroll,当我尝试通过提及使用他们的 ID 的人来使用它时,它不起作用并发送我添加的错误消息:The mentioned user is not in the server

const { DiscordAPIError } = require('discord.js');
const Discord = require('discord.js');
const BaseCommand = require('../../utils/structures/BaseCommand');

module.exports = class RickrollCommand extends BaseCommand {
  constructor() {
    super('rickroll', 'fun', []);
  }

  async run(client, message, args) {
    let mentionedMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    if (!args[0]) return message.channel.send('You need to mention a member to rickroll.');
    if (!mentionedMember) return message.channel.send('The user mentioned is not in the server.');
    if (mentionedMember.user.id == client.user.id) return message.channel.send('You really thought i was boutta rickroll myself? AINT NO WAY CHEIF');

    const rickrollEmbed = new Discord.MessageEmbed()
    .setTitle("You've Been Rickrolled!")
    .setThumbnail('https://i1.wp.com/my-thai.org/wp-content/uploads/rick-astley.png?resize=984%2C675&ssl=1')
    .setDescription('Rick astley sends his regards')
    .setColor('#FFA500');

    await mentionedMember.send('https://tenor.com/view/dance-moves-dancing-singer-groovy-gif-17029825')
    await mentionedMember.send(rickrollEmbed)
    .then(() => {
      message.channel.send("Successfully rickrolled the user.");
   })
    .catch(err => {
      message.channel.send('I was unable to rickroll the user.');
    });
  }
}

奇怪的是它只适用于我的 ID,而不适用于任何其他用户的 ID。

【问题讨论】:

  • 我认为问题在于,当使用用户 ID 时,您无法检索带有提及的用户,因此它会尝试从缓存中获取它,但缓存中唯一的用户是您的.因此它不会找到任何其他用户

标签: javascript discord discord.js bots


【解决方案1】:

我认为问题在于,您使用的是缓存而不是公会成员管理器。因此,当没有其他用户被缓存时,它只会使用您的 ID,因为您是发送消息的人 => 被加载到缓存中。

试试这个:

async run(client, message, args) {
  let mentionedMember = message.mentions.members.first() || await message.guild.members.fetch(args[0]);
  if (!args[0]) return message.channel.send('You need to mention a member to rickroll.');
  if (!mentionedMember) return message.channel.send('The user mentioned is not in the server.');

  ...
}

我不确定,但如果您仍然喜欢缓存,它也可能会更优化一些,但只需使用成员管理器作为额外的备份,如下所示:

async run(client, message, args) {
  let mentionedMember = message.mentions.members.first()
    || message.guild.members.cache.get(args[0])
    || await message.guild.members.fetch(args[0]);
  if (!args[0]) return message.channel.send('You need to mention a member to rickroll.');
  if (!mentionedMember) return message.channel.send('The user mentioned is not in the server.');

  ...
}

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2021-11-13
    • 2019-08-02
    • 2021-01-09
    • 1970-01-01
    相关资源
    最近更新 更多