如何更改 discord.py 机器人的活动?

新手上路,请多包涵

我想将机器人状态从播放更改为观看。我已经试过了,但它仍在播放状态:

 import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

PREFIX = ("$")
bot = commands.Bot(command_prefix=PREFIX, description='Hi')

@bot.event
async def on_ready():
    activity = discord.Game(name="Netflix", type=3)
    await bot.change_presence(status=discord.Status.idle, activity=activity)
    print("Bot is ready!")

bot.run('TOKEN')

原文由 Squv 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 594
2 个回答

您可以使用以下代码行,具体取决于您要将机器人更改为的活动:

 # Setting `Playing ` status
await bot.change_presence(activity=discord.Game(name="a game"))

# Setting `Streaming ` status
await bot.change_presence(activity=discord.Streaming(name="My Stream", url=my_twitch_url))

# Setting `Listening ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))

# Setting `Watching ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))

参考:

bot.change_presence

原文由 Squv 发布,翻译遵循 CC BY-SA 4.0 许可协议

提醒大家, 不要 在您的机器人或客户端中 on_ready 中的_presence(或进行 API 调用)。在 READY 或 GUILD_CREATE 事件(1006 或 1000 关闭代码)期间,Discord 很有可能完全断开您的连接,您无法采取任何措施来阻止它。

相反,在这些类的构造函数中设置 activitystatus kwargs。

播放-> activity = discord.Game(name="!help")

流媒体-> activity = discord.Streaming(name="!help", url="twitch_url_here")

听力-> activity = discord.Activity(type=discord.ActivityType.listening, name="!help")

观看-> activity = discord.Activity(type=discord.ActivityType.watching, name="!help")

 bot = commands.Bot(command_prefix="!", activity=activity, status=discord.Status.idle)

基本上: 不要在 on_ready 中做事。

原文由 Daud 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题