如何为不和谐机器人上的命令创建日志消息

问题描述 投票:0回答:1

我尝试创建一个频道,让机器人发布所使用的命令,但我尝试的任何方法都不起作用。

@client.event
async def on_command(ctx):
    print(f'Commande exécutée: {ctx.command} par {ctx.author}')

(尝试过 bot.event)

@bot.before_invoke
async def before_any_command(ctx):
    print(f'Commande exécutée: {ctx.command} par {ctx.author}')
python python-3.x discord discord.py
1个回答
0
投票
import discord
from discord.ext import commands

# Replace with your bot's token
TOKEN = 'YOUR_BOT_TOKEN' 

# Replace with the ID of your desired logging channel
LOGGING_CHANNEL_ID = 123456789012345678 

intents = discord.Intents.default()
intents.message_content = True  # Enable message content for commands

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')
    bot.logging_channel = bot.get_channel(LOGGING_CHANNEL_ID)

@bot.event
async def on_command(ctx):
    # Construct the log message
    log_message = f'**Command:** `{ctx.command}`\n'
    log_message += f'**Author:** {ctx.author.mention} (`{ctx.author}`)\n'
    log_message += f'**Channel:** {ctx.channel.mention}'

    if ctx.guild:  # If the command was used in a server
        log_message += f' (**Server:** {ctx.guild.name})'

    # Send the log message to the designated channel
    await bot.logging_channel.send(log_message) 

# Your bot's commands go here
@bot.command()
async def hello(ctx):
    await ctx.send("Hello there!")

bot.run(TOKEN)
© www.soinside.com 2019 - 2024. All rights reserved.