使用discord.js更改用户昵称 [英] Change user nickname with discord.js

查看:914
本文介绍了使用discord.js更改用户昵称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您是否可以提供帮助(我什么也没搜索...),我正在学习如何使用discord.js节点,并且我想更改用户昵称(而不是用户名本身)

I wonder if you can help (I search it and nothing...) I am learning how to work with discord.js node and I want to change my user nickname (not the username itself)

我的代码是

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
    console.log('I am ready!');
});

client.on('message', message => {
    if (message.content.includes('changeNick')) {
        client.setNickname({nick: message.content.replace('changeNick ', '')});
    }
});

client.login('token');


推荐答案

Discord.js通过获取 GuildMember ,并使用 GuildMember#setNickname 方法。这是设置运行消息的用户昵称的简单示例:

Discord.js implements changing nicknames by getting the GuildMember from the message, and using the GuildMember#setNickname method. Here's a simple example of setting the nickname of the user who ran the message:

if (message.content.includes('changeNick')) {
    message.member.setNickname(message.content.replace('changeNick ', ''));
}

但是,如果您的机器人未经许可,这根本不会做设置用户的昵称。如果您想设置用户的昵称,则漫游器本身必须具有设置昵称的权限。这需要更多技巧,但是您可以使用 Guild#me 来获取GuildMember,然后使用 GuildMember#hasPermission 来检查在权限#标志。我知道可能要花很多钱,所以这里有一个我刚刚说过的事情做一个例子。

But this simply won't do in the case of your bot not having permission to set a user's nickname. If you want to set a user's nickname, the bot itself will have to have permission to set nicknames. This requires a little more trickery, but you can do this using Guild#me to get the GuildMember, and then use GuildMember#hasPermission to check for the MANAGE_NICKNAMES permission found in Permissions#Flags. I know this can be a lot to take in, so here's an example of doing everything I just said put together.

if (message.content.includes('changeNick')) {
    if (!message.guild.me.hasPermission('MANAGE_NICKNAMES')) return message.channel.send('I don\'t have permission to change your nickname!');
    message.member.setNickname(message.content.replace('changeNick ', ''));
}

这将设置运行命令昵称的用户。但是,如果我们要更改BOT的昵称而不是用户的昵称,该怎么办?嗯,很简单。我们可以将 message.member.setNickname 替换为 message.guild.me.setNickname ,类似于我们检查权限的方式。这将更改机器人的昵称,而不是运行命令的用户的昵称。祝您编程愉快!

And this will work to set the user who ran the command's nickname. But what if we want to change the BOT'S nickname, not the user's? Well that's simple. We can just replace message.member.setNickname with message.guild.me.setNickname, similarly to how we checked permissions. This will change the bot's nickname instead of the user who ran the command's. Happy coding!

这篇关于使用discord.js更改用户昵称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆