Moved some files around to make development easier. Modified docker-compose to create two images in order to run the bot and the delayed background task in separate images.

This commit is contained in:
Alex Huddleston 2020-03-25 23:37:47 -05:00
parent 1e7417d95b
commit b1c01fc454
7 changed files with 85 additions and 43 deletions

View file

@ -22,7 +22,6 @@ RUN adduser \
USER covidbot
WORKDIR /home/covidbot/
ENV PATH="${PATH}:/home/covidbot/.local/bin"
RUN echo "${PATH}"
COPY Pipfile Pipfile

33
Dockerfile_report Normal file
View file

@ -0,0 +1,33 @@
# Pull Python 3.8.1 on alpine
FROM python:3.8.1-alpine
RUN apk --no-cache add gcc \
musl-dev \
python3-dev
# Create a new user to run as and set the working directory
ENV USER=covidreport
ENV UID=991
ENV GID=991
RUN addgroup -g "${GID}" covidreport
RUN adduser \
--disabled-password \
--gecos "" \
--home "/home/covidreport" \
--ingroup "${USER}" \
--uid "${UID}" \
"${USER}"
USER covidreport
WORKDIR /home/covidreport/
ENV PATH="${PATH}:/home/covidreport/.local/bin"
COPY Pipfile Pipfile
RUN pip install pipenv
RUN pipenv install Pipfile
COPY covid_report.py covid_report.py
ENTRYPOINT [ "pipenv", "run", "python", "covid_report.py" ]

View file

@ -1,37 +1,10 @@
#!/bin/python
import discord
from discord.ext import commands
from lib.parse_data import update_data, get_covid_data, covid_db
from re import match
from datetime import datetime
from asyncio import sleep
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
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')
report_match = match(r'(!report )([a-zA-Z\-\_\. ]+)', message.content)
if(report_match):
with open('.keys/report_channel_id') as cid:
channel = self.get_channel(int(cid.readline().strip()))
if(report_match.group(2).upper() == 'KEYS'):
await channel.send(covid_db.keys())
else:
await channel.send(get_covid_data(report_match.group(2).upper()))
from lib.covidBot import covidBot
if(__name__ == '__main__'):
with open('.keys/bot_api.key') as key:
discord_api_key = key.readline().strip()
client = MyClient()
client = covidBot()
client.run(discord_api_key)

12
covid_report.py Normal file
View file

@ -0,0 +1,12 @@
#!/bin/python
from discord import Client
from lib.covid_report_lib import background_task
if(__name__ == '__main__'):
with open('.keys/bot_api.key') as key:
discord_api_key = key.readline().strip()
client = Client()
client.loop.create_task(background_task(client))
client.run(discord_api_key)

View file

@ -11,3 +11,13 @@ services:
volumes:
- ./lib:/home/covidbot/lib
- ./.keys:/home/covidbot/.keys
covidreport:
build:
context: .
dockerfile: Dockerfile_report
image: covidreport:latest
user: covidreport
container_name: covidreport_container_service
volumes:
- ./lib:/home/covidreport/lib
- ./.keys:/home/covidreport/.keys

23
lib/covidBot.py Normal file
View file

@ -0,0 +1,23 @@
from discord import Client
from re import match
from lib.parse_data import update_data, get_covid_data, covid_db
class covidBot(Client):
async def on_ready(self):
print('Logged on as', self.user)
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')
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()))

View file

@ -1,9 +1,9 @@
#!/bin/python
import discord
from discord.ext import commands
from parse_data import update_data, get_covid_data, covid_db
from re import match
try:
from parse_data import update_data, get_covid_data
except:
from lib.parse_data import update_data, get_covid_data
from datetime import datetime
from asyncio import sleep
@ -15,17 +15,9 @@ async def background_task(client):
while not client.is_closed():
current_hour_and_minute = [int(t) for t in str(
datetime.now().time()).split(':')[:-1]]
if(current_hour_and_minute[0] == 12 and (current_hour_and_minute[1] == 0)):
if(current_hour_and_minute[0] == 4 and (current_hour_and_minute[1] == 34)):
update_data()
await channel.send('Daily report from: https://www.worldometers.info/coronavirus/')
await channel.send(get_covid_data('USA'))
await channel.send(get_covid_data('TOTAL'))
await sleep(60)
if(__name__ == '__main__'):
with open('.keys/bot_api.key') as key:
discord_api_key = key.readline().strip()
client = discord.Client()
client.loop.create_task(background_task(client))
client.run(discord_api_key)