Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

python - discord py type a new command which only works after first one ist triggered

I want to create a bot to play blackjack and i managed to get the bot to give out a card if you type $blackjack. Now i want that after you type $blackjack you can type $hit or $stand right now it works if you write the command like $blackjacks $hit in one message but that's useless since you don't know what you wanna do before you know your card.

This is my code:

        elif message.content.startswith("$blackjack"):
        blackjack_ziehe_karte()
        ansage_der_karte = (message.author.name + " "+ "du hast die karte/n: "+ karte)
        await message.channel.send(ansage_der_karte)


            if "$hit" in message.content:
                blackjack_ziehe_karte()
                ansage_der_karte = ansage_der_karte + " " + ", " + karte
                await message.channel.send(ansage_der_karte)

            elif "$stand"  in message.content:
                await message.channel.send ( "test")
question from:https://stackoverflow.com/questions/65928813/discord-py-type-a-new-command-which-only-works-after-first-one-ist-triggered

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would need to store all the active games somewhere. This would probably work fine in memory, but you may choose to use a database for persistence and sharing across multiple services/instances.

This storage below is client.games which is a dictionary where the keys are the user's Discord ID and the value is an instance of Game.

It is recommended to use discord.ext.commands instead of using on_message, especially with more complex commands. There is an overview on the docs which goes in-depth into why you might want to use it and how to get started.

For example:

import discord

class Card:
    def __init__(self, suit: str, value: str) -> None:
        self.suit = suit
        self.value = value

class Game:
    def __init__(self) -> None:
        self.cards: list[Card] = []

    def hit(self) -> Card:
        # Return a random `Card` instance
        raise NotImplementedError()

    ...

# Assuming you're not using `commands.Bot` and therefore not a `commands.Cog`
client = discord.Client(...)
client.games = {}  # user_id: `Game`

...

@client.event
async def on_message(message):
    if "$blackjack" == message.content:
        game = Game()
        card = game.hit()
        # e.g. Send card and show existing cards, handle going bust
        ...
        client.games[message.author.id] = game

    if "$hit" == message.content:
        # You can use .get() to avoid raising if they have not already started a game
        game = client.games[message.author.id]
        card = game.hit()
        # e.g. Send card and show existing cards, handle going bust
        ...

    if "$stand" == message.content:
        ...
        # Once the game is over, remove it
        client.games.pop(message.author.id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...