FakeBlob
FakeBlob

Reputation: 60

How to make a random reaction game (discord.py)

How do I make my code, so that it chooses 1/4 possible answers as an answer, and when a discord user reacts to either the right answer or the wrong answer it returns a winner or lost answer. Also i would like for the answers to be random every time.


class games(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    @commands.command()
    async def findimposter(self, ctx):
        embed1 = discord.Embed(title = "Who's the imposter?" , description = "Find out who the imposter is before the reactor breaks down!" , color=0xff0000)
        embed1.add_field(name = 'Red' , value= '<:redcrewmates:776867415514153031>' , inline=False)
        embed1.add_field(name = 'Blue' , value= '<:bluecrewmates:776867439085617153>' , inline=False)
        embed1.add_field(name = 'Lime' , value= '<:limecrewmates:776867489866711041>' , inline=False)
        embed1.add_field(name = 'White' , value= '<:whitecrewmates:776867529900425217>' , inline=False)
        msg1 = await ctx.send(embed=embed1)
        redcrew = '<:redcrewmates:776867415514153031>'
        bluecrew = '<:bluecrewmates:776867439085617153>'
        limecrew = '<:limecrewmates:776867489866711041>'
        whitecrew = '<:whitecrewmates:776867529900425217>'
        await msg1.add_reaction(redcrew)
        await msg1.add_reaction(bluecrew)
        await msg1.add_reaction(limecrew)
        await msg1.add_reaction(whitecrew)
def setup(bot):
    bot.add_cog(games(bot))

Sorry if i didn't word this right. Also this is a cog fyi

Upvotes: 1

Views: 3250

Answers (1)

Billy
Billy

Reputation: 1207

Resources

Procedure

  • Make a random choice, and decide the imposter
  • Add all reactions possible
  • Wait for user reaction
  • Do a check whether it's the correct user's response (the user who started the game). if not, then pass.
  • If yes, also check what emoji the user reacted with
  • Match the user's reaction with the imposter you can decide whether they are wrong or right

Sample Implementation

Here's a sample implementation

import discord
from discord.ext import commands
import random

def get_embed(_title, _description, _color):
    return discord.Embed(title=_title, description=_description, color=_color)

class games(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @commands.command()
    async def findimposter(self, ctx):
        """
        Impostors can sabotage the reactor, 
        which gives Crewmates 30–45 seconds to resolve the sabotage. 
        If it is not resolved in the allotted time, The Impostor(s) will win.
        """

        embed1 = discord.Embed(title = "Who's the imposter?" , description = "Find out who the imposter is, before the reactor breaks down!" , color=0xff0000)
        
        embed1.add_field(name = 'Red' , value= '<:redcrewmates:776867415514153031>' , inline=False)
        embed1.add_field(name = 'Blue' , value= '<:bluecrewmates:776867439085617153>' , inline=False)
        embed1.add_field(name = 'Lime' , value= '<:limecrewmates:776867489866711041>' , inline=False)
        embed1.add_field(name = 'White' , value= '<:whitecrewmates:776867529900425217>' , inline=False)
        
        msg = await ctx.send(embed=embed1)
        
        # imposter : emoji
        emojis = {
            'red': '<:redcrewmates:776867415514153031>',
            'blue': '<:bluecrewmates:776867439085617153>',
            'lime': '<:limecrewmates:776867489866711041>',
            'white': '<:whitecrewmates:776867529900425217>'
        }
        
        # pick the imposter
        imposter = random.choice(list(emojis.items()))
        imposter = imposter[0]
        
        # add all possible reactions
        for emoji in emojis.values():
            await msg.add_reaction(emoji)
        
        # check whether the correct user responded.
        # also check its a valid reaction.
        def check(reaction, user):
            self.reacted = reaction.emoji
            return user == ctx.author and str(reaction.emoji) in emojis.values()

        # waiting for the reaction to proceed
        try: 
            reaction, user = await self.bot.wait_for('reaction_add', timeout=30.0, check=check)
        
        except TimeoutError:
            # defeat, reactor meltdown
            description = "Reactor Meltdown.{0} was the imposter...".format(imposter)
            embed = get_embed("Defeat", description, discord.Color.red())
            await ctx.send(embed=embed)
        else:
            # victory, correct answer
            if str(self.reacted) == emojis[imposter]:
                description = "**{0}** was the imposter...".format(imposter)
                embed = get_embed("Victory", description, discord.Color.blue())
                await ctx.send(embed=embed)

            # defeat, wrong answer
            else:
                for key, value in emojis.items(): 
                    if value == str(self.reacted):
                        description = "**{0}** was not the imposter...".format(key)
                        embed = get_embed("Defeat", description, discord.Color.red())
                        await ctx.send(embed=embed)
                        break


def setup(bot):
    bot.add_cog(games(bot))

Upvotes: 1

Related Questions