尝试制作 discord.py 级别机器人时出现错误消息
Posted
技术标签:
【中文标题】尝试制作 discord.py 级别机器人时出现错误消息【英文标题】:Error message while trying to make discord.py level bot 【发布时间】:2021-06-25 01:57:35 【问题描述】:这就是我的 cog 文件 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 我正在尝试制作一个自定义的不和谐机器人,这是该机器人的模块之一,我被这个错误困扰了 3 天,所以我真的很想从你们那里得到任何提示:D
【问题讨论】:
【参考方案1】:我认为答案是 leveling
是一个变量,您为其分配了 2 个值。这使它成为一个元组。因此,当您尝试在元组上运行 find_one
时,它不能,因为它不是您想要的对象。
旁注:如果你创建一个方法return value1, value2
,也会发生类似的事情。在 python 中,这被认为是一个元组。
【讨论】:
这里我有另一个问题,因为当变量看起来像这样时: leveling = cluster["discord"]["levelling"] 我有另一个错误,但这次连接到 mongodb @Tapicz 我不明白 MongoClient 的东西,所以我不知道cluster
是什么类型的字典,听起来你的密钥不存在于数据库中或其他东西,我也不知道你的错误是什么。我希望这个网站上有另一个帖子可以帮助你。以上是关于尝试制作 discord.py 级别机器人时出现错误消息的主要内容,如果未能解决你的问题,请参考以下文章