Hey everyone! I’m working on a Discord bot and I want to add an away-from-keyboard (AFK) feature. I’ve written some code but I’m having trouble with the nickname part. It just won’t change. Can anyone help me figure out what’s wrong? Here’s what I’ve got so far:
@bot.command(name='away')
async def set_afk(ctx):
user = ctx.author
server = ctx.guild
original_name = user.display_name
afk_status = discord.utils.get(server.roles, name='Away')
if not afk_status:
afk_status = await server.create_role(name='Away')
try:
await ctx.author.edit(nick=f'[AFK] {original_name}')
await user.add_roles(afk_status)
await ctx.send(f'{user.mention} is now AFK.')
except:
await ctx.send('Oops! Something went wrong.')
I’m pretty new to Discord bot development so any advice would be awesome. Thanks in advance!
Hey there, Isaac_Cosmos! I’ve worked with Discord bots before, and I think I can help you out with your AFK feature. The issue you’re facing with the nickname change is probably due to permissions. Make sure your bot has the ‘Manage Nicknames’ permission and that its role is higher in the server hierarchy than the users it’s trying to modify.
Also, keep in mind that bots can’t change the server owner’s nickname - that’s a Discord limitation. You might want to add some error handling to catch these cases. Here’s a quick tip: use discord.errors.Forbidden to catch permission-related errors specifically.
One last thing - consider adding a way to remove the AFK status. Users will appreciate being able to easily come back from AFK. Good luck with your bot development!
I’ve encountered similar issues with Discord bots before. The nickname problem is likely due to permissions. Ensure your bot has ‘Manage Nicknames’ permission and its role is higher in the server hierarchy than the users it’s modifying. Also, bots can’t change the server owner’s nickname - that’s a Discord limitation.
For better error handling, try using specific exceptions like discord.errors.Forbidden to catch permission issues. This way, you can provide more informative feedback to users when something goes wrong.
Consider adding a way to remove the AFK status too. Users will appreciate an easy way to indicate they’re back. Lastly, make sure to test the bot thoroughly in different scenarios to catch any edge cases.
yo, i had this issue too. make sure ur bot has the right perms, like ‘manage nicknames’ and its role is higher than the users. also, it cant change server owner’s nick. try adding some error handling to see whats goin wrong. gl with ur bot!