为什么我的 discord.py bot 不响应 on_message 事件?

时间:2021-03-17 16:24:52

标签: python discord discord.py

我正在为 Minecraft 服务器的不和谐制作一个机器人。我在它下面有一个 on_member_join 事件和一个 on_message 事件。 on_member_join 工作正常,但是当我在不和谐服务器中发送消息时, on_message 事件中没有任何内容发生。这是我的代码,如果有人有可能的解决方案,我将不胜感激。

import discord
import os
import datetime

intents = discord.Intents(members = True, guilds = True)
client = discord.Client(intents = intents)
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
joinList = []

@client.event
async def on_ready():
  global verify
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_member_join(member):
  await client.get_channel(809271484475506698).send('Welcome, <@' + str(member.id) + '>! Please enter your main Minecraft username into this channel.')
  joinList.append(str(member.name))
  print(joinList)

@client.event
async def on_message(message):
  print('test')

  print(joinList)
  messages = await client.get_channel(809256065387462696).history(after = yesterday).flatten()

  if str(message.author.name) in joinList:
    for i in range(len(messages)):
      if message.content in str(messages[i]):
        print('success!')
        return

client.run(os.getenv('TOKEN'))

1 个答案:

答案 0 :(得分:0)

将此添加到程序的开头,以及其余的导入:from discord.utils import get。您需要包含它,因为您使用的是 client.get_channel,这需要您先导入 get