首页 > 解决方案 > discord.ext.commands.errors.MissingRequiredArgument:bot 是缺少的必需参数

问题描述

主文件

import os
import discord
from discord.ext import commands, tasks

bot = commands.Bot(command_prefix=commands.when_mentioned_or('moon '),
                   case_insensitive=True)

my_secret = os.environ['token']
for file in os.listdir("./cogs"):
    if file.endswith(".py"):
        name = file[:-3]
        bot.load_extension(f"cogs.{name}")

for utility in os.listdir("./cogs/utility"):
    if utility.endswith(".py"):
        name = utility[:-3]
        bot.load_extension(f"cogs.utility.{name}")


@bot.event
async def on_ready():
    print('  {} está online!'.format(bot.user.name))


bot.run(my_secret)

平.py

import discord
from discord.ext import commands

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

    @commands.command()
    async def ping(self, ctx, bot):
        await ctx.send('A Lua está com {}ms'.format(bot.latency * 100))

def setup(bot):
    bot.add_cog(Utility(bot))

当我使用 ping 命令时,我收到此错误:

discord.ext.commands.errors.MissingRequiredArgument: bot is a required argument that is missing.

标签: pythondiscord.py

解决方案


ping.py 中有 3 个错误:

  1. ping 命令中不需要参数。
  2. 它应该是 (self.bot.latency) 而不是 (bot.latency)
  3. 要在 ping 命令中将秒转换为微秒,请将 (self.bot.latency * 100) 替换为 (self.bot.latency * 1000)

这个 ping.py 程序应该可以工作:

import discord
from discord.ext import commands

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

    @commands.command()
    async def ping(self, ctx):
        await ctx.send('A Lua está com {}ms'.format(self.bot.latency * 1000))

def setup(bot):
    bot.add_cog(Utility(bot))

推荐阅读