简体   繁体   English

如何在 discord.js v13 中创建一个 setNickname 命令

[英]how to make a setNickname command in discord.js v13

I want to create a Change Nickname command in discord.js v13 , and It's not working.我想在discord.js v13中创建一个更改昵称命令,但它不起作用。 My code:我的代码:

            const target = message.mentions.members.first();
            const nickname = args.slice(1).join(' ');
            if (!target) return message.channel.send('Please specify a target to change nickname');
            if (!nickname) return message.channel.send('Please specify a nickname to change');


            target.setNickname(nickname);

I am using node.js v16我正在使用 node.js v16

命令

此用户的昵称应更改为 oskol

I'm going to throw a guess that the nickname you are trying to set is null or empty, this will cause discord to just reset the nickname to the users normal discord username.我将猜测您要设置的昵称是 null 还是空的,这将导致 discord 将昵称重置为用户正常的 discord 用户名。

Make sure to debug the values that are being passed on and providing such information when making a question on here as it will help people more easily help you.确保调试正在传递的值并在此处提出问题时提供此类信息,因为这将帮助人们更轻松地帮助您。



With that said, the below code worked fine for me话虽如此,下面的代码对我来说效果很好

    const target = msg.mentions.members.first();
    if (!target) return msg.reply('Please mention a user');
    const nick = args[1];
    if (!nick) return msg.reply('Please provide a nickname');
    const oldNick = target.nickname;
    if (oldNick === nick) return msg.reply('That user already has that nickname');
    console.log(`Changing ${target.user.tag}'s nickname from ${oldNick} to ${nick}`);
    target.setNickname('');

Your code is working for me, but make sure your bot has the following Permissions and the bot's role is above the role of the users who want to edit his nick: Change this: https://i.stack.imgur.com/lKP9h.png To this: https://i.stack.imgur.com/xX8GF.png Also make sure your command is lowercase because uppercase characters are not allowed in command names.您的代码对我有用,但请确保您的机器人具有以下权限,并且机器人的角色高于想要编辑他的昵称的用户的角色:更改此: https://i.stack.imgur.com/lKP9h .png为此: https://i.stack.imgur.com/xX8GF.png还要确保您的命令是小写的,因为命令名称中不允许使用大写字符。

The code you provided seems to work correctly.您提供的代码似乎可以正常工作。 The error might be your bot's intents.错误可能是您的机器人的意图。 Make sure that you enabled/requested all intents you need for this command (guild members if I remember correctly).确保您启用/请求了此命令所需的所有意图(如果我没记错的话,公会成员)。 Make sure that you also gave your bot the required permissions: MANAGE_NICKNAMES in the server settings (roles).确保您还为您的机器人提供了所需的权限:服务器设置(角色)中的MANAGE_NICKNAMES

Good luck!祝你好运!

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

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