【问题标题】:Why does my on_message event doesn't work (discord.py)为什么我的 on_message 事件不起作用(discord.py)
【发布时间】:2021-04-11 12:46:02
【问题描述】:

我正在制作一个运行良好的 discord 机器人,但我想开始使用 cogs,因为我认为这是编写代码的更好方式,但现在我的 on_message 不再工作并且它没有显示任何错误我在整个互联网上搜索了如何修复它的消息,所有解释都对我不起作用,所以我决定在这里问。所以这是我的代码:

from discord.ext import commands

TOKEN = 'my token'

bot = commands.Bot(command_prefix="$")


class Interactions(commands.Cog):
    @commands.Cog.listener()
    async def on_message(self, message):
        msg = message.content
        hi = ["hi", "hello", "hi!", "hello!"]
        bye = ["bye", "good bye", "bye!", "good bye!"]

        # Messages in english
        if str(msg).lower() in hi:
            await message.channel.send('Hello!')

        if str(msg).lower == 'how are you?':
            await message.channel.send("I'm fine")

        if str(msg).lower == "what's your favorite food?":
            await message.channel.send("Sushi with sweet potato!")

        if str(msg).lower == "what do you eat?":
            await message.channel.send(
                "I don't eat, I'm just a simple bot :pensive:")

        if str(msg).lower == "are you fine?":
            await message.channel.send("Yes, I am")

        if str(msg).lower in bye:
            await message.channel.send("Good bye!")

def run():
    bot.add_cog(Interactions(bot)
    bot.run(TOKEN)


if __name__ == "__main__":
    run()

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    导入:

    from discord.ext.commands import command, Cog
    

    其余代码:

    TOKEN = 'my token'
    
    bot = commands.Bot(command_prefix="$")
    
    
    class Interactions(commands.Cog):
        @commands.Cog.listener()
      
        async def on_message(self, message):
            pass
    

    这应该可以工作,如果不只是 ping 我 ;)

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2018-10-14
      • 1970-01-01
      • 2018-06-10
      • 2021-02-12
      • 2022-01-04
      • 2021-06-24
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多