python - 未调用 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' 不起作用吗?因为如果我必须把一切都放在首位,那真的很难过
感谢您的任何帮助,
解决方案
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))
推荐阅读
- binary-search-tree - 如果在 BST 中获取中序后继需要 O(h),为什么在调用 O(h) 函数 n 次时迭代中序遍历需要 O(n)?
- discord.py - Discord Bot 崩溃:typeerror __new__() 有一个意外的关键字参数“deny_new”
- windows - Visual Basic 安装工具包的安装引导程序 - EXE - 静默安装 (VB6)
- reactjs - How to call Azure AD Secured Azure Functions from ReactJS Application
- vb.net - 将带参数的下载完成事件传递给另一个类
- javascript - 如何通过 React-Native 中的 React-Navigation 多次传递参数?
- javascript - 指定原始窗口大小时,Window.open 不起作用
- python - 使用多处理创建两个 pygame 屏幕
- python - 我如何从目录中选择随机图像?Python
- sqlite - 使用 sqlite 批量插入和返回 ID