我正在寻找有关如何将此功能添加到我的机器人并使其打印出不一致的指导。 我希望机器人能够像输入命令/ combat那样将结果打印为discord而不是我的终端
import random
def combat():
hp = random.randint(1, 11)
ac = random.randint(1, 7)
print('Your HP is', hp, 'and your armor is', ac)
ehp = random.randint(1, 11)
eac = random.randint(1, 7)
print("My HP is", ehp, 'and my armor is', eac)
i = 0
while not hp < 0 | ehp < 0:
"""add counter"""
i = i + 1
'''hp = random.randint(1,11)
ac = random.randint(1,7)
print(hp, ac)
ehp = random.randint(1,11)
eac = random.randint(1,7)
print(ehp, eac)'''
print('Turn',i,':')
dmg = random.randint(1,9)
tdmg = dmg - eac
if tdmg < 0:
tdmg = 0
ehp = ehp - tdmg
print(' You dealt', tdmg, 'damage to me')
print(' I am at', ehp, 'health')
edmg = random.randint(1,9)
tedmg = edmg - ac
if tedmg < 0:
tedmg = 0
hp = hp - tedmg
print(' I dealt', tedmg, 'damage to you')
print(' You are at', hp, 'health')
if ehp < 1:
print('You win')
break
elif hp < 1:
print('I win')
break
combat()
Your HP is 3 and your armor is 5
My HP is 7 and my armor is 3
Turn 1 :
You dealt 0 damage to me
I am at 7 health
I dealt 3 damage to you
You are at 0 health
I win
答案 0 :(得分:1)
这假设您已经有一个机器人令牌。如果没有,请参见here。
您需要创建您的机器人,注册该命令,并将其转换为异步命令,然后使用send
代替print
。您还依靠print
来构建一些输出,我已经用f字符串代替了。
from discord.ext.commands import Bot
bot = Bot("/")
@bot.command()
async def combat(ctx):
hp = random.randint(1, 11)
ac = random.randint(1, 7)
await ctx.send(f'Your HP is {hp} and your armor is {ac}')
ehp = random.randint(1, 11)
eac = random.randint(1, 7)
await ctx.send(f'My HP is {ehp} and my armor is {eac}')
i = 0
while hp > 0 and ehp > 0:
i = i + 1
await ctx.send(f'Turn {i}:')
dmg = random.randint(1,9)
tdmg = dmg - eac
if tdmg < 0:
tdmg = 0
ehp = ehp - tdmg
await ctx.send(f' You dealt {tdmg} damage to me')
await ctx.send(f' I am at {ehp} health')
edmg = random.randint(1,9)
tedmg = edmg - ac
if tedmg < 0:
tedmg = 0
hp = hp - tedmg
await ctx.send(f' I dealt {tedmg} damage to you')
await ctx.send(f' You are at {hp} health')
if ehp < 1:
await ctx.send('You win')
break
elif hp < 1:
await ctx.send('I win')
break
bot.run("Token")