【问题标题】:how to change discord.py bot activity如何更改 discord.py 机器人活动
【发布时间】:2020-03-26 07:28:27
【问题描述】:

这对我来说可能很难,但我相信 stackoverflow 的力量,

我想将机器人状态从播放更改为观看。我试试这个,但它仍在播放状态。

代码:

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')

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    你可以使用这个 Ezz!

    # 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"))
    

    【讨论】:

      【解决方案2】:

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

      相反,在这些类的构造函数中设置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做事。

      【讨论】:

      • 这是正确答案,只需在on_ready 中获取使用change_presence 的速率限制:)
      • 哈哈,有人在这个 xD 中改了一个词,我记得读过旧版本
      【解决方案3】:

      根据this issue,Client.change_presence 中的游戏关键字参数已重命名为活动,因此您的代码应如下所示

      activity = discord.Game(name="Just")
      await client.change_presence(status=discord.Status.idle, activity=activity)
      

      【讨论】:

        【解决方案4】:

        机器人仍然可以是 Playing XStreaming Y,只是 NOT 自定义状态

        https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.change_presence

        【讨论】:

          【解决方案5】:

          可以试试

          client = commands.Bot (command_prefix = "!" , activity = discord.Game(name="your help command here")) 
          

          播放状态

          而不是在命令或就绪时执行。

          【讨论】:

            【解决方案6】:

            只需使用:

            @client.event
            async def on_ready():
                await client.change_presence(activity=discord.Streaming(name='Fortnite', url='https://www.twitch.tv/UR_TWITCH_GOES_HERE You cant do YT only Twitch.'))
                print("Bot is connected to all of the available servers in the bots mainframe.")
            

            对于流媒体,但对于其他我无能为力。

            【讨论】:

              【解决方案7】:

              如果你想要正常的存在,那么这样做:

              
              await bot.change_presence(activity=discord.Streaming(name="My Stream", url=my_twitch_url))
              
              await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))
              
              await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))```
              
              #but if you want to make the bot change status every 5 minutes try this :
              
              async def ch_pr():
               await client.wait_until_ready()
              
               statuses = ["Vodka Or beer? || bb:help",f"listening on {len(client.guilds)} server's","Still need help? do bb:guide for more help!"]
              
               while not client.is_closed():
              
                 status = random.choice(statuses)
              
                 await client.change_presence(activity=discord.Game(name=status))
              
                 await asyncio.sleep(5)
              
              client.loop.create_task(ch_pr())
              

              【讨论】:

                【解决方案8】:
                import discord
                from discord.ext import commands
                import datetime
                
                from urllib import parse, request
                import re
                
                bot = commands.Bot(command_prefix='prefix here', description="desc here")
                
                @bot.event
                async def on_ready():
                    await bot.change_presence(activity=discord.Streaming(name="to keep it a secret", url="http://www.twitch.tv/dheeran2010"))
                    print('Im Ready')
                
                
                bot.run('Token here')
                

                【讨论】:

                • 请修正格式,并解释这是如何回答问题的
                猜你喜欢
                • 2019-05-26
                • 2021-03-10
                • 1970-01-01
                • 1970-01-01
                • 2021-08-07
                • 1970-01-01
                • 2019-02-20
                • 2021-10-30
                • 2019-04-09
                相关资源
                最近更新 更多