简体   繁体   中英

Error message while trying to make discord.py level bot

thats my cog file 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))

error message:

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'

I'm using python 3.9.1, discord 1.0.1, discord.py 1.6.0, dnspython 2.1.0 and pymongo 3.11.3 I'm trying to make a custom discord bot, and that's one of modules for this bot, i'm stuck wit this error for like 3 days now, soo i would really like to get any tips from you guys:D

I think the answer is that leveling is one variable to which you assigned 2 values. This makes it into a tuple. therefore, when you try to run find_one on the tuple, it can't because it isn't the object you wanted it to be.

sidenote: a similar thing happens if you make a method return value1, value2 . In python, that is considered a tuple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM