Discord.py bot 不会响应命令......除了它是一个调平系统

时间:2021-03-01 15:48:09

标签: python discord discord.py

编辑:我可能应该补充一点,我在 repl.it 上执行了以下所有代码,尽管我怀疑这有什么不同。

编辑 2:我不知道我做了什么,但现在绝对没有任何效果。 “就绪”消息不会显示在控制台中。这可能与我添加的“keep_alive”文件有关。

所以我每天都在努力找出这段代码的问题所在,直到我意识到“嘿,我可以尝试询问 Stack Overflow”!

就上下文而言,每当我运行机器人并输入命令(即“!rank”)时,机器人似乎都无法识别该命令。除非我使用不存在的命令,否则控制台中没有红色字样。 This is the console after I used three commands: '!rank', '!leaderboard', and '!yeet', which purposefully doesn't exist.

如果你懒得打开图片,控制台会显示:

var job: Job? = null


job?.cancel()
job = coroutineScope.launch { aSuspendFunction() }


代码如下:

Ignoring exception in command None:

discord.ext.commands.errors.CommandNotFound: Command "yeet" is not found
from keep_alive import keep_alive

from discord.ext import commands
import discord
import os
import levelsys

cogs = [levelsys]

client = commands.Bot(command_prefix="!", intents = discord.Intents.all())

token = os.environ.get("DISCORD_BOT_SECRET")
client.run(token)

keep_alive()
token = os.environ.get("DISCORD_BOT_SECRET")
client.run(token)

for i in range(len(cogs)):
  cogs[i].setup(client)

(这是我添加的 keep_alive 文件,不知何故搞砸了一切)

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

general = [806613968684318720]

level = ["Sperm Whale", "Artillery General", "Commander", "Supreme General",]
levelnum = [1,5,10,15]

cluster = MongoClient("mongodb+srv://[Username]:[Password]@kingdom-kun.lffd9.mongodb.net/Kingdom-kun?retryWrites=true&w=majority")

client = discord.Client

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

class levelsys(commands.Cog):
  def _init_(self, client):
    self.client = client

  @commands.Cog.listener()
  async def on_ready(self):
    print("Kingdom-kun is ready to come!")

  @commands.Cog.listener()
  async def on_message(self, message):
    if message.channel == general:
      stats = leveling.find_one({"id" : message.author.id})
      if not message.author.bot:
        if stats is None:
          newuser = {"id": message.author.id, "xp": 100}
          leveling.insert_one(newuser)
        else:
          xp = stats["xp"] +5
          leveling.update_one({"id": message.author.id}, {"$set": {"xp"}})
          lvl = 0
          while True:
            if xp < {(50*(lvl**2))+(50*(lvl-1))}:
              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 were promoted 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)

#This is rank command
  @commands.command()
  async def rank(self, ctx):
    if ctx.channel.id == general:
      stats = leveling.find_one({"id": ctx.author.id})
      if stats is None:
        embed = discord.Embed(description = "You haven't sent any messages, no rank!!!")
        await ctx.channelsned(embed=embed)
      else:
        xp = stats["xp"]
        lvl = 0
        rank = 0
        while True:
          if xp < {(50*(lvl**2))+(50*(lvl-1))}:
              break
          lvl += 1
        xp -= {(50*(lvl-1)**2)+(50*(lvl-1))}
        boxes = [(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/(200*((1/2)* lvl)))*20}", inline=True)

        embed.add_field(name="Rank", value = f"{rank}/{ctx.guild.member_count}", inline=True)

        embed.add_field(name="Progress Bar[lvl]", 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)

#This is leaderboard command
  @commands.command()
  async def leaderboard(self, ctx):
    if (ctx.channel.id == general):
      rankings = leveling.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))

我尝试了很多方法 - 将括号更改为括号,然后再次返回,重新输入整个内容......再次等等,但没有任何改变。如果有人能回答这个难题,我将不胜感激。

无论如何,祝你有美好的一天!

1 个答案:

答案 0 :(得分:0)

在您的 on_message 活动中,您从未调用过 await bot.process_commands(message)

看看 discord.py documentation