diff --git a/cogs/basecog.py b/cogs/basecog.py index 865e13c..585664a 100644 --- a/cogs/basecog.py +++ b/cogs/basecog.py @@ -10,7 +10,6 @@ General Public License, version 2. import discord import discord.ext.commands as commands -import os class BaseCog(commands.Cog): def __init__(self, bot): @@ -26,30 +25,6 @@ class BaseCog(commands.Cog): embed = embed.add_field(name="License", value=self.gs.info.license) await ctx.send(embed=embed) - @commands.command(name="restart") - async def _restart_bot(self, ctx): - """Restarts the bot.""" - if ctx.author.id in self.gs.opt.owners_uids: - await ctx.message.add_reaction("✅") - await self.bot.logout() - else: - try: - await ctx.message.add_reaction("❌") - except: - return - - @commands.command(name="shutdown") - async def _shutdown_bot(self, ctx): - """Shuts down the bot.""" - if ctx.author.id in self.gs.opt.owners_uids: - await ctx.message.add_reaction("✅") - os._exit(42) - else: - try: - await ctx.message.add_reaction("❌") - except: - return - @commands.command(name="ping") async def _ping(self, ctx): await ctx.send(f'**Pong!** Current ping is {self.bot.latency*1000:.1f} ms') diff --git a/cogs/imagecog.py b/cogs/imagecog.py new file mode 100644 index 0000000..d1105c3 --- /dev/null +++ b/cogs/imagecog.py @@ -0,0 +1,100 @@ +""" +Image cog for qrm +--- +Copyright (C) 2019 Abigail Gold, 0x5c + +This file is part of discord-qrmbot and is released under the terms of the GNU +General Public License, version 2. +""" + +import discord +import discord.ext.commands as commands + +import aiohttp +import io + +class ImageCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.gs = bot.get_cog("GlobalSettings") + + @commands.command(name="plan", aliases=['bands']) + async def _bandplan(self, ctx, msg: str = ''): + '''Posts an image of Frequency Allocations. + Optional argument: `cn`, `ca`, `nl`, `us`, `mx`.''' + urls = {'cn': 'https://cdn.discordapp.com/attachments/364489754839875586/468770333223157791/Chinese_Amateur_Radio_Bands.png', + 'ca': 'https://cdn.discordapp.com/attachments/448839119934717953/469972377778782208/RAC_Bandplan_December_1_2015-1.png', + 'nl': 'http://www.pd3jdm.com/wp-content/uploads/2015/09/bandplan.jpg', + 'us': 'https://cdn.discordapp.com/attachments/377206780700393473/466729318945652737/band-chart.png', + 'mx': 'https://cdn.discordapp.com/attachments/443246106416119810/553771222421209090/mx_chart.png'} + names = {'cn': 'Chinese', + 'ca': 'Canadian', + 'nl': 'Dutch', + 'us': 'US', + 'mx': 'Mexican'} + arg = msg.lower() + + with ctx.typing(): + try: + embed = discord.Embed(title=f'{names[arg]} Amateur Radio Bands', colour=self.gs.colours.good) + embed.set_image(url=urls[arg]) + except: + embed = discord.Embed(title=f'{names["us"]} Amateur Radio Bands', colour=self.gs.colours.good) + embed.set_image(url=urls['us']) + await ctx.send(embed=embed) + + @commands.command(name="cond", aliases=['condx']) + async def _band_conditions(self, ctx, msg : str = ''): + '''Posts an image of HF Band Conditions.''' + with ctx.typing(): + async with aiohttp.ClientSession() as session: + async with session.get('http://www.hamqsl.com/solarsun.php') as resp: + if resp.status != 200: + return await ctx.send('Could not download file...') + data = io.BytesIO(await resp.read()) + await ctx.send(file=discord.File(data, 'condx.png')) + + @commands.command(name="grayline", aliases=['greyline', 'grey', 'gray', 'gl']) + async def _grayline(self, ctx, msg : str = ''): + '''Posts a map of the current greyline, where HF propagation is the best.''' + with ctx.typing(): + async with aiohttp.ClientSession() as session: + async with session.get('http://www.fourmilab.ch/cgi-bin/uncgi/Earth?img=NOAAtopo.evif&imgsize=320&dynimg=y&opt=-p&lat=&lon=&alt=&tle=&date=0&utc=&jd=') as resp: + if resp.status != 200: + return await ctx.send('Could not download file...') + data = io.BytesIO(await resp.read()) + await ctx.send(file=discord.File(data, 'greyline.jpg')) + + @commands.command(name="map") + async def _map(self, ctx, msg: str = ''): + '''Posts an image of Frequency Allocations. + Optional argument:`cq` = CQ Zones, `itu` = ITU Zones, `arrl` or `rac` = + ARRL/RAC sections, `cn` = Chinese Callsign Areas, `us` = US Callsign Areas.''' + map_urls = {"cq": 'https://cdn.discordapp.com/attachments/427925486908473344/472856720142761985/cq-zone.png', + "itu": 'https://cdn.discordapp.com/attachments/427925486908473344/472856796235563018/itu-zone.png', + "arrl": 'https://cdn.discordapp.com/attachments/427925486908473344/472856898220064778/sections.png', + "rac": 'https://cdn.discordapp.com/attachments/427925486908473344/472856898220064778/sections.png', + "cn": 'https://cdn.discordapp.com/attachments/443246106416119810/492846548242137091/2011-0802-E4B8ADE59BBDE4B89AE4BD99E58886E58CBAE59CB0E59BBEE88BB1E696871800x1344.png', + "us": 'https://cdn.discordapp.com/attachments/427925486908473344/472856506476265497/WASmap_Color.png' + } + map_titles = {"cq": 'Worldwide CQ Zones Map', + "itu": 'Worldwide ITU Zones Map', + "arrl": 'ARRL/RAC Section Map', + "rac": 'ARRL/RAC Section Map', + "cn": 'Chinese Callsign Areas', + "us": 'US Callsign Areas' + } + + arg = msg.lower() + with ctx.typing(): + try: + embed = discord.Embed(title=map_titles[arg], colour=self.gs.colours.good) + embed.set_image(url=map_urls[arg]) + except: + embed = discord.Embed(title=map_titles["us"], colour=self.gs.colours.good) + embed.set_image(url=map_urls["us"]) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(ImageCog(bot)) diff --git a/cogs/studycog.py b/cogs/studycog.py new file mode 100644 index 0000000..530b3c9 --- /dev/null +++ b/cogs/studycog.py @@ -0,0 +1,100 @@ +""" +Study cog for qrm +--- +Copyright (C) 2019 Abigail Gold, 0x5c + +This file is part of discord-qrmbot and is released under the terms of the GNU +General Public License, version 2. +""" + +import discord +import discord.ext.commands as commands + +import random +import json +import aiohttp + +class StudyCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.gs = bot.get_cog("GlobalSettings") + self.lastq = dict() + + @commands.command(name="rq", aliases=['randomq']) + async def _random_question(self, ctx, level: str = None): + '''Gets a random question from the Technician, General, and/or Extra question pools.''' + tech_pool = 'E2_2018' + gen_pool = 'E3_2019' + extra_pool = 'E4_2016' + + with ctx.typing(): + selected_pool = None + try: + level = level.lower() + except AttributeError: # no level given (it's None) + pass + + if level in ['t', 'technician', 'tech']: + selected_pool = tech_pool + + if level in ['g', 'gen', 'general']: + selected_pool = gen_pool + + if level in ['e', 'ae', 'extra']: + selected_pool = extra_pool + + if (level is None) or (level == 'all'): # no pool given or user wants all, so pick a random pool and use that + selected_pool = random.choice([tech_pool, gen_pool, extra_pool]) + if (level is not None) and (selected_pool is None): # unrecognized pool given by user + await ctx.send('The question pool you gave was unrecognized. ' + + 'There are many ways to call up certain question pools - try ?rq t, g, or e. ' + + '(Note that only the US question pools are available).') + return + + async with aiohttp.ClientSession() as session: + async with session.get(f'https://hamstudy.org/pools/{selected_pool}') as resp: + if resp.status != 200: + return await ctx.send('Could not load questions...') + pool = json.loads(await resp.read())['pool'] + + # Select a question + pool_section = random.choice(pool)['sections'] + pool_questions = random.choice(pool_section)['questions'] + question = random.choice(pool_questions) + + embed = discord.Embed(title=question['id'], colour=self.gs.colours.good) + embed = embed.add_field(name='Question:', value=question['text'], inline=False) + embed = embed.add_field(name='Answers:', value= + '**A:** ' + question['answers']['A'] + + '\n**B:** ' + question['answers']['B'] + + '\n**C:** ' + question['answers']['C'] + + '\n**D:** ' + question['answers']['D'], inline=False) + embed = embed.add_field(name='Answer:', value='Type _?rqa_ for answer', inline=False) + if 'image' in question: + image_url = f'https://hamstudy.org/_1330011/images/{selected_pool.split("_",1)[1]}/{question["image"]}' + embed = embed.set_image(url=image_url) + self.lastq[ctx.message.channel.id] = (question['id'], question['answer']) + await ctx.send(embed=embed) + + @commands.command(name="rqa") + async def _q_answer(self, ctx, ans: str = None): + '''Returns the answer to question last asked (Optional argument: your answer).''' + with ctx.typing(): + correct_ans = self.lastq[ctx.message.channel.id][1] + q_num = self.lastq[ctx.message.channel.id][0] + if ans is not None: + ans = ans.upper() + if ans == correct_ans: + result = f'Correct! The answer to {q_num} was **{correct_ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.good) + else: + result = f'Incorrect. The answer to {q_num} was **{correct_ans}**, not **{ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.bad) + else: + result = f'The correct answer to {q_num} was **{correct_ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.neutral) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(StudyCog(bot)) diff --git a/main.py b/main.py index ca6e8ea..ddf4358 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,9 @@ import options as opt import keys -# --- Global settings --- +# --- Settings --- + +exit_code = 1 # The default exit code. ?shutdown and ?restart will change it accordingly (fail-safe) debug_mode = opt.debug # Separate assignement in-case we define an override (ternary operator goes here) @@ -43,6 +45,34 @@ bot = commands.Bot(command_prefix=opt.prefix, description=info.description, help # --- Commands --- +@bot.command(name="restart") +async def _restart_bot(ctx): + """Restarts the bot.""" + global exit_code + if ctx.author.id in opt.owners_uids: + await ctx.message.add_reaction("✅") + exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted. + await bot.logout() + else: + try: + await ctx.message.add_reaction("❌") + except: + return + +@bot.command(name="shutdown") +async def _shutdown_bot(ctx): + """Shuts down the bot.""" + global exit_code + if ctx.author.id in opt.owners_uids: + await ctx.message.add_reaction("✅") + exit_code = 0 # Signals to the wrapper script that the bot should not be restarted. + await bot.logout() + else: + try: + await ctx.message.add_reaction("❌") + except: + return + # --- Events --- @@ -71,6 +101,8 @@ bot.load_extension("cogs.basecog") bot.load_extension("cogs.morsecog") bot.load_extension("cogs.funcog") bot.load_extension("cogs.hamcog") +bot.load_extension("cogs.imagecog") +bot.load_extension("cogs.studycog") _ensure_activity.start() @@ -92,3 +124,12 @@ except ConnectionResetError as ex: # More generic connection reset error if debug_mode: raise raise SystemExit("ConnectionResetError: {}".format(ex)) + + +# --- Exit --- +# Codes for the wrapper shell script: +# 0 - Clean exit, don't restart +# 1 - Error exit, [restarting is up to the shell script] +# 42 - Clean exit, do restart + +raise SystemExit(exit_code) diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..a48c23e --- /dev/null +++ b/run.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# A wrapper script for painless discord bots. +# v1.0.0 +# Copyright (c) 2019 0x5c +# Released under the terms of the MIT license. +# Part of: +# https://github.com/0x5c/quick-bot-no-pain + + +# If $BOTENV is not defined, default to 'botenv' +if [[ -z ${BOTENV+x} ]]; then + BOTENV='botenv' +fi + +# Argument handling # ? TODO: Argument passing ? +if [[ $1 == '--pass-errors' ]]; then + _PASS_ERRORS=1 +fi + + +# A function called when the bot exits to decide what to do +code_handling() { + case $err in + 0) + echo "$_message: exiting" + exit 0 # The bot whishes to stay alone. + ;; + 42) + echo "$_message: restarting" + return # The bot whishes to be restarted (returns to the loop). + ;; + *) + if [[ $_PASS_ERRORS -eq 0 ]]; then # The bot crashed and: + echo "$_message: restarting" + return # ...we should return to the loop to restart it. + else + echo "$_message: exiting (--pass-errors)" + exit $err # ...we should just exit and pass the code to our parent (probably a daemon/service manager). + fi + ;; + esac +} + + +echo "$0: Starting bot..." + +# The loop +while true; do + ./$BOTENV/bin/python3 main.py + err=$? + _message="$0: The bot exited with [$err]" + code_handling +done