如何将参数传递给在不和谐机器人中传递给 bot.get_command() 的命令?
How to pass arguments into a command being passed into bot.get_command() in a discord bot?
我正在尝试为我的私人 discord 服务器创建一个 discord 机器人,但我遇到了 运行 问题。
我有三个函数,它们以齿轮的形式 load
、unload
和 reload
扩展。 load
和 unload
命令的创建完全没问题,但我在使用 reload
命令时遇到了问题。
为了不重复代码,我想在 reload(extension)
命令中调用 unload(extension)
和 load(extension
,但是,我还没有弄清楚如何这样做。
这是我的代码:
import discord
from discord.ext import commands
import os
from settings import BOT_TOKEN
client = commands.Bot(command_prefix=(".", "!", "?", "-"))
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle)
print("Discord_Bot is ready")
@client.command()
async def load(ctx, extension):
client.load_extension("cogs.{0}".format(extension))
@client.command()
async def unload(ctx, extension):
client.unload_extension("cogs.{0}".format(extension))
@client.command()
async def reload(ctx, extension):
await ctx.invoke(client.get_command("unload({0}".format(extension)))
await ctx.invoke(client.get_command("load({0})".format(extension)))
# Load Cogs on Boot
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
client.load_extension("cogs.{0}".format(filename[:-3]))
client.run(BOT_TOKEN)
我还有一个 example_cog.py
用于测试 load
、unload
和 reload
命令的功能。此文件中没有命令,只有作为 cog 所需的基础知识。
example_cog.py
import discord
from discord.ext import commands
class Example_Cog(commands.Cog):
def __init__(self, client):
self.client = client
def setup(client):
client.add_cog(Example_Cog(client))
当我在我的私人 discord 服务器上使用 bot 并尝试重新加载时,它不起作用。我已阅读文档,但无法弄清楚如何将参数传递给 bot.get_command()
函数。非常感谢在这个问题上的帮助。
我尝试了很多不同的方法来使用 bot.get_command()
函数,但其中 none 有效。其中包括:
await ctx.invoke(client.get_command("unload {0}".format(extension)))
await ctx.invoke(client.get_command("unload({0})".format(extension)))
谢谢,本
您需要以字符串类型传递命令名称。示例:
@bot.event
async def on_ready():
# call command without args
await bot.get_command('examplecommand').callback()
# call command with args
await bot.get_command('exampleArgsCommand').callback(ctx, message)
@bot.command()
async def examplecommand():
pass
@bot.command()
async def exampleArgsCommand(ctx, message):
pass
我正在尝试为我的私人 discord 服务器创建一个 discord 机器人,但我遇到了 运行 问题。
我有三个函数,它们以齿轮的形式 load
、unload
和 reload
扩展。 load
和 unload
命令的创建完全没问题,但我在使用 reload
命令时遇到了问题。
为了不重复代码,我想在 reload(extension)
命令中调用 unload(extension)
和 load(extension
,但是,我还没有弄清楚如何这样做。
这是我的代码:
import discord
from discord.ext import commands
import os
from settings import BOT_TOKEN
client = commands.Bot(command_prefix=(".", "!", "?", "-"))
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle)
print("Discord_Bot is ready")
@client.command()
async def load(ctx, extension):
client.load_extension("cogs.{0}".format(extension))
@client.command()
async def unload(ctx, extension):
client.unload_extension("cogs.{0}".format(extension))
@client.command()
async def reload(ctx, extension):
await ctx.invoke(client.get_command("unload({0}".format(extension)))
await ctx.invoke(client.get_command("load({0})".format(extension)))
# Load Cogs on Boot
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
client.load_extension("cogs.{0}".format(filename[:-3]))
client.run(BOT_TOKEN)
我还有一个 example_cog.py
用于测试 load
、unload
和 reload
命令的功能。此文件中没有命令,只有作为 cog 所需的基础知识。
example_cog.py
import discord
from discord.ext import commands
class Example_Cog(commands.Cog):
def __init__(self, client):
self.client = client
def setup(client):
client.add_cog(Example_Cog(client))
当我在我的私人 discord 服务器上使用 bot 并尝试重新加载时,它不起作用。我已阅读文档,但无法弄清楚如何将参数传递给 bot.get_command()
函数。非常感谢在这个问题上的帮助。
我尝试了很多不同的方法来使用 bot.get_command()
函数,但其中 none 有效。其中包括:
await ctx.invoke(client.get_command("unload {0}".format(extension)))
await ctx.invoke(client.get_command("unload({0})".format(extension)))
谢谢,本
您需要以字符串类型传递命令名称。示例:
@bot.event
async def on_ready():
# call command without args
await bot.get_command('examplecommand').callback()
# call command with args
await bot.get_command('exampleArgsCommand').callback(ctx, message)
@bot.command()
async def examplecommand():
pass
@bot.command()
async def exampleArgsCommand(ctx, message):
pass