From 14598b41693d6223cd9108b79b7607feb5de7576 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 15:52:16 -0400 Subject: [PATCH 01/10] Embeds --- discord_bot.py | 147 ++++++++++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 64 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index f968c39..51f6ad0 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -24,80 +24,99 @@ def escape_discord_formatting_characters(text: str): return re.sub(r'([-\\*_#|~:@[\]()<>`])', r'\\\1', text) -def format_game(game): - global gameTTL - ended = time.time() - game['last_seen'] >= gameTTL - if ended: - text = '~~' + game['id'].upper() + '~~' - else: - text = '**' + game['id'].upper() + '**' - match game['type']: - case 'DRTL': - text += ' <:diabloico:760201452957335552>' - case 'DSHR': - text += ' <:diabloico:760201452957335552> (spawn)' - case 'HRTL': - text += ' <:hellfire:766901810580815932>' - case 'HSHR': - text += ' <:hellfire:766901810580815932> (spawn)' - case 'IRON': - text += ' Ironman' - case 'MEMD': - text += ' <:one_ring:1061898681504251954>' - case 'DRDX': - text += ' <:diabloico:760201452957335552> X' - case 'DWKD': - text += ' <:mod_wkd:1097321063077122068> modDiablo' - case 'HWKD': - text += ' <:mod_wkd:1097321063077122068> modHellfire' - case _: - text += ' ' + game['type'] - - text += ' ' + game['version'] - - match game['tick_rate']: - case 20: - text += '' - case 30: - text += ' Fast' - case 40: - text += ' Faster' - case 50: - text += ' Fastest' - case _: - text += ' speed: ' + str(game['tick_rate']) - - match game['difficulty']: - case 0: - text += ' Normal' - case 1: - text += ' Nightmare' - case 2: - text += ' Hell' - +async def format_game(game): + embed = { + "type": "rich", + "title": game['id'].upper(), + "description": "", + "color": 0x00ff00, + "fields": [], + "thumbnail": { + "url": "", + "height": 0, + "width": 0 + }, + "author": { + "name": f"DevilutionX {game['version']}" + }, + "footer": { + "text": f"Duration: {format_time_delta(round((time.time() - game['first_seen']) / 60))}" + } + } + + # Checking if the game is ended to adjust border color + if time.time() - game['last_seen'] >= gameTTL: + embed["color"] = 0xff0000 # red for closed games + + # Players + embed["fields"].append({ + "name": "Players", + "value": ', '.join([name for name in game['players']]), + "inline": True + }) + + # Difficulty + difficulties = ["Normal", "Nightmare", "Hell"] + embed["fields"].append({ + "name": "Difficulty", + "value": difficulties[game['difficulty']], + "inline": True + }) + + # Game Speed + tick_rate_mapping = { + 20: "Normal", + 30: "Fast", + 40: "Faster", + 50: "Fastest" + } + speed = tick_rate_mapping.get(game['tick_rate'], f"Speed: {game['tick_rate']}") + embed["fields"].append({ + "name": "Game Speed", + "value": speed, + "inline": True + }) + + # Game Options + diablo_game_codes = {'DRTL', 'DSHR', 'IRON', 'MEMD', 'DWKD', 'LTDR', 'LTDS'} attributes = [] if game['run_in_town']: attributes.append('Run in Town') if game['full_quests']: attributes.append('Quests') - if game['theo_quest'] and game['type'] != 'DRTL': + if game['theo_quest'] and game['type'] not in diablo_game_codes: attributes.append('Theo Quest') - if game['cow_quest'] and game['type'] != 'DRTL': + if game['cow_quest'] and game['type'] not in diablo_game_codes: attributes.append('Cow Quest') if game['friendly_fire']: attributes.append('Friendly Fire') - if len(attributes) != 0: - text += ' (' - text += ', '.join(attributes) - text += ')' - - text += '\nPlayers: **' + '**, **'.join([escape_discord_formatting_characters(name) for name in game['players']]) + '**' - text += '\nStarted: ' - if ended: - text += '\nEnded after: `' + format_time_delta(round((time.time() - game['first_seen']) / 60)) + '`' - - return text + if attributes: + embed["fields"].append({ + "name": "Game Options", + "value": ', '.join(attributes), + "inline": True + }) + + # Thumbnail based on game type + game_type_icons = { + 'DRTL': 'URL', # DevilutionX Diablo Retail + 'DSHR': 'URL', # DevilutionX Diablo Shareware + 'HRTL': 'URL', # DevilutionX Hellfire Retail + 'HSHR': 'URL', # DevilutionX Hellfire Shareware + 'IRON': 'URL', # Ironman Diablo Retail (Sixcy) + 'MEMD': 'URL', # Middle Earth Diablo Retail (DakkJaniels) + 'DRDX': 'URL', # DiabloX Diablo Retail (ikonomov) + 'DWKD': 'URL', # wkdmod Diablo Retail (wkdgmr) + 'HWKD': 'URL', # wkdmod Hellfire Retail (wkdgmr) + 'LTDR': 'URL', # Lord of Terror Diablo Retail (kphoenix) + 'LTDS': 'URL', # Lord of Terror Diablo Shareware (kphoenix) + 'LTHR': 'URL', # Lord of Terror Hellfire Retail (kphoenix) + 'LTHS': 'URL', # Lord of Terror Hellfire Shareware (kphoenix) + } + embed["thumbnail"]["url"] = game_type_icons.get(game['type'], "") + + return embed async def update_status_message(): From ec76cd4fc3d5905689ad5a5d91fc033098824450 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 16:04:26 -0400 Subject: [PATCH 02/10] Fix --- discord_bot.py | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 51f6ad0..916b1e7 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -24,7 +24,7 @@ def escape_discord_formatting_characters(text: str): return re.sub(r'([-\\*_#|~:@[\]()<>`])', r'\\\1', text) -async def format_game(game): +def format_game(game): embed = { "type": "rich", "title": game['id'].upper(), @@ -119,35 +119,17 @@ async def format_game(game): return embed -async def update_status_message(): - global current_online +async def update_game_message(game_id): global global_channel - global global_online_list_message - if global_online_list_message is not None: + embed_data = format_game(game_list[game_id]) + embed = discord.Embed.from_dict(embed_data) + if 'message' in game_list[game_id]: try: - await global_online_list_message.delete() + await game_list[game_id]['message'].edit(embed=embed) except discord.errors.NotFound: pass - global_online_list_message = None - text = 'There are currently **' + str(current_online) + '** public games.' - if current_online == 1: - text = 'There is currently **' + str(current_online) + '** public game.' - assert isinstance(global_channel, discord.TextChannel) - global_online_list_message = await global_channel.send(text) - - -async def update_game_message(game_id): - global global_channel - text = format_game(game_list[game_id]) - if 'message' in game_list[game_id]: - if game_list[game_id]['message'].content != text: - try: - await game_list[game_id]['message'].edit(content=text) - except discord.errors.NotFound: - pass return - assert isinstance(global_channel, discord.TextChannel) - game_list[game_id]['message'] = await global_channel.send(text) + game_list[game_id]['message'] = await global_channel.send(embed=embed) def format_time_delta(minutes): @@ -282,7 +264,6 @@ async def background_task(): continue 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) From f164efee44d2199d0c4fda218e74e0c01de7e0f5 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 20:42:36 -0400 Subject: [PATCH 03/10] Add "Game Closed" to replace game id --- discord_bot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/discord_bot.py b/discord_bot.py index 916b1e7..24d0566 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -47,6 +47,7 @@ def format_game(game): # Checking if the game is ended to adjust border color if time.time() - game['last_seen'] >= gameTTL: embed["color"] = 0xff0000 # red for closed games + embed["title"] = "Game Closed" # Players embed["fields"].append({ From 680d5c41f26afa32e1cec9e96d3a672fac5cdc70 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 21:03:24 -0400 Subject: [PATCH 04/10] Fix closed games --- discord_bot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/discord_bot.py b/discord_bot.py index 24d0566..0af08e0 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -156,8 +156,10 @@ def format_time_delta(minutes): async def end_game_message(game_id): if 'message' in game_list[game_id]: + embed_data = format_game(game_list[game_id]) + embed = discord.Embed.from_dict(embed_data) try: - await game_list[game_id]['message'].edit(content=format_game(game_list[game_id])) + await game_list[game_id]['message'].edit(embed=embed) except discord.errors.NotFound: pass From 140f0a1819650bd6990bb0dc0517a6c2e060bef7 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 21:26:32 -0400 Subject: [PATCH 05/10] Remove Players and Options as inline --- discord_bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 0af08e0..0f76191 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -53,7 +53,7 @@ def format_game(game): embed["fields"].append({ "name": "Players", "value": ', '.join([name for name in game['players']]), - "inline": True + "inline": False }) # Difficulty @@ -73,7 +73,7 @@ def format_game(game): } speed = tick_rate_mapping.get(game['tick_rate'], f"Speed: {game['tick_rate']}") embed["fields"].append({ - "name": "Game Speed", + "name": "Speed", "value": speed, "inline": True }) @@ -96,7 +96,7 @@ def format_game(game): embed["fields"].append({ "name": "Game Options", "value": ', '.join(attributes), - "inline": True + "inline": False }) # Thumbnail based on game type From ce431451c85db1853fa5d9d21291586a9688ce5e Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 21:58:07 -0400 Subject: [PATCH 06/10] Update game icons --- discord_bot.py | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 0f76191..749d472 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -99,21 +99,41 @@ def format_game(game): "inline": False }) + diablo_retail_url = 'https://user-images.githubusercontent.com/68359262/272125642-d3363701-9b6e-4eff-b43e-11b9a0b58813.png' + diablo_shareware_url = 'https://user-images.githubusercontent.com/68359262/272125649-076eb58c-f587-4b16-822f-7d9b56559c69.png' + hellfire_retail_url = 'https://user-images.githubusercontent.com/68359262/272125658-3fc00693-e6dc-4273-81e8-803dbf9f5d93.png' + hellfire_shareware_url = 'https://user-images.githubusercontent.com/68359262/272125665-17805298-1400-49aa-a6a3-ac80ba5767ee.png' # Thumbnail based on game type game_type_icons = { - 'DRTL': 'URL', # DevilutionX Diablo Retail - 'DSHR': 'URL', # DevilutionX Diablo Shareware - 'HRTL': 'URL', # DevilutionX Hellfire Retail - 'HSHR': 'URL', # DevilutionX Hellfire Shareware - 'IRON': 'URL', # Ironman Diablo Retail (Sixcy) - 'MEMD': 'URL', # Middle Earth Diablo Retail (DakkJaniels) - 'DRDX': 'URL', # DiabloX Diablo Retail (ikonomov) - 'DWKD': 'URL', # wkdmod Diablo Retail (wkdgmr) - 'HWKD': 'URL', # wkdmod Hellfire Retail (wkdgmr) - 'LTDR': 'URL', # Lord of Terror Diablo Retail (kphoenix) - 'LTDS': 'URL', # Lord of Terror Diablo Shareware (kphoenix) - 'LTHR': 'URL', # Lord of Terror Hellfire Retail (kphoenix) - 'LTHS': 'URL', # Lord of Terror Hellfire Shareware (kphoenix) + 'DRTL': diablo_retail_url, # DevilutionX Diablo Retail + 'DSHR': diablo_shareware_url, # DevilutionX Diablo Shareware + 'HRTL': hellfire_retail_url, # DevilutionX Hellfire Retail + 'HSHR': hellfire_shareware_url, # DevilutionX Hellfire Shareware + + 'IRON': diablo_retail_url, # Ironman Diablo Retail (Sixcy) + # MISSING: Ironman Diablo Shareware (Sixcy) + # MISSING: Ironman Hellfire Retail (Sixcy) + # MISSING: Ironman Hellfire Shareware (Sixcy) + + 'MEMD': diablo_retail_url, # Middle Earth Diablo Retail (DakkJaniels) + # MISSING: Middle Earth Diablo Shareware (DakkJaniels) + # MISSING: Middle Earth Hellfire Retail (DakkJaniels) + # MISSING: Middle Earth Hellfire Shareware (DakkJaniels) + + 'DRDX': diablo_retail_url, # DiabloX Diablo Retail (ikonomov) + # MISSING: DiabloX Diablo Shareware (ikonomov) + # MISSING: DiabloX Hellfire Retail (ikonomov) + # MISSING: DiabloX Hellfire Shareware (ikonomov) + + 'DWKD': diablo_retail_url, # wkdmod Diablo Retail (wkdgmr) + # MISSING: wkdmod Diablo Shareware (wkdgmr) + 'HWKD': hellfire_retail_url, # wkdmod Hellfire Retail (wkdgmr) + # MISSING: wkdmod Hellfire Shareware (wkdgmr) + + 'LTDR': diablo_retail_url, # Lord of Terror Diablo Retail (kphoenix) + 'LTDS': diablo_shareware_url, # Lord of Terror Diablo Shareware (kphoenix) + 'LTHR': hellfire_retail_url, # Lord of Terror Hellfire Retail (kphoenix) + 'LTHS': hellfire_shareware_url, # Lord of Terror Hellfire Shareware (kphoenix) } embed["thumbnail"]["url"] = game_type_icons.get(game['type'], "") From 7ff2fcbeb963fa38233cadc8b27154bb2548761e Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 22:09:17 -0400 Subject: [PATCH 07/10] Modify game title based on 4 letter code --- discord_bot.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 749d472..a3f4788 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -36,9 +36,6 @@ def format_game(game): "height": 0, "width": 0 }, - "author": { - "name": f"DevilutionX {game['version']}" - }, "footer": { "text": f"Duration: {format_time_delta(round((time.time() - game['first_seen']) / 60))}" } @@ -49,6 +46,26 @@ def format_game(game): embed["color"] = 0xff0000 # red for closed games embed["title"] = "Game Closed" + # Mapping 4-letter code to game title + game_titles = { + 'DRTL': 'DevilutionX', + 'DSHR': 'DevilutionX', + 'HRTL': 'DevilutionX', + 'HSHR': 'DevilutionX', + 'IRON': 'Ironman', + 'MEMD': 'Middle Earth', + 'DRDX': 'DiabloX', + 'DWKD': 'wkdmod', + 'HWKD': 'wkdmod', + 'LTDR': 'Lord of Terror', + 'LTDS': 'Lord of Terror', + 'LTHR': 'Lord of Terror', + 'LTHS': 'Lord of Terror' + } + + # Setting the author field to game title with version + embed["author"]["name"] = f"{game_titles.get(game['type'], 'Unknown Game')} {game['version']}" + # Players embed["fields"].append({ "name": "Players", From 4bcd1ec4bcd68f77ee719d8e9c88cc434af42679 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 22:12:19 -0400 Subject: [PATCH 08/10] Update missing game ids --- discord_bot.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index a3f4788..15f2c84 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -133,9 +133,6 @@ def format_game(game): # MISSING: Ironman Hellfire Shareware (Sixcy) 'MEMD': diablo_retail_url, # Middle Earth Diablo Retail (DakkJaniels) - # MISSING: Middle Earth Diablo Shareware (DakkJaniels) - # MISSING: Middle Earth Hellfire Retail (DakkJaniels) - # MISSING: Middle Earth Hellfire Shareware (DakkJaniels) 'DRDX': diablo_retail_url, # DiabloX Diablo Retail (ikonomov) # MISSING: DiabloX Diablo Shareware (ikonomov) From 74ac6b5a82af5be82e0654241431a2665207e7d2 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 22:17:55 -0400 Subject: [PATCH 09/10] Handle undefined difficulty and game speed --- discord_bot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 15f2c84..44770eb 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -75,9 +75,11 @@ def format_game(game): # Difficulty difficulties = ["Normal", "Nightmare", "Hell"] + difficulty_value = difficulties[game['difficulty']] if 0 <= game['difficulty'] < len( + difficulties) else "Unknown" embed["fields"].append({ "name": "Difficulty", - "value": difficulties[game['difficulty']], + "value": difficulty_value, "inline": True }) @@ -88,7 +90,7 @@ def format_game(game): 40: "Faster", 50: "Fastest" } - speed = tick_rate_mapping.get(game['tick_rate'], f"Speed: {game['tick_rate']}") + speed = tick_rate_mapping.get(game['tick_rate'], f"Unknown ({game['tick_rate']})") embed["fields"].append({ "name": "Speed", "value": speed, From 18dfca7a453410a99680af70c407a2da4eede3e7 Mon Sep 17 00:00:00 2001 From: KPhoenix Date: Mon, 2 Oct 2023 22:21:27 -0400 Subject: [PATCH 10/10] Change unknown to custom for tick rate --- discord_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord_bot.py b/discord_bot.py index 44770eb..0811f08 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -90,7 +90,7 @@ def format_game(game): 40: "Faster", 50: "Fastest" } - speed = tick_rate_mapping.get(game['tick_rate'], f"Unknown ({game['tick_rate']})") + speed = tick_rate_mapping.get(game['tick_rate'], f"Custom ({game['tick_rate']})") embed["fields"].append({ "name": "Speed", "value": speed,