如何制作随机反应游戏(discord.py)

Posted

技术标签:

【中文标题】如何制作随机反应游戏(discord.py)【英文标题】:How to make a random reaction game (discord.py) 【发布时间】:2021-02-28 19:53:24 【问题描述】:

如何制作我的代码,以便它选择 1/4 可能的答案作为答案,并且当不和谐用户对正确答案或错误答案做出反应时,它会返回获胜或失败的答案。 我也希望每次的答案都是随机的。


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

对不起,如果我没有说对的话。 这也是一个齿轮供参考

【问题讨论】:

【参考方案1】:

资源

discord.Client.wait_for - 等待用户反应 random.choice - 随机挑选冒名顶替者

程序

随机选择,确定冒名顶替者 添加所有可能的反应 等待用户反应 检查是否是正确的用户响应(启动游戏的用户)。如果没有,则通过。 如果是,还要检查用户对什么表情符号的反应 将用户的反应与冒名顶替者匹配,您可以决定他们是错是对

示例实现

这是一个示例实现

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

【讨论】:

以上是关于如何制作随机反应游戏(discord.py)的主要内容,如果未能解决你的问题,请参考以下文章

(discord.py) 向不和谐机器人发布的嵌入添加反应

如何改进反垃圾邮件功能 discord.py?

Discord.py禁止命令

Discord bot 添加对消息 discord.py 的反应(无自定义表情符号)

如何使用 discord.py 制作一个检查某个服务器通道中所有消息的机器人

Discord.py:根据消息得到的反应来编辑机器人发送的消息