Changed bot format to match bot mode in the Discord python API. Created a top X summary report command.

This commit is contained in:
Alex Huddleston 2020-03-26 21:12:14 -05:00
parent d826ebcd06
commit 391def59c2
5 changed files with 62 additions and 34 deletions

View file

@ -1,10 +1,11 @@
#!/bin/python
from lib.covidBot import covidBot
from discord.ext import commands
from lib.covidBot import add_commands
if(__name__ == '__main__'):
bot = commands.Bot(command_prefix='!')
with open('.keys/bot_api.key') as key:
discord_api_key = key.readline().strip()
client = covidBot()
client.run(discord_api_key)
add_commands(bot)
bot.run(discord_api_key)

View file

@ -1,23 +1,33 @@
from discord import Client
from discord.ext import commands
from re import match
from lib.parse_data import update_data, get_covid_data, covid_db
from lib.parse_data import update_data, get_covid_data, covid_db, get_top_data
class covidBot(Client):
async def on_ready(self):
print('Logged on as', self.user)
@commands.command()
async def ping(ctx):
await ctx.send('pong')
async def on_message(self, message):
# don't respond to ourselves
if(message.author == self.user):
return
if(message.content == '!ping'):
await message.channel.send('pong')
@commands.command()
async def report(ctx, arg):
if(arg):
if(arg == 'KEYS'):
await ctx.send(covid_db.keys())
else:
await ctx.send(get_covid_data(arg.upper()))
report_match = match(r'(!report )([a-zA-Z\-\_\. ]+)', message.content)
if(report_match):
if(report_match.group(2).upper() == 'KEYS'):
await message.channel.send(covid_db.keys())
else:
await message.channel.send(get_covid_data(report_match.group(2).upper()))
@commands.command()
async def top(ctx, arg='5'):
try:
num = int(arg)
except Exception as e:
await ctx.send(f'{arg} isn\'t a number.')
return
await ctx.send(get_top_data(num))
def add_commands(bot):
bot.add_command(ping)
bot.add_command(report)
bot.add_command(top)

View file

@ -1,6 +1,3 @@
#!/usr/bin/python
class covidData():
def __init__(self, selection='', import_data=[]):
super().__init__()
@ -36,4 +33,4 @@ class covidData():
output = ''
for key in self.data.keys():
output += f'{key}: {self.data[key]}\n'
return output
return output

View file

@ -1,9 +1,5 @@
#!/bin/python
try:
from parse_data import update_data, get_covid_data
except:
from lib.parse_data import update_data, get_covid_data
from lib.parse_data import update_data, get_covid_data
from datetime import datetime
from pytz import timezone
from asyncio import sleep

View file

@ -2,14 +2,14 @@
import requests
from bs4 import BeautifulSoup
try:
from covidData import covidData
except:
from lib.covidData import covidData
from lib.covidData import covidData
# temporary database
covid_db = {}
# San Antonio url
sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus'
def update_data():
data_html = requests.get('https://www.worldometers.info/coronavirus/')
@ -32,5 +32,29 @@ def get_covid_data(selection):
return covid_db[selection].get_formatted_data()
def get_top_data(number):
if(not covid_db):
update_data()
covid_db_dict = {}
for data in covid_db.keys():
try:
if(covid_db[data].data['Selection'] and covid_db[data].data['Total Cases'] and not covid_db[data].data['Selection'] == 'Total'):
covid_db_dict[covid_db[data].data['Selection'].upper()] = int(covid_db[data].data['Total Cases'].replace(',', ''))
except Exception as e:
print(e)
return f'{e} tests'
covid_db_dict_sorted = {key: value for key, value in sorted(covid_db_dict.items(), key=lambda item: item[1], reverse=True)}
top_covid_data = [(covid_db[selection].data['Selection'], covid_db[selection].data['Total Cases']) for selection in list(covid_db_dict_sorted.keys())[:number]]
output = ''
counter = number + 1
for data in top_covid_data:
output += f'# {counter - number}\n'
output += f'{data[0]}: {data[1]}'
if(not counter == number):
output += '\n\n'
counter += 1
return output
if(__name__ == '__main__'):
print(get_covid_data())