pymongo.errors.ConfigurationError:DNS 标签为空

问题描述

我正在用 python 制作 discord bot。我正在使用数据库 (https://account.mongodb.com/) 制作调平系统,但出现以下语法错误:pymongo.errors.ConfigurationError: A DNS label is empty。请帮帮我。

main.py:

import os
import discord
import datetime
import levelsys

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='$',intents=intents)

cogs = [levelsys]
for i in range(len(cogs)):
  cogs[i].setup(bot)

 # Setting permissions that a user should have to execute this command.

print('Server Running')
bot.run(os.getenv('token'))

levelsys.py:

import discord
from discord.ext import commands
from pymongo import MongoClient
import urllib.parse

bot_channel = 798127930295058442
talk_channels = [690995360834912327]

level = ['new','old_new','Master Oogway']
levelnum = [5,15,30]

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='$',intents=intents)

cluster = MongoClient("mongodb+srv://Myself:" + urllib.parse.quote("stackoverflow") + "@cluster0.4h226.mongodb.n...")

leveling = cluster["discord"]["leveling"]

class levelsys(commands.Cog):
    def __init__(self,bot):
      self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
      print('ready!')

    @commands.Cog.listener()
    async def on_message(self,message):
      if message.channel.id in talk_channels:
        stats = leveling.find_one({"id": message.author.id})
        if not message.author.bot:
          if stats is None:
            newuser = {"id": message.auhor.id,"xp": 100}
            leveling.insert_one(newuser)
          else:
            xp = stats["xp"] + 5
            leveling.update_one({"id": message.author.id},{"set":{"xp":xp}})
            lvl = 0
            while True:
              if xp < ((50*(lvl**2))+(50*(lvl-1))):
                break
              lvl += 1
            xp -= ((50*(lvl**2))+(50*(lvl-1)))
            if xp == 0:
              await message.channel.send(f"Well done {message.author.mention}: you advanced to **level: {lvl}**!")
              for i in range(len(level)):
                if lvl == levelnum[1]:
                  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 = leveling.find_one({"id": ctx.author.id})
        if stats is None:
          embed = discord.Embed(description="You need to write more messages to get the 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 = leveling.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.add_field(name="Level Bar",value=Boxes*":blue_square:"+ (20-Boxes)* ":white_large_square:",inline=False)
          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 = leveling.find().sort("xp",-1)
        i=1
        embed = discord.Embed(title="Rankings:")
        for x in rankings:
          try:
            temp = ctx.quild.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(bot):
  bot.add_cog(levelsys(bot))

错误

pymongo.errors.ConfigurationError: A DNS label is empty.

请帮助我并告诉我是否还有其他错误需要修复。非常感谢!

解决方法

猜测您的问题是连接字符串; “SRV”模式下的 mongodb 使用 DNS 来确定副本集/分片的详细信息。