【发布时间】:2021-12-20 20:52:28
【问题描述】:
我正在为我所在的 D&D 服务器开发一个掷骰子机器人,它应该循环并反复测试来自用户说掷骰子或结束战斗的消息,但出于某种原因它只是行不通。没有错误信息,什么都没有。有人知道怎么解决吗?
@bot.command()
async def battle(ctx, arg, arg2):
await ctx.send("**A battle has begun between "+arg+" and "+arg2+"!**")
await ctx.send("_ _")
await ctx.send("Would you like to use dice roll mechanics for this battle? [yes/no]")
@bot.event
async def on_message(message):
if message.content == "no" or message.content == "No" or message.content == "NO" or message.content == "n" or message.content == "N":
await ctx.send("Ok! Pin the battle message to archive this fight and get to RPing!")
if message.content == "yes" or message.content == "Yes" or message.content == "YES" or message.content == "y" or message.content == "Y":
await ctx.send("Alright! Whenever your character makes a move or performs an action, say 'roll die' to roll a 20-sided die and see how effective your action was! To end the battle, say 'end battle'!")
fight = 0
while fight == 0:
@bot.event
async def on_message(message):
if message.content == "roll die" or message.content == "ROLL DIE" or message.content == "Roll die" or message.content == "Roll Die":
roll = random.randint(1, 20)
await ctx.send("You rolled: "+str(roll))
if message.content == "end battle" or message.content == "END BATTLE" or message.content == "End battle" or message.conent == "End Battle":
await ctx.send("Battle is over! Pin the starting battle message I sent to archive this fight!")
fight+=1
【问题讨论】:
-
你为什么要定义一个函数
on_message()在一个名为on_message()的函数?在while循环内?并且从不调用它? -
查看 discord.py 的
Client.wait_for()方法。
标签: python while-loop discord discord.py bots