Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DiscordServerError in main loop #23

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 54 additions & 50 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import math
import re
import warnings
from typing import Optional

# Constants
Expand Down Expand Up @@ -209,66 +210,69 @@ async def background_task():
last_refresh = 0
refresh_seconds = 60 # refresh gamelist every x seconds
while True:
sleep_time = refresh_seconds - (time.time() - last_refresh)
if sleep_time > 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 = 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']):
try:
sleep_time = refresh_seconds - (time.time() - last_refresh)
if sleep_time > 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

key = game['id'].upper()
if key in game_list:
game_list[key]['players'] = game['players']
game_list[key]['last_seen'] = time.time()
continue
# Load the output as a JSON list
games = json.loads(output)

game_list[key] = game
game_list[key]['first_seen'] = time.time()
game_list[key]['last_seen'] = time.time()
ct = datetime.datetime.now()
print('[' + str(ct) + '] Refreshing game list - ' + str(len(games)) + ' games')

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 game in games:
if any_player_name_is_invalid(game['players']) or any_player_name_contains_a_banned_word(game['players']):
continue

for key in ended_games:
del game_list[key]
key = game['id'].upper()
if key in game_list:
game_list[key]['players'] = game['players']
game_list[key]['last_seen'] = time.time()
continue

if len(ended_games) != 0:
await remove_game_messages(game_list.keys())
game_list[key] = game
game_list[key]['first_seen'] = time.time()
game_list[key]['last_seen'] = time.time()

for gameId in game_list.keys():
await update_game_message(gameId)
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)

if (current_online == len(game_list)) or len(ended_games) != 0:
continue
for key in ended_games:
del game_list[key]

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
Loading