如何在interactions.py中修改Discord机器人的在线状态?
Issue Setting Discord Bot Presence with interactions.py
I'm working on a Discord bot using the interactions.py library, but I'm stuck trying to set its online presence activity. No matter what I do, the bot's status doesn't update to show the game I want. Here's the code I've been testing:
import interactions import os from keep_alive import keep_alive bot = interactions.Client(token=os.getenv('TOKEN'), command_prefix="!f") @bot.event async def on_start(): activity = interactions.ClientPresence( name="zkus /mravenci", type=0, ) await bot.change_presence( activity=interactions.PresenceActivityType.GAME(activity))
What's Wrong Here?
There are two key issues with your current code:
- Incorrect class usage: You're using
ClientPresenceto define the activity, but that's not the right class for this purpose.ClientPresencerepresents the full presence state (including status like online/idle), not just the activity itself. You should useinteractions.Activityinstead. - Misusing the ActivityType enum:
PresenceActivityType.GAMEis an enum member, not a function you can call with an argument. You specify the activity type as a parameter when creating the Activity object.
Corrected Code
Here's the fixed version that should properly set your bot's presence:
import interactions import os from keep_alive import keep_alive bot = interactions.Client(token=os.getenv('TOKEN'), command_prefix="!f") @bot.event async def on_start(): # Create the activity object with the correct type and name activity = interactions.Activity( name="zkus /mravenci", type=interactions.PresenceActivityType.GAME ) # Update the bot's presence with the activity await bot.change_presence(activity=activity)
Explanation of Changes
- Swapped
ClientPresenceforinteractions.Activity, which is the correct class to define what the bot is "doing". - Set the
typeparameter directly toPresenceActivityType.GAMEinstead of trying to wrap the activity with it. This tells Discord that the bot is playing a game. - Cleaned up indentation for better readability.
Once you run this corrected code, your bot should show up as "Playing zkus /mravenci" in Discord's user list.
内容的提问来源于stack exchange,提问作者Jakub Šidlík
相关产品推荐
相关产品推荐

