Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Why is bot.get_channel(<id>) not returning DMChannels? #10187

Answered by Rapptz
TheAyes asked this question in Q&A
Discussion options

I'm currently trying to make a chat bot users can message in dms:

async def send_response(self, message: MyMessage):
"""Send a response back to the Discord channel"""
if not self.bot:
	return
# Get the channel
channel = self.bot.get_channel(int(message.channel)) # This returns `None`
if channel: # Consequently. this fails
	await channel.send(message.content)

I've made extra sure that the CHANNEL_ID ( the one you can copy with RMB -> Copy Channel ID is exactly the same as the one that is being passed to that function. I also tried doing the same thing in a server channel... Which works perfectly fine.

Now I'm under the impression that I'm either doing something wrong or the get_channel function isn't working properly.

You must be logged in to vote

DMs are only available on a LRU cache of 128 channels because Discord does not actually recommend keeping track of these long term. This is documented and warned pretty sure. You can also just use Client.get_partial_messageable to avoid an API call if you know the channel ID is valid.

Replies: 1 comment 5 replies

Comment options

Message.channel is already a channel. Use this?

Discord Channel objects don't support int casts in the way you're trying to use them. If you had used the message.channel.id attribute it would be fine, even then it's already an int.

You must be logged in to vote
5 replies
Comment options

I'm writing a system that is not exclusively taking inputs from discord. For this matter I have to have some middle ground. The Message type is not an actual discord.Message but a type of my own.
In the context of that type, id is being stored as a string.

I've just now also tried to get the user instead of the channel because I saw an ancient issue about dm channels not having it's own channel_id and it instead being a user_id. But even that returns None:
image

I really want to avoid using a whole lot of methods that are exclusive to discord for the reason stated above.

Some more context:

@dataclass(slots=True)
class MyMessage:
	channel_id: str
	direction: Literal["in", "out"]
	content: str
	author: str
	role: Literal["system", "assistant", "user"]
	response_time: float
	timestamp: float = time.time()

I surely could just "add" the methods to directly act upon the message but then it became exclusive to discord and I would have to do that for every single input.

Comment options

After further debugging I looks like that bot.fetch_channel is working properly and returns the DMChannel as desired. However directly using the api undermines most of what discord.py is meant for.

Does this indicate an issue with the get_channel function?

Comment options

DMs are only available on a LRU cache of 128 channels because Discord does not actually recommend keeping track of these long term. This is documented and warned pretty sure. You can also just use Client.get_partial_messageable to avoid an API call if you know the channel ID is valid.

Answer selected by TheAyes
Comment options

I'm very far away from 128 channels. However using a partial does indeed work.

Comment options

You also don't get DM channels at all until someone sends a message there either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /