如何在discord.py中创建石头剪刀布命令

时间:2020-12-23 21:41:06

标签: python discord.py discord.py-rewrite

我对 discord.py 很陌生,我想创建一个石头剪刀布命令。我让它工作了,但我想使用用户输入来完成。

我尝试使用 await bot.wait_for 代码,但由于某种原因它不起作用。我没有收到任何错误,但它不起作用,我不知道为什么。这是我的代码:

from discord.ext.commands import Bot
import random

bot = Bot(".")

@bot.command(help="Play with .rps [your choice]")
async def rps(ctx):
    rpsGame = ['rock', 'paper', 'scissors']
    await ctx.send(f"Rock, paper, or scissors? Choose wisely...")

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and msg.content.lower() in rpsGame

    user_choice = await bot.wait_for('message', check=check)

    comp_choice = random.choice(rpsGame)
    if user_choice == 'rock':
        if comp_choice == 'rock':
            await ctx.send(f'Well, that was weird. We tied.\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'paper':
            await ctx.send(f'Nice try, but I won that time!!\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'scissors':
            await ctx.send(f"Aw, you beat me. It won't happen again!\nYour choice: {user_choice}\nMy choice: {comp_choice}")

    elif user_choice == 'paper':
        if comp_choice == 'rock':
            await ctx.send(f'The pen beats the sword? More like the paper beats the rock!!\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'paper':
            await ctx.send(f'Oh, wacky. We just tied. I call a rematch!!\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'scissors':
            await ctx.send(f"Aw man, you actually managed to beat me.\nYour choice: {user_choice}\nMy choice: {comp_choice}")

    elif user_choice == 'scissors':
        if comp_choice == 'rock':
            await ctx.send(f'HAHA!! I JUST CRUSHED YOU!! I rock!!\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'paper':
            await ctx.send(f'Bruh. >: |\nYour choice: {user_choice}\nMy choice: {comp_choice}')
        elif comp_choice == 'scissors':
            await ctx.send(f"Oh well, we tied.\nYour choice: {user_choice}\nMy choice: {comp_choice}")

我能做些什么来修复它?

2 个答案:

答案 0 :(得分:0)

user_choice 是整个消息对象,它包含存储消息内容的 content 属性。

这应该可以解决您的问题:

user_choice = (await bot.wait_for('message', check=check)).content

参考:

答案 1 :(得分:0)

你可以这样做:

@bot.command()
async def rps(ctx, choice):
    choices=["rock", "paper", "scissors"]
    if choice not in choices:
        await ctx.send("error: please put rock, paper or scissors")
    else:
        await ctx.send(random.choice(choices))