gpt4 book ai didi

python - 使用 discord.py 运行多个命令

转载 作者:行者123 更新时间:2023-12-04 09:37:42 25 4
gpt4 key购买 nike

使用 discord.py 和 python:

好的,基本上我有这个机器人,它每分钟更新某个游戏的最优惠价格。但是,当我这样做时,其他人无法访问该机器人。例如,假设我有一个名为“hello”的命令,当调用该命令时,会在聊天中打印 hello。由于代码一直在运行,用户无法调用命令hello,因为代码太忙于运行每分钟更新的代码。有什么办法可以让它运行 updateminute 代码,而其他人也可以输入命令?

import discord
import asyncio
import bazaar
from discord.ext import commands, tasks

client = commands.Bot(command_prefix = '.')


@client.command()
async def calculate(ctx):
while True:
await ctx.send(file2.calculate())
await asyncio.sleep(210)

@client.command()
async def hello(ctx):
await ctx.send("Hello")

client.run(token)

在 file2.py 中:
def updateminute():
for product in product_list:
#Grab Api and stuff
#check to see whether it is profitable
time.sleep(0.3) #cause if i don't i will get a key error

#calculate the stuff
#return the result


综上所述,由于bot太忙于计算updateminute和等待,其他人无法访问bot。有什么方法可以尝试解决这个问题,以便机器人计算其内容,以便人们可以使用机器人命令?谢谢!

最佳答案

您可以查看threading !基本上,运行两个单独的线程:一个用于接收请求,另一个用于更新价格。

关于python - 使用 discord.py 运行多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62481368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com