我正在制作一个运行良好的 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()
答案 0 :(得分:0)
导入:
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 我 ;)