Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from discord.ext import commands
class CogDownloader(commands.Cog)
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=['dlcog'])
@commands.is_owner()
async def downloadcog(self, ctx, url: str, overwrite: bool):
"""Download a cog from github"""
raw_url = url.replace("/blob", "").replace("github.com", "raw.githubusercontent.com")
r = await bot.session.get(raw_url)
message = await ctx.send("Downloading cog...")
file_name = raw_url[raw_url.rfind("/")+1:]
file_path = "./cogs/" + file_name
try:
with open(file_path, "x", encoding="utf-8") as n:
n.write(await r.text())
except FileExistsError:
if overwrite:
with open(file_path, "w", encoding="utf-8") as n:
n.write(await r.text())
try:
bot.load_extension(f"cogs.{file_name[:-3]}")
except commands.ExtensionAlreadyLoaded:
if overwrite:
self.bot.reload_extension(f"cogs.{file_name[:-3]}")
except Exception as e:
raise e
await message.edit(f"Downloaded `{file_name}` and loaded it.")
def setup(bot):
bot.add_cog(CogDownloader(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment