繁体   English   中英

dicord.py:get_channel() 返回无

[英]dicord.py: get_channel() returns None

我正在开发一个机器人,除了执行一些代码外,它还应该删除添加到特定消息的反应。

在我编写的代码中,方法get_channel()返回None

async def on_raw_reaction_add(self, payload):
    if payload.message_id == self.message_id['private_room']:
        if payload.emoji.name == self.lock:
            rchannel = self.get_channel(payload.channel_id)
            rmsg = await rchannel.fetch_message(payload.message_id)
            ruser = await self.fetch_user(payload.user_id)
            await rmsg.remove_reaction(self.lock, ruser)

我可以使用await fetch_channel()方法,它工作正常,但由于它是一个 API 调用,它的工作速度比上面提到的慢得多(至少我认为是这样)。

编辑:

我尝试使用以下代码进行调试:

print(self.get_user(payload.user_id))
print(await self.fetch_user(payload.user_id))
print(self.get_channel(payload.channel_id))
print(await self.fetch_channel(payload.channel_id))

output 是:

None
wolex#0000
None
lounge

我禁用了membersguilds意图。 要启用它们,请执行以下操作:

intents = discord.Intents.none()
intents.reactions = True
intents.members = True
intents.guilds = True

您还需要启用“特权网关意图”。 遵循官方文档

确保CHANNEL_ID 是 int 而不是 string

CHANNEL_ID = 746382173
channel = None

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))
  channel = client.get_channel(CHANNEL_ID)
  print(channel)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM