这就是我的齿轮文件 levelsys.py
import discord
from discord.ext import commands
from pymongo import MongoClient
general = [825768173730660404]
bot_channel = 825871367575830548
level = ["Level 1", "Level 2", "Level 3"]
levelnum = [5, 10, 15]
cluster = MongoClient(
"mongodb+srv://my username:<my password>@bot.orejh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
levelling = cluster["discord"], ["levelling"]
class levelsys(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_message(self, message):
if message.channel.id in general:
stats = levelling.find_one({"id": message.author.id})
if not message.author.bot:
if stats is None:
newuser = {"id": message.author.id, "xp": 100}
levelling.insert_one(newuser)
else:
xp = stats["xp"] + 5
levelling.update_one({"id": message.author.id}, {"$set": {"xp": xp}})
lvl = 0
while True:
if xp < ((50 * (lvl ** 2)) + (50 * lvl)):
break
lvl += 1
xp -= ((50 * ((lvl - 1) ** 2)) + (50 * (lvl - 1)))
if xp == 0:
await message.channel.send(
f"well done {message.author.mention}! You leveled up to **level: {lvl}**!")
for i in range(len(level)):
if lvl == levelnum[i]:
await message.author.add_roles(
discord.utils.get(message.author.guild.roles, name=level[i]))
embed = discord.Embed(
description=f"{message.author.mention} you have gotten role **{level[i]}**!!!")
embed.set_thumbnail(url=message.author.avatar_url)
await message.channel.send(embed=embed)
@commands.command()
async def rank(self, ctx):
if ctx.channel.id == bot_channel:
stats = levelling.find_one({"id": ctx.author.id})
if stats is None:
embed = discord.Embed(description="You haven't sent any messages, no rank!!!")
await ctx.channel.send(embed=embed)
else:
xp = stats["xp"]
lvl = 0
rank = 0
while True:
if xp < ((50 * (lvl ** 2)) + (50 * lvl)):
break
lvl += 1
xp -= ((50 * ((lvl - 1) ** 2)) + (50 * (lvl - 1)))
boxes = int((xp / (200 * ((1 / 2) * lvl))) * 20)
rankings = levelling.find().sort("xp", -1)
for x in rankings:
rank += 1
if stats["id"] == x["id"]:
break
embed = discord.Embed(title="{}'s level stats".format(ctx.author.name))
embed.add_field(name="Name", value=ctx.author.mention, inline=True)
embed.add_field(name="XP", value=f"{xp}/{int(200 * ((1 / 2) * lvl))}", inline=True)
embed.add_field(name="Rank", value=f"{rank}/{ctx.guild.member_count}", inline=True)
embed.set_thumbnail(url=ctx.author.avatar_url)
await ctx.channel.send(embed=embed)
@commands.command()
async def leaderboard(self, ctx):
if (ctx.channel.id == bot_channel):
rankings = levelling.find().sort("xp", -1)
i = 1
embed = discord.Embed(title="Rankings:")
for x in rankings:
try:
temp = ctx.guild.get_member(x["id"])
tempxp = x["xp"]
embed.add_field(name=f"{i}: {temp.name}", value=f"Total XP: {tempxp}", inline=False)
i += 1
except:
pass
if i == 11:
break
await ctx.channel.send(embed=embed)
def setup(client):
client.add_cog(levelsys(client))
错误信息:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Kacper\.virtualenvs\Nocne_Farfocle\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "D:\Programming\Python\Nocne_Farfocle\levelsys.py", line 25, in on_message
stats = levelling.find_one({"id": message.author.id})
AttributeError: 'tuple' object has no attribute 'find_one'
我使用的是 python 3.9.1、discord 1.0.1、discord.py 1.6.0、dnspython 2.1.0 和 pymongo 3.11.3 我正在尝试制作一个自定义的 Discord 机器人,这是该机器人的模块之一,我现在被这个错误困住了大约 3 天,所以我真的很想从你们那里得到任何提示:D
答案 0 :(得分:0)
我认为答案是 leveling
是您分配了 2 个值的一个变量。这使它成为一个元组。因此,当您尝试在元组上运行 find_one
时,它不能,因为它不是您想要的对象。
旁注:如果你创建一个方法 return value1, value2
,也会发生类似的事情。在 python 中,这被认为是一个元组。