user12327406
user12327406

Reputation:

Change Discord bot's nickname with discord.py

I am making a Discord bot and I want to have it so when on_ready it sets the bot's nickname to

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

I am also using repl.it:
https://repl.it/@CodingAndMemes/Moderator-Bot

Upvotes: 0

Views: 647

Answers (1)

Harmon758
Harmon758

Reputation: 5157

You'll want to use 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) and the nick parameter of that method, to have it change its own nickname.

For example:

@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 ⇦ ⇦")

Make sure your bot has permission to change its own nickname. Otherwise, this will raise a 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.

See https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-get-a-specific-model if you don't want to get the Guild object by ID.

I would advise you to check that your bot's nickname isn't already what you want it to be first before attempting to change it, i.e. with 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, as it would do so every time you launched the bot and potentially multiple times afterwards.

I'd also advise you not to host a bot on repl.it. It can be used for testing if that's what you're doing, but it's not designed for hosting purposes and there are a lot of disadvantages to attempting to do so.

Upvotes: 1

Related Questions