1
0
Fork 0

Integrate backup & restore extensions

This commit is contained in:
Ethanell 2020-07-30 11:56:31 +02:00
parent c59a89e5ac
commit 80c012438e
4 changed files with 154 additions and 125 deletions

View file

@ -8,3 +8,5 @@ bot.load_extension("extensions.reminder")
bot.load_extension("extensions.greetings") bot.load_extension("extensions.greetings")
bot.load_extension("extensions.presentation") bot.load_extension("extensions.presentation")
bot.load_extension("extensions.rorec") bot.load_extension("extensions.rorec")
bot.load_extension("extensions.backup")
bot.load_extension("extensions.restore")

View file

@ -1,18 +1,30 @@
from discord.ext import commands
from backup_bot.logger import logger
from os.path import isdir
from os import mkdir
import shelve import shelve
from datetime import datetime
from discord import File, Embed
from collections import OrderedDict from collections import OrderedDict
from datetime import datetime
from os import mkdir
from os.path import isdir
from discord import File, Embed
from discord.ext import commands
from administrator.logger import logger
extension_name = "backup" extension_name = "backup"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@commands.command("backup") class Backup(commands.Cog):
async def backup_cmd(ctx: commands.Context): def __init__(self, bot: commands.Bot):
self.bot = bot
def description(self):
return "Backup all message on the guild"
@commands.group("backup", pass_context=True)
@commands.guild_only()
@commands.has_permissions(manage_messages=True)
async def backup(self, ctx: commands.Context):
embed = Embed(title="Backup", description="In progress... \N{hourglass}") embed = Embed(title="Backup", description="In progress... \N{hourglass}")
msg = await ctx.send(embed=embed) msg = await ctx.send(embed=embed)
file_name = f"backup/{datetime.now().strftime('%d-%m-%Y %H:%M')}" file_name = f"backup/{datetime.now().strftime('%d-%m-%Y %H:%M')}"
@ -62,23 +74,23 @@ async def backup_cmd(ctx: commands.Context):
await ctx.send(file=File(file_name + ".db", "backup.db")) await ctx.send(file=File(file_name + ".db", "backup.db"))
def setup(bot: commands.Bot): def setup(bot):
logger.info(f"Loading...") logger.info(f"Loading...")
if not isdir("backup"): if not isdir("backup"):
logger.info(f"Create backup folder") logger.info(f"Create backup folder")
mkdir("backup") mkdir("backup")
try: try:
bot.add_command(backup_cmd) bot.add_cog(Backup(bot))
except Exception as e: except Exception as e:
logger.error(f"Error loading: {e}") logger.error(f"Error loading: {e}")
else: else:
logger.info(f"Load successful") logger.info(f"Load successful")
def teardown(bot: commands.Bot): def teardown(bot):
logger.info(f"Unloading...") logger.info(f"Unloading...")
try: try:
bot.remove_command("backup") bot.remove_cog("Backup")
except Exception as e: except Exception as e:
logger.error(f"Error unloading: {e}") logger.error(f"Error unloading: {e}")
else: else:

View file

@ -1,17 +1,29 @@
from discord.ext import commands
from backup_bot.logger import logger
from os.path import isdir
from os import mkdir, remove
import shelve import shelve
from requests import get from os import mkdir, remove
from os.path import isdir
from discord import Embed from discord import Embed
from discord.ext import commands
from requests import get
from administrator.logger import logger
extension_name = "restore" extension_name = "restore"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@commands.command("restore") class Restore(commands.Cog):
async def restore_cmd(ctx: commands.Context): def __init__(self, bot: commands.Bot):
self.bot = bot
def description(self):
return "Restore a backup of guild's messages"
@commands.group("restore", pass_context=True)
@commands.guild_only()
@commands.has_permissions(manage_messages=True)
async def restore(self, ctx: commands.Context):
if len(ctx.message.attachments) != 1: if len(ctx.message.attachments) != 1:
await ctx.send("No backup file given ! \N{cross mark}") await ctx.send("No backup file given ! \N{cross mark}")
else: else:
@ -73,23 +85,23 @@ async def restore_cmd(ctx: commands.Context):
await msg.edit(embed=embed) await msg.edit(embed=embed)
def setup(bot: commands.Bot): def setup(bot):
logger.info(f"Loading...") logger.info(f"Loading...")
if not isdir("backup"): if not isdir("backup"):
logger.info(f"Create backup folder") logger.info(f"Create backup folder")
mkdir("backup") mkdir("backup")
try: try:
bot.add_command(restore_cmd) bot.add_cog(Restore(bot))
except Exception as e: except Exception as e:
logger.error(f"Error loading: {e}") logger.error(f"Error loading: {e}")
else: else:
logger.info(f"Load successful") logger.info(f"Load successful")
def teardown(bot: commands.Bot): def teardown(bot):
logger.info(f"Unloading...") logger.info(f"Unloading...")
try: try:
bot.remove_command("restore") bot.remove_cog("Restore")
except Exception as e: except Exception as e:
logger.error(f"Error unloading: {e}") logger.error(f"Error unloading: {e}")
else: else:

View file

@ -1,11 +1,14 @@
aiohttp==3.6.2 aiohttp==3.6.2
async-timeout==3.0.1 async-timeout==3.0.1
attrs==19.3.0 attrs==19.3.0
certifi==2020.6.20
chardet==3.0.4 chardet==3.0.4
discord==1.0.1 discord==1.0.1
discord.py==1.3.4 discord.py==1.3.4
idna==2.10 idna==2.10
multidict==4.7.6 multidict==4.7.6
requests==2.24.0
SQLAlchemy==1.3.18 SQLAlchemy==1.3.18
urllib3==1.25.10
websockets==8.1 websockets==8.1
yarl==1.4.2 yarl==1.4.2