Skip to content

Commit

Permalink
Merge pull request #51 from ufosc/develop
Browse files Browse the repository at this point in the history
Add question, eboard, reminder, requirements versions
  • Loading branch information
hjarrell authored Jun 8, 2019
2 parents 75e118a + be3d7ab commit 75f8c27
Show file tree
Hide file tree
Showing 21 changed files with 457 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ __pycache__/
*.jpg
config.py
database/data/sqlite3.db
praw.ini

.idea/
11 changes: 7 additions & 4 deletions ALBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cogs.CONSTANTS as CONSTANTS
import config

'''Cogs to load when the bot first starts'''
"""Cogs to load when the bot first starts"""
startup_cogs = [
"cogs.messages",
"cogs.errors",
Expand All @@ -14,8 +14,10 @@
"cogs.projects",
"cogs.admin",
"cogs.music",
"cogs.compile",
"cogs.welcome"
"cogs.welcome",
"cogs.help",
"cogs.reminder",
"cogs.government"
]

bot_url = 'https://discordapp.com/api/oauth2/authorize?client_id={0}&scope=bot&permissions=0'
Expand All @@ -30,7 +32,8 @@ async def on_ready():
print('Invite URL: {iurl}'.format(iurl=bot_url.format(bot.user.id)))
print('-----')
await bot.change_presence(activity=discord.Game(name="Destroying propritary software"))



if __name__ == "__main__":
for extension in startup_cogs:
try:
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3-slim-stretch

WORKDIR /app

COPY ./requirements.txt .

RUN apt-get update && apt-get install -y git ffmpeg libopus-dev && \
pip install --trusted-host pypi.python.org -r requirements.txt
20 changes: 9 additions & 11 deletions cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

import cogs.util

class Admin:
class Admin(commands.Cog, name='Admin'):

def __init__(self, bot):
self.bot = bot

@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
async def load(self, ctx, extension_name : str):
'''Loads an extension.'''
"""Loads an extension."""
try:
if extension_name.startswith("cogs."):
self.bot.load_extension(extension_name)
else:
self.bot.load_extension("cogs." + extension_name)
except (AttributeError, ImportError) as e:
except Exception as e:
await ctx.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
await ctx.send("{} loaded.".format(extension_name))
Expand All @@ -39,15 +39,13 @@ async def unload(self, ctx, extension_name : str):
@commands.command(hidden=True)
@commands.check(cogs.util.is_officer_check)
async def reload(self, ctx, extension_name : str):
'''Unloads and then loads an extension'''
"""Unloads and then loads an extension"""
try:
if extension_name.startswith("cogs."):
self.bot.unload_extension(extension_name)
self.bot.load_extension(extension_name)
self.bot.reload_extension(extension_name)
else:
self.bot.unload_extension("cogs." + extension_name)
self.bot.load_extension("cogs." + extension_name)
except (AttributeError, ImportError) as e:
self.bot.reload_extension("cogs." + extension_name)
except Exception as e:
await ctx.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
await ctx.send("{} reloaded.".format(extension_name))
Expand All @@ -60,7 +58,7 @@ async def whereami(self, ctx):
@commands.command(hidden=True, name="eval")
@commands.check(cogs.util.is_owner)
async def admin_eval(self, ctx, *, cmd : str):
'''Evaluates Python code only if the executor is hjarrell'''
"""Evaluates Python code only if the executor is hjarrell"""
env = {
'bot': self.bot,
'discord': discord,
Expand All @@ -83,7 +81,7 @@ async def admin_eval(self, ctx, *, cmd : str):
ret = await eval("__admin_eval()", env)
except Exception as e:
value = stdout.getvalue()
await ctx.send("```py\n{value}{e}\n```".format())
await ctx.send("```py\n{}{}\n```".format(value, e))
else:
value = stdout.getvalue()
if ret is None:
Expand Down
9 changes: 5 additions & 4 deletions cogs/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import cogs.util

class Compile:
class Compile(commands.Cog, name='Compile'):

def __init__(self, bot):
self.bot = bot
self.complangs = []
self.is_debug = False

@commands.command(name="complangs")
async def compile_langs(self, ctx):
'''Get the languages and ids for compiling code'''
"""Get the languages and ids for compiling code"""
if len(self.complangs) == 0:
langs = requests.get("https://api.judge0.com/languages")

Expand All @@ -27,7 +28,7 @@ async def compile_langs(self, ctx):

@commands.command(name="compile")
async def _compile(self, ctx, lang_id : int, *, program : str):
'''Compiles and runs code using the judge0 api'''
"""Compiles and runs code using the judge0 api"""
payload = {'source_code' : program, 'language_id' : lang_id}
headers = {'Content-Type': "application/json"}
r = requests.post("https://api.judge0.com/submissions/?base64_encoded=false&wait=true", data=json.dumps(payload), headers=headers)
Expand All @@ -43,7 +44,7 @@ async def _compile(self, ctx, lang_id : int, *, program : str):
@commands.command(name="compdebug")
@commands.check(cogs.util.is_officer_check)
async def debug_compile(self, ctx):
'''Toggles whether to print the full compile output'''
"""Toggles whether to print the full compile output"""
self.is_debug = not self.is_debug
await ctx.send("is_debugging = {}".format(self.is_debug))

Expand Down
22 changes: 12 additions & 10 deletions cogs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from database.database import SQLCursor, SQLConnection
from cogs.messages import track

class ALBotErrorHandlers:
class ALBotErrorHandlers(commands.Cog, name='Error Handler'):
""" Handles errors """

def __init__(self, bot, db):
self.bot = bot
self.db = db
Expand All @@ -34,9 +33,9 @@ def _construct_error_embed(self, command_name, error_name, error_text, full_comm
break
else:
embed.add_field(name='Press {expand}'.format(expand=CONSTANTS.REACTION_EXPAND), value='for full error backtrace', inline=False)

return embed

def _construct_unknown_command_embed(self, error_text, full_text):
title = "{notfound} Invalid command.".format(notfound=CONSTANTS.REACTION_NOT_FOUND)
embed = discord.Embed(title=title, colour=discord.Colour(CONSTANTS.EMBED_COLOR_ERROR), description='```{0}```'.format(error_text))
Expand All @@ -45,6 +44,7 @@ def _construct_unknown_command_embed(self, error_text, full_text):

return embed

@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if type(error) == discord.ext.commands.MissingPermissions:
await ctx.message.add_reaction(CONSTANTS.REACTION_DENY)
Expand All @@ -69,7 +69,8 @@ async def on_command_error(self, ctx, error):
bt_string = ''.join(traceback.format_exception(type(error), error, error.__traceback__))
print('{bname} encountered an error:\n{0}'.format(bt_string, bname=CONSTANTS.BOT_NAME))
cur.execute('INSERT INTO error_messages (message_id, channel_id, command_name, error_name, error_text, full_backtrace, full_command_string) VALUES (?,?,?,?,?,?,?);',(msg.id, msg.channel.id, ctx.command.name, str(type(error)), str(error), bt_string, ctx.message.content))


@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.user_id == self.bot.user.id:
return
Expand All @@ -81,10 +82,11 @@ async def on_raw_reaction_add(self, payload):
if not row:
return

to_edit = await self.bot.get_channel(payload.channel_id).get_message(payload.message_id)
to_edit = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
new_embed = self._construct_error_embed(row[0],row[1],row[2],row[3],row[4])
await to_edit.edit(content='{err} Command error {err}'.format(err=CONSTANTS.REACTION_ERROR),embed=new_embed)


@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.user_id == self.bot.user.id:
return
Expand All @@ -96,11 +98,11 @@ async def on_raw_reaction_remove(self, payload):
if not row:
return

to_edit = await self.bot.get_channel(payload.channel_id).get_message(payload.message_id)
to_edit = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
new_embed = self._construct_error_embed(row[0],row[1],row[2],row[3])
await to_edit.edit(content='{err} Command error {err}'.format(err=CONSTANTS.REACTION_ERROR),embed=new_embed)



def setup(bot):
bot.add_cog(ALBotErrorHandlers(bot, SQLConnection()))
Loading

0 comments on commit 75f8c27

Please sign in to comment.