简体   繁体   English

如何在 discord.py 中创建自定义循环状态?

[英]How do I create a custom cycling status in discord.py?

I have a discord bot that I'm using to learn how to use the API and to practise my python knowledge.我有一个 discord 机器人,我用它来学习如何使用 API 并练习我的 python 知识。 I'm trying to create a system that randomly chooses a status and applies it then waits a short time, currently 10 minutes, then continue like that infinitely until the bot is shutdown.我正在尝试创建一个随机选择状态并应用它的系统,然后等待很短的时间,目前为 10 分钟,然后无限继续,直到机器人关闭。 Currently, I have the loop in my on_ready( ) function but the bot shuts down after a short time (less than 10 minutes, around 2-3) and I can't figure out why.目前,我的on_ready( ) function 中有循环,但机器人在短时间内(不到 10 分钟,大约 2-3 分钟)后关闭,我不知道为什么。 The current code is this:当前代码是这样的:

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))
  infiniteTime = 1
  while infiniteTime != 20:
    statusType = random.randint(0, 1)
    if statusType == 0:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
    else:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
    time.sleep(600)

watchingStatus and listeningStatus are 1D arrays with 11 strings in. Any assistance/feedback is appreciated as I'm new to the API. watchingStatus状态和listeningStatus状态是 1D arrays,有 11 个字符串。感谢任何帮助/反馈,因为我是 API 的新手。 Cheers!干杯!

ETA: Should probably clarify that I've realised that having the code in the on_ready() function means that it never ends and so can't do anything else. ETA:应该澄清一下,我已经意识到在on_ready() function 中有代码意味着它永远不会结束,所以不能做任何其他事情。 I'm asking because I don't know why the bot shuts-down on Discord's end without me stopping the code and I don't know where else I should put this loop as I'm new to this async/await system and the way the API functions.我问是因为我不知道为什么机器人会在不停止代码的情况下在 Discord 结束时关闭,而且我不知道我应该把这个循环放在哪里,因为我是这个 async/await 系统的新手,而且API 功能的方式。

The place where you are trying to change the status of the bot is correct, the problem is the way of trying to create this loop is stopping your bot from working, as time.sleep is not suitable for an asynchronous library such as discord.py.您尝试更改机器人状态的地方是正确的,问题是尝试创建此循环的方式正在阻止您的机器人工作,因为time.sleep不适合异步库,例如 discord.py。 Instead you can use asyncio.sleep() .相反,您可以使用asyncio.sleep()

You could do something like this:你可以这样做:

import asyncio

@client.event
async def on_ready():
  # ...
  # Here goes your code for on_ready()
  # ...
  while True:
    statusType = random.randint(0, 1)
    if statusType == 0:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
    else:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
    asyncio.sleep(600)

Another way of doing this would be using discord.ext.tasks , which are functions defined to be running background tasks like the one you want to achieve.另一种方法是使用discord.ext.tasks ,这些函数定义为运行您想要实现的后台任务。

from discord.ext import commands, tasks
import asyncio

# Your code here

@loop(seconds=600)
async def status_change():
    # ...
    # Some code to define playingStatus and watchingStatus arrays
    # ...
    statusType = random.randint(0, 1)
    if statusType == 0:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
    else:
      statusNum = random.randint(0, 10)
      await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))

status_change.before_loop(client.wait_until_ready())    
status_change.start()
client.run("TOKEN")

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM