【发布时间】:2021-08-11 01:04:26
【问题描述】:
我想将 Discord 机器人与命令行应用程序结合起来。但是,在启动时调用函数 fruit() 后,我的 discord bot 命令将不起作用(没有错误)。机器人运行后,fruit() 函数将无限运行。 client.run 之后的任何代码都将不起作用。有什么解决方案可以修复我的代码,以便在运行脚本时激活命令并运行机器人,然后永远运行 main 函数?
from discord.ext import commands
token = "my token"
client = commands.Bot(command_prefix=">")
def fruit():
option = input("Choose a fruit, 1 for banana, 2 for lime")
if option == 1:
print("Bananas are sweet")
fruit()
elif option == 2:
print("Limes are sour")
fruit()
else:
print("Invalid option")
fruit()
@client.command()
async def sayhi(ctx):
await ctx.send("Hi")
@client.event
async def on_ready():
fruit()
client.run(token)```
【问题讨论】:
-
尝试输入here的一些解决方案。此外,您可能希望更改代码,以免最终达到递归限制。
标签: python discord discord.py