问题描述
这个问题可能很复杂,我的大脑无法真正很好地解释它,所以请对此做出cr脚的解释,我的问题是,例如,当您触发命令时,启动它会开始,比如说一个基于文本的游戏。您将拥有可以实际玩游戏的命令,但是我担心的是,人们仍然可以触发游戏中的命令而无需启动游戏。例如
如果message.content.startswith( / play):#这是play命令,您可以在其中执行游戏以启动
等待client.send_message(message.channel,欢迎来到游戏!)
,如果message.content.startswith( / examine):
等待client.send_message(message.channel,您检查了一下岩石,很好,得到了一块岩石!)#In-游戏命令/动作
我的意思是,有一种方法只能使用
仅在激活游戏本身时才可以使用游戏中的命令吗?上:您将如何存储用户的信息,就像基本上保存游戏一样(您不需要真正回答这个,因为我想自己学习,但是任何提示都很棒!)
discord.User
s与 Game
s的映射。此映射中存在一个 User
表示他们正在玩游戏。一些基本内容如下所示: 来自discord.ext导入命令
类游戏:
def __init __(self):
self.points = 0
self.inventory = []
bot =命令。Bot('/')
会话= {}
@ bot.command(pass_context = True)
异步定义播放(ctx):
,如果会话中为ctx.message.author.id:
await bot.say(您已经在玩)
返回
次会话[ctx.message.author.id] = Game()
await bot.say(欢迎来到游戏!)
@ bot.command(pass_context = True)
异步定义退出(ctx):如果会话中没有ctx.message.author.id,则
:
await bot.say(您不玩游戏)
返回
个会话[ctx.message.author.id]
await bot.say(游戏结束)
@ bot.command(pass_context = True)
异步定义检查(ctx):
session = sessions.get(ctx.message.author.id,无)
,如果会话为无:
await bot.say(您不玩游戏)
return
session.inventory.append( A rock)
await bot.say( You检查了一下岩石,然后发现了一块岩石!)
bot.run( TOKEN)
您可以做一些事情来扩展此范围:利用 check
s和 CommandError
s避免重复用于检查会话的代码;确保游戏
是,并编写代码以使用酱菜保存游戏;编写比收集岩石更有趣的游戏。
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!)
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.User
s to Game
s. 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")
Some things you could do to extend this: make use of check
s and CommandError
s to avoid having to repeat the code for checking sessions; make sure that Game
s are pickleable, and write code for saving games using pickle; write a game that's more fun than collecting rocks.
这篇关于仅在触发当前命令时如何使用命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!