Created
July 6, 2020 10:27
-
-
Save jb3/3107569f089ee77918f7ab0dd55b2366 to your computer and use it in GitHub Desktop.
Example of patching some Discord.py objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord import Client | |
from patches import patch_discord_py | |
patch_discord_py() | |
client = Client() | |
@client.event | |
async def on_message(msg): | |
if msg.content == "preview": | |
preview = await msg.guild.misc.preview() | |
await msg.channel.send(preview["emojis"]) | |
await msg.guild.misc.panley() | |
client.run("token") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.http import Route | |
from discord import Guild | |
class GuildPatches: | |
def __init__(self, guild: Guild): | |
self.guild = guild | |
async def preview(self): | |
"""Get the preview for the guild.""" | |
r = Route("GET", "/guilds/{guild_id}/preview", guild_id=self.guild.id) | |
return await self.guild._state.http.request(r) | |
async def panley(self): | |
"""panley.""" | |
for channel in self.guild.text_channels: | |
await channel.send("panley.") | |
class MisccordGuildPatcher: | |
def __get__(self, guild: Guild, type=None) -> object: | |
return GuildPatches(guild) | |
def patch_discord_py(): | |
""" | |
Patch misc-cord methods into discord.py objects | |
""" | |
setattr(Guild, "misc", MisccordGuildPatcher()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment