Skip to content

Instantly share code, notes, and snippets.

@jb3
Created July 6, 2020 10:27
Show Gist options
  • Save jb3/3107569f089ee77918f7ab0dd55b2366 to your computer and use it in GitHub Desktop.
Save jb3/3107569f089ee77918f7ab0dd55b2366 to your computer and use it in GitHub Desktop.
Example of patching some Discord.py objects
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")
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