Skip to content

Instantly share code, notes, and snippets.

@foxbot
Created July 13, 2018 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxbot/ba8a82452a9f3a313904e6e00dbabb76 to your computer and use it in GitHub Desktop.
Save foxbot/ba8a82452a9f3a313904e6e00dbabb76 to your computer and use it in GitHub Desktop.
world cup score reporting bot for discord
import aiohttp
import asyncio
import discord
import os
CHANNEL_ID = 381870553235193857
class State:
def __init__(self, token):
self.discord = discord.http.HTTPClient()
self.discord.token = token
self.session = aiohttp.ClientSession()
self.home = {}
self.away = {}
self.events = set()
async def run(self):
while True:
await self.fetch_update()
await asyncio.sleep(15)
async def fetch_update(self):
resp = await self.session.get('https://worldcup.sfg.io/matches/today')
data = await resp.json()
match = data[0]
self.home = match['home_team']
self.away = match['away_team']
async def process_event(event):
id = event['id']
if id in self.events:
return
self.events.add(id)
print('handling event %s' % id)
if event['type_of_event'] == 'goal':
msg = '**%s** %s - %s **%s**\n*%s*' % (
self.home['code'], self.home['goals'],
self.away['goals'], self.away['code'],
event['player']
)
print('sending GOAL')
await self.discord.send_message(CHANNEL_ID, msg)
if not 'home_team_events' in match or not 'away_team_events' in match:
return
for event in match['home_team_events']:
await process_event(event)
for event in match['away_team_events']:
await process_event(event)
token = os.getenv('TOKEN')
if not token:
print('no token')
os.abort()
token = 'Bot %s' % token
state = State(token)
print('hi')
loop = asyncio.get_event_loop()
loop.run_until_complete(state.run())
loop.close()
print('bye')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment