简体   繁体   English

Discord.js SetNickname Invalid Form_Body

[英]Discord.js SetNickname Invalid Form_Body

I searched for someone with the same error but didn't found.我搜索了有同样错误的人,但没有找到。 I'm trying to make a command that sets the nickname of a user but it gives me an error when changing the nickname (actually the code works because if I change the "mentionedMember.setNickname(args)" to something else like "mentionedMember.setNickname(message.author.id)" it works).我正在尝试创建一个设置用户昵称的命令,但在更改昵称时它会给我一个错误(实际上代码有效,因为如果我将“mentionedMember.setNickname(args)”更改为“mentionedMember.setNickname(args)”之类的其他内容)。 setNickname(message.author.id)”有效)。

The error:错误:

UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body nick: Could not interpret "['testing']" as string. UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body nick: 无法将“['testing']”解释为字符串。

The code:代码:

const Discord = require('discord.js');
const Client = new Discord.Client();
const Command = require('../../Structures/Command');
const { MessageEmbed } = require('discord.js');
const config = require('../../../config.json');

module.exports = class extends Command {
 constructor(...args) {
  super(...args, {
   aliases: ['nickname', 'name'],
   description: "changes someone's nickname.",
   category: 'mod',
   usage: '<@person> <new nickname>',
  });
 }
 async run(message, target) {
  let userId = message.content.substring(message.content.indexOf(' ') + 1);
  const args = message.content.split(' ').slice(2);
  const mentionedMember =
   message.mentions.members.first() ||
   message.guild.members.cache.get(target) ||
   message.guild.members.cache.get(args[0]);
  const now = new Date();

  if (!mentionedMember) {
   try {
    if (!message.guild.members.get(args.slice(0, 1).join(' ')))
     throw new Error("There isn't someone with this ID!");
    user = message.guild.members.get(args.slice(0, 1).join(' '));
    user = user.user;
   } catch (error) {
    return message.channel.send(
     `${message.author.username}, this username doesn't exists!`
    );
   }
  }
  if (
   (mentionedMember.id !== message.guild.owner.id,
   mentionedMember.id !== this.client.owners)
  ) {
   if (!message.member.hasPermission('MANAGE_NICKNAMES'))
    return message.channel.send("You don't have permissions!");
   if (!message.guild.me.hasPermission('MANAGE_NICKNAMES'))
    return message.channel.send(
     "I don't have permissions to manage nicknames. Give it to me!"
    );
   if (
    mentionedMember.roles.highest.position >=
    message.member.roles.highest.position
   ) {
    return message.channel.send("You can't change this user's nickname.");
   }

   if (!args) {
    return message.channel.send('Remember mentioning someone!');
   }

   const LogChannel = await message.guild.channels.cache.find(
    (channel) => channel.id == config.logModeraçãoId
   );
   var embed = new Discord.MessageEmbed()
    .setAuthor(
     `${message.author.username} - (${message.author.id})`,
     message.author.displayAvatarURL()
    )
    .setThumbnail(mentionedMember.user.displayAvatarURL())
    .setColor('#BA1F1F').setDescription(`
               **Member:** ${mentionedMember.user.username} - (${
    mentionedMember.user.id
   })
               **Action:** changing nickname
               **Reason:** ${'not specified'}
               **Time:** ${now}
               `);
   LogChannel.send(embed);
   message.channel.send('Nickname changed!');
   mentionedMember.setNickname(args);
  }
 }
};

the function .setNickname() accepts a parameter of type string. function .setNickname()接受字符串类型的参数。 message.author.id is a string which is why it worked. message.author.id是一个字符串,这就是它起作用的原因。 args is an array so it doesn't work. args是一个数组,所以它不起作用。

args is an array, but GuildMember.setNickname() requires a string. args是一个数组,但GuildMember.setNickname()需要一个字符串。 You'll need to use args[0] to get the string you want.您需要使用args[0]来获取所需的字符串。

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

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