Python Discord Bot命令日志
我有一个Discord Bot,我试图通过跟踪命令使用情况来跟踪Bot的使用情况。下面是我的代码。我遇到了无法获取命令名的问题。 代码如下:Python Discord Bot命令日志,python,python-3.x,discord,discord.py,discord.py-rewrite,Python,Python 3.x,Discord,Discord.py,Discord.py Rewrite,我有一个Discord Bot,我试图通过跟踪命令使用情况来跟踪Bot的使用情况。下面是我的代码。我遇到了无法获取命令名的问题。 代码如下: @commands.Cog.listener(name='on_command') async def print(self, ctx, command): server = ctx.guild.name user = command.author command = ctx.command
@commands.Cog.listener(name='on_command')
async def print(self, ctx, command):
server = ctx.guild.name
user = command.author
command = ctx.command
print(f'{server} > {user} > {command}')
当运行命令(任何命令)时,它会显示“缺少必需的参数‘command’”。我还尝试了其他代码。我尝试过的其他代码:
@commands.Cog.listener(name='on_command')
async def print(self, command, server=None, user=None):
server = command.guild.name
user = command.author
print(f'{server} > {user} > {command}')
这只是发送除命令之外的所有内容。代替命令的是发送一个看起来像十六进制代码的东西(0x___;)。我错过了什么?我能试试什么
运行命令(任何命令)时,它会显示“缺少必需的参数‘command’”
之所以出现此错误,是因为它只声明了一个参数,即ctx
,因此Discord将不会向它传递任何其他内容,因此命令
参数将始终丢失
文档中说,您可以使用ctx.command
获取命令
@commands.Cog.listener(name='on_command')
async def print(self, ctx):
server = ctx.guild.name
user = ctx.author
command = ctx.command
print(f'{server} > {user} > {command}')
代替命令的是发送一个看起来像十六进制代码的东西(0x___;)
这是
上下文
实例的字符串表示。由于您只发送命令
,而不使用它,而且它实际上是上下文
,而不是我上面解释的命令
,所以这将是结果。我想要类似的东西,并通过反复试验实现了这一点,其中,msg_dump_channel是要将输出打印到的通道的id
from discord.ext import commands
bot = commands.Bot(command_prefix = '>')
@bot.event
async def on_command(ctx):
channel = bot.get_channel(msg_dump_channel)
server = ctx.guild.name
user = ctx.author
command = ctx.command
await channel.send('{} used {} in {}'.format(user, command, server))