繁体   English   中英

discord.js 无法使用带有用户 ID 的命令

[英]discord.js Cant use command with user ID

我正在制作一个命令,通过向他们发送一个 dm 来向提到的用户发送一个命令,当我尝试通过提及使用他们的 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,因为您是发送消息的人 => 被加载到缓存中。

尝试这个:

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

  ...
}

暂无
暂无

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

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