【发布时间】:2021-10-24 22:44:40
【问题描述】:
我有一个问题,如何使用 Pymongo 编写角色和其他项目的存储,我尝试这样做:
@Bot.event
async def on_ready():
await Bot.change_presence(status = discord.Status.online, activity = discord.Streaming(name = '!help', url = ''))
print(Bot.user.name + " is ready.")
for guild in Bot.guilds:
for member in guild.members:
post = {
"_id": member.id,
"server_id": guild.id,
"balance": 300,
"xp": 0,
"lvl": 1
},
{
"role": 0,
"cost": 0
}
if collection.count_documents({"_id": member.id}) == 0:
collection.insert_one(post)
和
@commands.command()
async def add_shop(self, ctx, role: discord.Role, cost: int):
self.collection.update_one({"_id": ctx.author.id},
{"$set": {"role": role}})
self.collection.update_one({"_id": ctx.author.id},
{"$set": {"cost": cost}})
await ctx.message.add_reaction("✅")
@commands.command()
async def shop(self, ctx):
r = self.collection.find_one({"_id": ctx.author.id}["role"])
c = self.collection.find_one({"_id": ctx.author.id}["cost"])
await ctx.send(r, c)
但这对我来说并不奏效。我决定联系你。提前谢谢!
【问题讨论】:
-
当你说“它没有成功”时,你得到了什么错误?
-
命令引发异常:InvalidDocument:无法编码对象:
,类型: 并且在输入第二个命令时:discord.ext.commands.errors.CommandInvokeError:命令引发异常:KeyError:'role' -
@Den 第一个不言自明,您不能在 MongoDB 中放置
discord.Role,因为它不知道如何处理它。改为存储角色的ID(add_shop命令的第二行)。 -
我想获取角色ID,用于使用命令添加角色。
标签: python mongodb discord.py pymongo