【问题标题】:I need help! I am making a discord bot in python and my mini text-based game wont work我需要帮助!我正在用 python 制作一个不和谐的机器人,我的基于文本的迷你游戏无法运行
【发布时间】:2023-02-23 01:29:37
【问题描述】:

我的问题是我目前正在学习如何制作一个不和谐的机器人,但我制作我的方式与那里的一些例子不同,所以很难找到帮助。我正在尝试添加一个有趣的基于文本的二十一点游戏。我正在尝试将我的旧二十一点转换为不和谐机器人功能。但是,我目前一直在尝试让它等待用户说“坚持”或“扭曲”。它只是在没有它们的情况下运行游戏。

我的代码中还有其他功能,例如反应之类的功能,但我将它们排除在外,因为我认为它们对这个问题并不重要。我曾尝试在我的 21 点游戏中重用 @client 事件,但似乎没有用。

import discord
import os
import requests
import json
import random
import time

intents = discord.Intents().all()
client = discord.Client(intents=intents);
@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
    if message.author == client.user:
      return
    if message.content.startswith("$blackjack"):
      def yourcards(cards, a):
          if a == 1:
              cards.append("an ace")
              return "you got an ace"
          elif a == 2:
              cards.append("a two")
              return "you got a two"
          elif a == 3:
              cards.append("a three")
              return "you got a three"
          elif a == 4:
              cards.append("a four")
              return "you got a four"
          elif a == 5:
              cards.append("a five")
              return "you got a five"
          elif a == 6:
              cards.append("a six")
              return "you got a six"
          elif a == 7:
              cards.append("a seven")
              return "you got a seven"
          elif a == 8:
              cards.append("an eight")
              return "you got a eight"
          elif a == 9:
              cards.append("a nine")
              return "you got a nine"
          elif a == 10:
              cards.append("a ten")
              return "you got a ten"
          elif a == 11:
              cards.append("a jack")
              return "you got a jack"
          elif a == 12:
              cards.append("a queen")
              return "you got a queen"
          elif a == 13:
              cards.append("a king")
              return "you got a king"
          else:
              print("error")
      def theircards(compcards, a):
          if a == 1:
              compcards.append("an ace")
              return "they got an ace"
          elif a == 2:
              compcards.append("a two")
              return "They got a two"
          elif a == 3:
              compcards.append("a three")
              return "They got a three"
          elif a == 4:
              compcards.append("a four")
              return "They got a four"
          elif a == 5:
              compcards.append("a five")
              return "They got a five"
          elif a == 6:
              compcards.append("a six")
              return "They got a six"
          elif a == 7:
              compcards.append("a seven")
              return "They got a seven"
          elif a == 8:
              compcards.append("an eight")
              return "They got a eight"
          elif a == 9:
              compcards.append("a nine")
              return "They got a nine"
          elif a == 10:
              compcards.append("a ten")
              return "They got a ten"
          elif a == 11:
              compcards.append("a jack")
              return "They got a jack"
          elif a == 12:
              compcards.append("a queen")
              return "They got a queen"
          elif a == 13:
              compcards.append("a king")
              return "They got a king"
          
  
      
      #start
      
              
      money = 500
      await message.channel.send("_____BlackJack!_____")
      time.sleep(0.5)
      #main code
      choice = ""
      tot = 0
      cards=[]
      compcards=[]
      card = random.randint(1,11)
      await message.channel.send((yourcards(cards, card)))
      if card > 10:
          card = 10
      tot += card
      time.sleep(0.75)
      while choice != "1":
          card = random.randint(1,11)
          await message.channel.send(yourcards(cards, card))
          if card > 10:
              card = 10
          time.sleep(1)
          tot += card
          currentmessage ="your current total is: "+ str(tot)
          await message.channel.send(currentmessage)
          if tot> 21:
              await message.channel.send("You've gone bust")
              choice = "1"
              time.sleep(2)
          if tot == 21:
              await message.channel.send("well done!")
              choice = "1"
          elif choice == "3":
              choice = "1"
              await message.channel.send("you have:")
              time.sleep(0.7)
              await message.channel.send(', '.join(cards))
          else:
              await message.channel.send("you have:")
              time.sleep(0.7)
              await message.channel.send(', '.join(cards))
              @client.event
              async def on_message(message):
                if message.content == "2":
                  choice = 2
              time.sleep(1)

              
      #dealer

              
      comptot = 0
      card = random.randint(1,11)
      curmsg =theircards(compcards, card)
      await message.channel.send(curmsg)
      if card > 10:
          card = 10
      comptot += card
      time.sleep(1)
      while comptot <=15:
          card = random.randint(1,11)
          if card > 10:
              card = 10
          curmsg =theircards(compcards, card)
          await message.channel.send(curmsg)
          time.sleep(1)
          comptot += card
          curmsg="their total is: "+ str(comptot)
          await message.channel.send(curmsg)
          time.sleep(1)

          
      #win conditions

          
      if tot >21:
          await message.channel.send("dealer automatically wins as you went bust")
          
      elif comptot >21:
          await message.channel.send("you automatically win as dealer went bust")
              
      elif tot > comptot:
          await message.channel.send("you win")
              
      elif comptot > tot:
          await message.channel.send("you loose")
          
      elif tot == comptot:
          await message.channel.send("draw")






      
client.run(os.environ['TOKEN'])

【问题讨论】:

  • “行不通”是什么意思?当您运行您的代码时会发生什么,您期望会发生什么?有什么错误吗?参见How to Ask

标签: python discord bots


【解决方案1】:

好吧,乍一看,您的 on_message 事件似乎嵌套在您的 on_ready 事件中,这可能会导致问题。您应该只有一个 on_message 事件,并且应该在 on_ready 事件之外定义它:

import discord
import os
import requests
import json
import random
import time

intents = discord.Intents().all()
client = discord.Client(intents=intents)

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith("$blackjack"):
    # rest of your code...

希望有帮助!?

【讨论】:

  • 此外,您提到它不是在等待用户说“坚持”或“扭曲”。但我看不到你在任何地方检查这个?确保您在某处使用 if 语句来检查用户是否说了“stick”或“twist”?
  • 糟糕的是,在复制代码的重要部分时,我不小心将 on 消息缩进了错误。实际代码没有缩进。棍棒或扭曲的东西被意外移除。它最初是“1:stick 2:扭曲 >>>" 然后是输入,这就是为什么它使用 1 和 2 而不是坚持和扭曲。
  • 您能否编辑您的问题,以便代码是您所拥有的,请让事情变得更容易?另外,为了澄清一些事情,当您运行它时当前正在发生什么? “它只是在没有它们的情况下运行游戏”是什么意思?
猜你喜欢
  • 2013-09-30
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多