python - 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.
解决方案
ping.py 中有 3 个错误:
- ping 命令中不需要参数。
- 它应该是 (self.bot.latency) 而不是 (bot.latency)
- 要在 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))
推荐阅读
- javascript - 浏览器禁用 javascript 时禁用表单字段和按钮
- icons - sap.m.upload.UploadSet 不显示图标
- bash - 解析来自多个文件夹的文本文件并创建新的 [shell]
- python-3.x - 导入错误:无法从分数导入名称 gcd
- node.js - 解析错误:'import' 和 'export' 可能只出现在 'sourceType: module'eslint
- windows - 运行 cmd 命令:我想看看(获取协议)在后台发生了什么
- r - 用 R 中的条件替换列中的 Na
- r - 将 dplyr::across 与两组变量一起使用
- wordpress - 按类别划分的 Slick 滑块设置
- sql - 如何使用 SQL 计算每个组中的行数?