如何在python3.6.4中为discord bot创建商店?

2024-04-23 18:07:13 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我为一个叫“战场”的机器人创建了货币,简称bp。问题是,我想创建一个商店来购买商品(不必存储),到目前为止,我已经知道了这一点,但不知道如何将其转换为一个命令,如buy name。你知道吗

items = {
    "Game1":[3,20],
    "Game2":[5,30],
    "game3":[5,20],
    "game4":[8,10],
    }
while True:
    print("game1 = 3BP / game2 = 5BP / game3 = 5BP / game4 = 8BP")
    print("Account Balance bp",stash)
    choice = input("What would you like to buy?: ").strip().title()
    if choice in items:
        if items[choice][1]>0:
            if stash>=items[choice][0]:
                items[choice][1]=items[choice][1]-1
                stash= stash-items[choice][0]
                print("Thank you..!")
                print("")
            else:
                print("Sorry you dont enough money...")
                print("")
        else:
            print("sorry sold out")
            print("")
    else:
        print("Sorry we dont have that item...")
        print("")

我试着加入client.commmand() async def buy():然后是代码,但我似乎无法工作。 你能不能给我指出正确的方向,告诉我在哪里能找到答案,或者你能不能告诉我我做错了什么


Tags: youif机器人itemsbuyelseprintdont
1条回答
网友
1楼 · 发布于 2024-04-23 18:07:13

我想问题是choice = input("What would you like to buy?: ").strip().title(),因为bot不会通过input()函数接收输入。不过,通常可以将消息的上下文传递给函数。我在你的完整代码中看到:

@commands.has_role('Moderator')
@client.command(pass_context = True)
async def clear(ctx, amount=100):

在这里我会注意到pass_context = True。这允许函数读取不一致消息的上下文和内容。例如,您可以调用ctx.message.author来获取被解释消息的作者。所以如果说“Bob Joe”类型“!清除50“,然后:

ctx.message.author == "Bob Joe"

这也应该将amount从100改为>50。使用类似的技术,你可以有一个用户“!买金鞋”会触发你的async def buy(ctx, item):案例:

item == "GoldShoes"

然后由您的逻辑来决定如何处理这些输入,但也许这有助于解释这个过程。如果我不清楚什么,请告诉我。你知道吗

相关问题 更多 >