dicord.py: get_channel() returns None

Question:

I am working on a bot which in addition to executing some code should remove reactions added to a specific message.

In the code I wrote, the method get_channel() returns 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)

I can use await fetch_channel() method which works fine but as it’s an API call it’s working much slower (at least I think it is) than the one mentioned above.

EDIT:

I’ve tried to debug with the following code:

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))

The output is:

None
wolex#0000
None
lounge
Asked By: Adrian Krievins

||

Answers:

I had members and guilds intents disabled. To enable them do the following:

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

You’ll also need ”Privileged Gateway Intents” enabled. Follow the official docs.

Answered By: Adrian Krievins

Make sure the CHANNEL_ID is an int and not a 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)
Answered By: ankitgupta5294