[英]Discord Bot Command log
我有一個 Discord 機器人,我試圖通過跟蹤命令使用來跟蹤機器人的使用情況。 我的代碼如下。 我遇到了似乎無法獲取命令名稱的問題。 這是代碼:
@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}')
當運行一個命令(任何命令)時,它說“缺少必需的 arg '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____)。 我錯過了什么? 我可以嘗試什么?
當運行命令(任何命令)時,它說“缺少必需的 arg 'command'”
您收到此錯誤是因為 on_command 的API 文檔指出它只有 1 個參數,即ctx
,因此 Discord永遠不會將任何其他內容傳遞給它,因此您的command
參數將始終丟失。
Context
的文檔說您可以使用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____)
這是Context
實例的字符串表示。 因為你只是發送command
而你什么都不做,並且看到它實際上是Context
而不是我上面解釋的Command
,這將是結果。
我想要類似的東西,並通過反復試驗得到了這個,其中 msg_dump_channel 是您要將 output 打印到的頻道的 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.