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

How to create a Rock Paper Scissors command in discord.py

我是 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}")

我该如何解决?

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

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

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

参考文献:

你可以这样做:

@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))
@bot.command()
async def rps(ctx, choice):
    asyncio.sleep(1)
    choices=["rock", "paper", "scissors"]
    comp_choice = random.choice(choices)
    asyncio.sleep(10)
    await ctx.send(f'GGS!, you chose {choice} and i chose {comp_choice} ')
    if choice not in choices:
        await ctx.send("error: please put rock, paper or scissors")