Skip to content
Advertisement

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

I’m very new to discord.py and I want to create a Rock, Paper, Scissors command. I got it working, but I want to do it using user input.

I tried using the await bot.wait_for code, but for some reason it doesn’t work. I’m not getting any errors, but it just doesn’t work and I can’t figure out why. Here’s my code:

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

What can I do to fix it?

Advertisement

Answer

user_choice is the whole message object, it contains an attribute for content which stores the content of the message.

This should fix your problem:

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

References:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement