Discord.py: Add helpers like fetch_or_get_channel to Client

Created on 9 Sep 2020  路  1Comment  路  Source: Rapptz/discord.py

The Problem

When working with on_raw_* events, the only options to get the actual model is to call Client.fetch_* or Client.get_*. I find my self writing utility functions like:

async def get_or_fetch_channel(self, channel_id) -> discord.TextChannel:
    return chan if (chan := self.bot.get_channel(channel_id)) is None \
        else await self.bot.fetch_channel(channel_id)

How about if these were included with the library? I opened an issue because wasn't sure if you'd like to have them in the library, I can make a PR if you agree on this.

The Ideal Solution

Having those extension functions on the Client class, so one can just say-
channel = await bot.get_or_fetch_channel(channel_id)

The Current Solution

Writing a utility function in the project.

Summary

Addition of three new functions: Client#get_or_fetch_channel, Client#get_or_fetch_message, Client#get_or_fetch_guild which return the models if they exist in cache or fetch them if not.

suggestion

Most helpful comment

Python allows you to do this already without the function boilerplate:

channel = bot.get_channel(channel_id) or await bot.fetch_channel(channel_id)

This is sufficient enough and usually simple enough that having it in the library only buys you character counts which isn't too much when you consider you also have to handle errors and whatever else you're planning on doing.

>All comments

Python allows you to do this already without the function boilerplate:

channel = bot.get_channel(channel_id) or await bot.fetch_channel(channel_id)

This is sufficient enough and usually simple enough that having it in the library only buys you character counts which isn't too much when you consider you also have to handle errors and whatever else you're planning on doing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yolotroll101 picture Yolotroll101  路  3Comments

reuscam picture reuscam  路  3Comments

TunaHobby picture TunaHobby  路  3Comments

AraHaan picture AraHaan  路  3Comments

synbitz picture synbitz  路  3Comments