首页 > 解决方案 > discord.py 同时使用 on_message() 和 command()

问题描述

使用下面的代码,我的机器人打印了我写的内容,但命令不起作用。我认为顺序是问题。所以,我尝试先对@bot.command() 进行编码,然后再对@bot.event 进行编码,但仍然无法正常工作......有什么问题?

import discord
from discord.ext import commands, tasks
import time
import datetime

bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"))
token = ''

@bot.event #working
async def on_message_delete(message):
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    print('<' + st + '>' '메시지 삭제 ' + message.author.name + ':' + message.content)


@bot.event #working
async def on_message_edit(before, after):
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    fmt = '**{0.author}** 가 메시지를 수정했습니다.:\n{0.content} -> {1.content}'
    print('<' + st + '>' + fmt.format(before, after))


@bot.event #working
async def on_message(message):
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    print('<' + st + '> ' + message.author.name + " : " + message.content)


@bot.command() #Not working
async def test(ctx):
    embed = discord.Embed \
            (
                title='노노봇 명령어 입니다',
                description='',
                colour=discord.Colour.blue()
            )
    embed.add_field(name='~', value='~', inline=False)
    await ctx.channel.send(embed=embed)


bot.run(token)

标签: pythondiscorddiscord.py

解决方案


推荐阅读