首页 > 解决方案 > 未调用 Python 构造函数,错误

问题描述

目前正在使用 Discord Bot 来练习我的技能。

为我的 Casino Cog 开发 Blackjack 系统,我想我只是缺少一些关于该语言的东西,因为我还不精通 Python。

我的意图是让 Casino Cog 接受用户的命令来开始二十一点游戏,然后它会创建一个游戏并将其添加到它自己的正在进行的二十一点游戏列表中。出于某种原因,构造函数/init 在我的命令处理程序中出错

有谁知道这可能发生的原因?我不允许在异步方法中同步创建对象吗?我是否必须使用 asyncio 将其包装在等待中...我很迷茫

@commands.command(name='blackjack')
  async def bj(self, pCtx, amount:int):
    if amount < 0:
      await pCtx.send("Can't bet a negative amount")
      return
    ID = pCtx.message.author.id
    user = await self.bot.db.koomdata.find_one({'_uid':int(ID)})
    if user['_currency'] < amount:
      await pCtx.send("You don't have enough money to gamble")
      return
    print('pre')
    game = cogs.blackjack.BlackjackGame(self.bot, pCtx.message, int(amount))
    print('post')
    self.bjSessions.append(game)

我们从来没有到达上一行中的“post”打印,我已经尝试在 VS Code 中设置断点并且程序只是停在该行,所以我假设我传入的数据有一些错误。但就目前而言我可以告诉init函数接受所有这些参数

def __init__(self, bot, message, amount:int):
  self.bot = bot
  self.message = message
  self.amount = amount
  self.player = message.author.id
  self.playerhand = []
  self.dealerhand =  [] 
  self.dealerStopped = False
    

编辑:文件结构:

secrets.py
bot.py
-> cogs
    -> casino.py
    -> blackjack.py

错误:模块“cogs”没有属性“blackjack”

进一步编辑:python 不允许我从其他目录导入 python 文件吗?当我尝试导入某些东西时,'cogs.blackjack' 不起作用吗?因为如果我必须把一切都放在首位,那真的很难过

感谢您的任何帮助,

标签: pythondiscord

解决方案


  async def bj(self, pCtx, amount:int):
if amount < 0:
  await pCtx.send("Can't bet a negative amount")
  return
ID = pCtx.message.author.id
user = await self.bot.db.koomdata.find_one({'_uid':int(ID)})
if user['_currency'] < amount:
  await pCtx.send("You don't have enough money to gamble")
  return
game = cogs.blackjack.BlackjackGame(self.bot, pCtx.message, int(amount))
self.bjSessions.append(game)


  def __init__(self, bot, message, amount:int):
self.bot = bot
self.message = message
self.amount = amount
self.player = message.author.id
self.playerhand = []
self.dealerhand =  [] 
self.dealerStopped = False



def deal(self):
self.playerhand = []
self.dealerhand = []
self.playerhand.append(random.choice(cards))
self.dealerhand.append(random.choice(cards))
self.playerhand.append(random.choice(cards))
self.dealerhand.append(random.choice(cards))
self.playerhand.append(random.choice(cards))

推荐阅读