如何在on_message函数中发送消息?

时间:2020-07-23 18:45:37

标签: python discord bots discord.py

import discord 
from discord.ext import commands
bot = commands.Bot(command_prefix = "{")
@bot.event  
async def on_message(message):
    msg = message.content
    if "hi" in message.content:
        await message.channel.purge( limit = 1 )
        await bot.send_message(message.channel, msg) #The problem is here
    print(f"{message.content}, {message.author}")

它说“ Bot”对象没有属性“ send_message”。
如果邮件中包含单词“ hi”,该如何发送消息?

1 个答案:

答案 0 :(得分:2)

send_message是旧的discord.py方法。在discord.py rewrite(> v1.0)中,您必须在on_message事件中使用discord.TextChannel.send()方法:

@bot.event  
async def on_message(message):
    msg = message.content
    if "hi" in message.content:
        await message.channel.purge(limit = 1)
        await message.channel.send(msg)

这里是summary between discord.py and discord.py rewrite,以防两个版本之间的区别不大。