[英]How to use User Input in python discord.py Disord Bot
I would like to create a Discord Bot that is able to play rock, paper, scissors.我想创建一个能够玩石头、纸和剪刀的Discord Bot 。 I think that the basic code of the game isn't complicated but the discord.py implementation is hard for me.我认为游戏的基本代码并不复杂,但 discord.py 实现对我来说很难。 The Problem is probably this line of code player = await bot.wait_for('message')
.问题可能是这行代码player = await bot.wait_for('message')
。 I have read the discord.py docs, but it doesn't seem to work correctly.我已经阅读了 discord.py 文档,但它似乎无法正常工作。 This is my Code:这是我的代码:
@bot.command(pass_context=True, aliases=['g', 'ga', 'gam'])
@commands.guild_only()
async def game(ctx):
await ctx.send(
'Hallo, wie gehts denn so ' + ctx.message.author.mention + '? Lust auf eine Runde Schere, Stein, Papier?')
wins = 0
losses = 0
ties = 0
while True:
await ctx.send('%s Siege, %s Niederlagen, %s Unentschieden \n' % (wins, losses, ties))
while True:
await ctx.send('Auf was setzt du? (s)tein, (p)apier, (sc)here oder (q)uit')
player = await bot.wait_for('message')
print(str(player))
if player == 'q':
return
if player == 's' or player == 'p' or player == 'sc':
break
await ctx.send('Schreib s, p, sc oder q!')
if player == 's':
await ctx.send('Stein gegen...')
elif player == 'p':
await ctx.send('Papier gegen...')
elif player == 'sc':
await ctx.send('Schere gegen...')
randomnum = random.randint(1, 3)
if randomnum == 1:
computer = 's'
await ctx.send('Stein!')
elif randomnum == 2:
computer = 'p'
await ctx.send('Papier!')
elif randomnum == 3:
computer = 'sc'
await ctx.send('Schere!')
if player == computer:
await ctx.send('Unentschieden!')
ties = ties + 1
elif player == 's' and computer == 'sc':
await ctx.send('Du gewinnst!')
wins = wins + 1
elif player == 's' and computer == 'p':
await ctx.send('Ich habe gewonnen!')
losses = losses + 1
elif player == 'p' and computer == 's':
await ctx.send('Du gewinnst!')
wins = wins + 1
elif player == 'p' and computer == 'sc':
losses = losses + 1
await ctx.send('Ich habe gewonnen!')
elif player == 'sc' and computer == 'p':
await ctx.send('Du gewinnst!')
wins = wins + 1
elif player == 'sc' and computer == 's':
await ctx.send('Ich habe gewonnen!')
losses = losses + 1
You may want to write a check so that it knows who the author is.您可能想写一张支票,以便它知道作者是谁。
def check(author):
def inner_check(message):
return message.author == author
return inner_check
Then you would pass the inner_check to wait_for by calling the outer_check with the appropriate argument:然后,您可以通过使用适当的参数调用 outer_check 将 inner_check 传递给 wait_for:
playerChoice = await bot.wait_for('message', check=check(context.author), timeout=30)
I found a solution.我找到了解决方案。 Just change the player variable to player.content an it should work.只需将 player 变量更改为 player.content就可以了。
if player.content ==
Thanks for the effort!感谢您的努力!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.