使用 discord.py 更改 Discord 机器人的昵称

Change Discord bot's nickname with discord.py

我正在制作一个 Discord 机器人,我想拥有它,所以当 on_ready 它将机器人的昵称设置为

⇨ ⇨ m O d E r A t O r B o T ⇦ ⇦

我也在用 repl.it:
https://repl.it/@CodingAndMemes/Moderator-Bot

您需要使用 Bot.get_guild to retrieve the specific Guild object for the server where you want to set your bot's nickname. Then, you can use the me attribute of the Guild object (Guild.me) to get the Member object that represents your bot in the server. With that Member object, you can use its edit coroutine method (Member.edit) 和该方法的 nick 参数,让它更改自己的昵称。

例如:

@bot.event
async def on_ready():
    guild = bot.get_guild(123456789)
    me = guild.me
    await me.edit(nick="⇨ ⇨ m O d E r A t O r  B o T ⇦ ⇦")

确保您的机器人有权更改自己的昵称。否则,这将引发 discord.Forbidden error. If you don't know whether your bot will have either the change_nickname or manage_nicknames permission, you should check for it first, e.g. with Member.guild_permissions.

如果您不想通过 ID 获取 Guild 对象,请参阅 https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-get-a-specific-model

我建议您先检查您的机器人的昵称是否已经不是您想要的昵称,然后再尝试更改它,即使用 Member.nick. That way you're not performing an unnecessary API request. This is especially true if this is going to be in on_ready,因为每次您启动机器人,之后可能多次。

我还建议您不要在 repl.it 上托管机器人。它可用于测试您是否正在这样做,但它不是为托管目的而设计的,尝试这样做有很多缺点。