Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在discord.py中创建自定义循环状态?_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

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

Python 如何在discord.py中创建自定义循环状态?,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我有一个discord机器人,用来学习如何使用API和练习python知识。我正在尝试创建一个系统,随机选择一个状态并应用它,然后等待很短的时间,目前为10分钟,然后无限期地继续,直到bot关闭。目前,我的on_ready()功能中有一个循环,但bot会在短时间后关闭(不到10分钟,大约2-3分钟),我不知道为什么。当前代码如下: @client.event async def on_ready(): print("We have logged in as {0.user}&quo

我有一个discord机器人,用来学习如何使用API和练习python知识。我正在尝试创建一个系统,随机选择一个状态并应用它,然后等待很短的时间,目前为10分钟,然后无限期地继续,直到bot关闭。目前,我的
on_ready(
)功能中有一个循环,但bot会在短时间后关闭(不到10分钟,大约2-3分钟),我不知道为什么。当前代码如下:

@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
listingstatus
是1D数组,其中包含11个字符串。感谢您的任何帮助/反馈,因为我是API新手。干杯


ETA:我应该澄清一下,我已经意识到,将代码放在
on_ready()
函数中意味着它永远不会结束,因此不能做任何其他事情。我这样问是因为我不知道为什么机器人会在不停止代码的情况下在Discord端关闭,我也不知道我还应该把这个循环放在哪里,因为我是这个异步/等待系统的新手,API的工作方式也是如此。

您尝试更改机器人状态的地方是正确的,问题是,尝试创建此循环的方法会停止bot的工作,因为
时间。sleep
不适用于discord.py等异步库。相反,您可以使用
asyncio.sleep()

你可以这样做:

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)
另一种方法是使用
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")
参考资料:


您尝试更改bot状态的位置是正确的,问题是尝试创建此循环的方式正在停止bot的工作,因为
时间。sleep
不适用于异步库,如discord.py。相反,您可以使用
asyncio.sleep()

你可以这样做:

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)
另一种方法是使用
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")
参考资料:


您应该为此使用任务循环:您应该为此使用任务循环:两种方法都会出现错误。第一次正确引导并登录,但随后表示从未等待“睡眠”,以使tracemalloc获得回溯()。第二个表示没有定义名称“loop()。有什么想法吗?我的错,因为你的代码很难复制,我无法测试它是否完全工作。对于第一个,您需要等待睡眠:
等待asyncio.sleep(600)
。对于第二种方法,您可能需要使用
从tasks import loop
显式导入循环。这很有效,谢谢!很抱歉,这是如此难以复制,这是相当简陋,所以有点难以确定!两种方法都有错误。第一次正确引导并登录,但随后表示从未等待“睡眠”,以使tracemalloc获得回溯()。第二个表示没有定义名称“loop()。有什么想法吗?我的错,因为你的代码很难复制,我无法测试它是否完全工作。对于第一个,您需要等待睡眠:
等待asyncio.sleep(600)
。对于第二种方法,您可能需要使用
从tasks import loop
显式导入循环。这很有效,谢谢!很抱歉,这是如此难以复制,这是相当简陋,所以有点难以确定!