Skip to content

Commit

Permalink
Merge branch 'main' into py-types
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo authored Feb 16, 2024
2 parents e6abf30 + a4941d9 commit bc246ed
Showing 1 changed file with 58 additions and 50 deletions.
108 changes: 58 additions & 50 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import math
import re
import warnings
from typing import Optional

# Constants
Expand Down Expand Up @@ -219,66 +220,73 @@ async def background_task() -> None:
last_refresh = 0.0
refresh_seconds = 60 # refresh gamelist every x seconds
while True:
sleep_time = refresh_seconds - (time.time() - last_refresh)
if sleep_time > 0.0:
await asyncio.sleep(sleep_time)
last_refresh = time.time()

# Call the external program and get the output
proc = await asyncio.create_subprocess_shell(
'./devilutionx-gamelist',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)

stdout, stderr = await proc.communicate()
output = stdout.decode()
if not output:
continue

# Load the output as a JSON list
games: List[Dict[str, Any]] = json.loads(output)

ct = datetime.datetime.now()
print('[' + str(ct) + '] Refreshing game list - ' + str(len(games)) + ' games')

for game in games:
if any_player_name_is_invalid(game['players']) or any_player_name_contains_a_banned_word(game['players']):
continue
try:
sleep_time = refresh_seconds - (time.time() - last_refresh)
if sleep_time > 0:
await asyncio.sleep(sleep_time)
last_refresh = time.time()

key = game['id'].upper()
if key in game_list:
game_list[key]['players'] = game['players']
game_list[key]['last_seen'] = time.time()
# Call the external program and get the output
proc = await asyncio.create_subprocess_shell(
'./devilutionx-gamelist',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)

try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), 30)
except TimeoutError:
proc.terminate()
continue
output = stdout.decode()
if not output:
continue

game_list[key] = game
game_list[key]['first_seen'] = time.time()
game_list[key]['last_seen'] = time.time()
# Load the output as a JSON list
games = json.loads(output)

ended_games: List[str] = []
for key, game in game_list.items():
if time.time() - game['last_seen'] < gameTTL:
continue
ended_games.append(key)
await end_game_message(key)
ct = datetime.datetime.now()
print('[' + str(ct) + '] Refreshing game list - ' + str(len(games)) + ' games')

for key in ended_games:
del game_list[key]
for game in games:
if any_player_name_is_invalid(game['players']) or any_player_name_contains_a_banned_word(game['players']):
continue

if len(ended_games) != 0:
await remove_game_messages(list(game_list.keys()))
key = game['id'].upper()
if key in game_list:
game_list[key]['players'] = game['players']
game_list[key]['last_seen'] = time.time()
continue

for gameId in game_list.keys():
await update_game_message(gameId)
game_list[key] = game
game_list[key]['first_seen'] = time.time()
game_list[key]['last_seen'] = time.time()

ended_games = []
for key, game in game_list.items():
if time.time() - game['last_seen'] < gameTTL:
continue
ended_games.append(key)
await end_game_message(key)

for key in ended_games:
del game_list[key]

if (current_online == len(game_list)) or len(ended_games) != 0:
continue
if len(ended_games) != 0:
await remove_game_messages(game_list.keys())

for gameId in game_list.keys():
await update_game_message(gameId)

if (current_online == len(game_list)) or len(ended_games) != 0:
continue

current_online = len(game_list)
await update_status_message()
current_online = len(game_list)
await update_status_message()

activity = discord.Activity(name='Games online: '+str(current_online), type=discord.ActivityType.watching)
await client.change_presence(activity=activity)
activity = discord.Activity(name='Games online: '+str(current_online), type=discord.ActivityType.watching)
await client.change_presence(activity=activity)
except discord.errors.DiscordServerError as server_error:
warnings.warn(repr(server_error))


@client.event
Expand Down

0 comments on commit bc246ed

Please sign in to comment.