我正在寻找关于如何将此功能添加到我的机器人并让它打印到不和谐的一些指导。我希望机器人能够在输入命令/战斗时将结果打印到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
这假设您已经拥有机器人令牌。如果没有,请参阅here。
您需要创建机器人,注册命令,并将其转换为异步,并使用send
而不是print
。您还依靠print
来构建一些输出,我用f-strings替换了它们。
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")