Python 3.x Discord.py Python经济机器人与mongodb“;“非类型”;错误
(糟糕的英语) 我想用mongodb创建一个经济型机器人,它在两天前运行良好,但现在我出现了“非类型”错误 main.py中的Sorce代码(用于在数据库中添加用户): 这段代码正在运行,因为我在db上看到了这一点 cog的源代码有问题:Python 3.x Discord.py Python经济机器人与mongodb“;“非类型”;错误,python-3.x,mongodb,discord.py,pymongo,Python 3.x,Mongodb,Discord.py,Pymongo,(糟糕的英语) 我想用mongodb创建一个经济型机器人,它在两天前运行良好,但现在我出现了“非类型”错误 main.py中的Sorce代码(用于在数据库中添加用户): 这段代码正在运行,因为我在db上看到了这一点 cog的源代码有问题: class Economic(commands.Cog): def __init__(self, client): self.client = client self.cluster = MongoClient(f&q
class Economic(commands.Cog):
def __init__(self, client):
self.client = client
self.cluster = MongoClient(f"mongodb+srv://discord:{secret}@cluster403.xn9cm.mongodb.net/discord1?retryWrites=true&w=majority")
cluster = MongoClient(f"mongodb+srv://discord:{secret}@cluster403.xn9cm.mongodb.net/users?retryWrites=true&w=majority")
self.collection = cluster.users.tutu
@commands.command(
name = "balance",
aliases = ["ball","money"],
brief = "User balance",
usage = "balance <@user>",
description = "none....."
)
async def user_balace(self, ctx, member: discord.Member = None):
if member is None:
embed = discord.Embed(
title = f"__{ctx.author}__'s balance",
description = f"Money: {self.collection.find_one({'_id': ctx.author.id})['balance']}"
)
await ctx.send(embed=embed)
有人能帮我吗?(抱歉我的英语不好)错误消息实质上是说
self.collection.find\u one({u id:ctx.author.id})
返回为None
。在中,我们可以看到find_one()
将在集合中找不到此类项目时返回None
。我会重新编写您的代码,以便在数据库中没有特定用户的情况下具有安全性
if self.collection.find({'_id': ctx.author.id}).count() == 0:
self.collection.insert_one({USER OBJECT})
user = self.collection.find_one({'_id': ctx.author.id})
Ignoring exception in command balance:
Traceback (most recent call last):
File "D:\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Nick403\Desktop\Apps\PROJECTS\BOT\cogs\eco.py", line 22, in user_balace
description = f"Money: {self.collection.find_one({'_id': ctx.author.id})['balance']}"
TypeError: 'NoneType' object is not subscriptable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "D:\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "D:\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
if self.collection.find({'_id': ctx.author.id}).count() == 0:
self.collection.insert_one({USER OBJECT})
user = self.collection.find_one({'_id': ctx.author.id})