<bdo id='XS0sm'></bdo><ul id='XS0sm'></ul>

    1. <i id='XS0sm'><tr id='XS0sm'><dt id='XS0sm'><q id='XS0sm'><span id='XS0sm'><b id='XS0sm'><form id='XS0sm'><ins id='XS0sm'></ins><ul id='XS0sm'></ul><sub id='XS0sm'></sub></form><legend id='XS0sm'></legend><bdo id='XS0sm'><pre id='XS0sm'><center id='XS0sm'></center></pre></bdo></b><th id='XS0sm'></th></span></q></dt></tr></i><div id='XS0sm'><tfoot id='XS0sm'></tfoot><dl id='XS0sm'><fieldset id='XS0sm'></fieldset></dl></div>
      <tfoot id='XS0sm'></tfoot>

      <small id='XS0sm'></small><noframes id='XS0sm'>

    2. <legend id='XS0sm'><style id='XS0sm'><dir id='XS0sm'><q id='XS0sm'></q></dir></style></legend>

      如何仅在触发当前命令时使用命令?

      时间:2023-10-10

        <legend id='eQGmy'><style id='eQGmy'><dir id='eQGmy'><q id='eQGmy'></q></dir></style></legend>
          <tbody id='eQGmy'></tbody>
        <i id='eQGmy'><tr id='eQGmy'><dt id='eQGmy'><q id='eQGmy'><span id='eQGmy'><b id='eQGmy'><form id='eQGmy'><ins id='eQGmy'></ins><ul id='eQGmy'></ul><sub id='eQGmy'></sub></form><legend id='eQGmy'></legend><bdo id='eQGmy'><pre id='eQGmy'><center id='eQGmy'></center></pre></bdo></b><th id='eQGmy'></th></span></q></dt></tr></i><div id='eQGmy'><tfoot id='eQGmy'></tfoot><dl id='eQGmy'><fieldset id='eQGmy'></fieldset></dl></div>

        • <tfoot id='eQGmy'></tfoot>
            <bdo id='eQGmy'></bdo><ul id='eQGmy'></ul>

            <small id='eQGmy'></small><noframes id='eQGmy'>

                本文介绍了如何仅在触发当前命令时使用命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这个问题可能很复杂,我的大脑无法很好地解释它,所以请用这个蹩脚的解释来解释,我的问题,当你触发一个命令时,例如 .start 它将开始让我们说一个基于文本的游戏,当然您将拥有能够实际玩游戏的命令,但我担心人们仍然可以触发游戏内命令而无需启动游戏.

                This question might be complicated and my brain can't really explain it well so please bare with this crappy explanation, My question, When you trigger a command for example .start it will start let's say a text based game, of course you would have the commands to be able to actually play the game however my concern is people can still trigger the ingame commands without needing to start the game for example .

                     if message.content.startswith("/play"):       #Here is the play command where you execute the game to start
                         await client.send_message(message.channel, "Welcome to the game!")
                     if message.content.startswith("/examine):
                         await client.send_message(message.channel, "You examined the rock and well, got a rock!") #In-Game commands/movements
                

                我的意思是,有没有办法只有在游戏本身被激活时才能使用游戏中的命令?附加问题:您将如何存储用户的信息,例如基本上保存游戏(您实际上不需要回答这个问题,因为我想自己学习,但任何提示都会很棒!)

                What i'm saying is, is there a way of only being able to use the in-game commands only when the game itself is activated? Additional Question: How would you store a user's information like basically saving the game (You don't really need to answer this as i would like to learn this myself but any tips would be great!)

                推荐答案

                首先,我们需要一些对象来存储特定会话的状态.我们可以把这个对象称为Game.我们将维护 discord.Users 到 Games 的映射.此映射中存在的 User 表示他们正在玩游戏.一些基础知识类似于:

                First, we want some object that stores the state of a particular session. We can just call this object Game. We'll maintain a mapping of discord.Users to Games. A User existing in this mapping means that they are playing the game. Some basics would look something like:

                from discord.ext import commands
                
                class Game:
                    def __init__(self):
                        self.points = 0
                        self.inventory = []
                
                bot = commands.Bot('/')
                
                sessions = {}
                
                @bot.command(pass_context=True)
                async def play(ctx):
                    if ctx.message.author.id in sessions:
                        await bot.say("You're already playing")
                        return
                    sessions[ctx.message.author.id] = Game()
                    await bot.say("Welcome to the game!")
                
                @bot.command(pass_context=True)
                async def quit(ctx):
                    if ctx.message.author.id not in sessions:
                        await bot.say("You're not playing the game")
                        return
                    del sessions[ctx.message.author.id]
                    await bot.say("Game Over")
                
                @bot.command(pass_context=True)
                async def examine(ctx):
                    session = sessions.get(ctx.message.author.id, None)
                    if session is None:
                        await bot.say("You're not playing the game")
                        return
                    session.inventory.append("A rock")
                    await bot.say("You examined the rock and well, got a rock!")
                
                bot.run("TOKEN")
                

                您可以做一些事情来扩展它:利用 checks 和 CommandErrors 来避免重复检查会话的代码;确保 Game 是 pickleable,并编写使用pickle保存游戏的代码;写一个比收集石头更有趣的游戏.

                Some things you could do to extend this: make use of checks and CommandErrors to avoid having to repeat the code for checking sessions; make sure that Games are pickleable, and write code for saving games using pickle; write a game that's more fun than collecting rocks.

                这篇关于如何仅在触发当前命令时使用命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Discord.py 如何发出静音命令? 下一篇:如何正确解析我的 Discord 机器人中的标记用户?

                相关文章

                最新文章

                  <tfoot id='VKOQp'></tfoot>

                    <legend id='VKOQp'><style id='VKOQp'><dir id='VKOQp'><q id='VKOQp'></q></dir></style></legend>

                    <small id='VKOQp'></small><noframes id='VKOQp'>

                  1. <i id='VKOQp'><tr id='VKOQp'><dt id='VKOQp'><q id='VKOQp'><span id='VKOQp'><b id='VKOQp'><form id='VKOQp'><ins id='VKOQp'></ins><ul id='VKOQp'></ul><sub id='VKOQp'></sub></form><legend id='VKOQp'></legend><bdo id='VKOQp'><pre id='VKOQp'><center id='VKOQp'></center></pre></bdo></b><th id='VKOQp'></th></span></q></dt></tr></i><div id='VKOQp'><tfoot id='VKOQp'></tfoot><dl id='VKOQp'><fieldset id='VKOQp'></fieldset></dl></div>
                      <bdo id='VKOQp'></bdo><ul id='VKOQp'></ul>