From 208229effdb6f2248fa6229cec013a7b53bcec01 Mon Sep 17 00:00:00 2001
From: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com>
Date: Fri, 9 Jul 2021 17:08:09 +0530
Subject: [PATCH 1/3] feat: handle Discord start and close

---
 load.py | 56 +++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/load.py b/load.py
index 298ced8..d042e83 100644
--- a/load.py
+++ b/load.py
@@ -112,22 +112,10 @@ def prefs_changed(cmdr, is_beta):
 
 
 def plugin_start3(plugin_dir):
-    plugin_path = join(dirname(plugin_dir), plugin_name)
-    this.app = dsdk.Discord(CLIENT_ID, dsdk.CreateFlags.default, plugin_path)
-    this.activity_manager = this.app.get_activity_manager()
-    this.activity = dsdk.Activity()
-
-    this.call_back_thread = threading.Thread(target=run_callbacks)
-    this.call_back_thread.setDaemon(True)
-    this.call_back_thread.start()
-
-    this.presence_state = _('Connecting CMDR Interface')
-    this.presence_details = ''
-    this.time_start = time.time()
-
-    this.disablePresence = None
-
-    update_presence()
+    this.plugin_dir = plugin_dir
+    this.discord_thread = threading.Thread(target=check_run, args=(plugin_dir,))
+    this.discord_thread.setDaemon(True)
+    this.discord_thread.start()
     return 'DiscordPresence'
 
 
@@ -212,7 +200,37 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
         update_presence()
 
 
-def run_callbacks():
-    while True:
+def check_run(plugin_dir):
+    plugin_path = join(dirname(plugin_dir), plugin_name)
+    retry = True
+    while retry:
         time.sleep(1 / 10)
-        this.app.run_callbacks()
+        try:
+            this.app = dsdk.Discord(CLIENT_ID, dsdk.CreateFlags.no_require_discord, plugin_path)
+            retry = False
+        except Exception as ex:
+            print(ex)
+
+    this.activity_manager = this.app.get_activity_manager()
+    this.activity = dsdk.Activity()
+
+    this.call_back_thread = threading.Thread(target=run_callbacks)
+    this.call_back_thread.setDaemon(True)
+    this.call_back_thread.start()
+    this.presence_state = _('Connecting CMDR Interface')
+    this.presence_details = ''
+    this.time_start = time.time()
+
+    this.disablePresence = None
+
+    update_presence()
+
+
+def run_callbacks():
+    try:
+        while True:
+            time.sleep(1 / 10)
+            this.app.run_callbacks()
+    except Exception as ex:
+        print(ex)
+        check_run(this.plugin_dir)

From 34c93b13d5fb0c3f8a1ebb53181f7a66b1618920 Mon Sep 17 00:00:00 2001
From: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com>
Date: Fri, 9 Jul 2021 18:27:56 +0530
Subject: [PATCH 2/3] feat: handle transaction error during discord class
 rebuild

---
 load.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/load.py b/load.py
index d042e83..601c92a 100644
--- a/load.py
+++ b/load.py
@@ -50,6 +50,8 @@ def callback(result):
     logger.info(f'Callback: {result}')
     if result == dsdk.Result.ok:
         logger.info("Successfully set the activity!")
+    elif result == dsdk.Result.transaction_aborted:
+        logger.warning(f'Transaction aborted due to SDK shutting down: {result}')
     else:
         logger.error(f'Error in callback: {result}')
         raise Exception(result)
@@ -208,8 +210,8 @@ def check_run(plugin_dir):
         try:
             this.app = dsdk.Discord(CLIENT_ID, dsdk.CreateFlags.no_require_discord, plugin_path)
             retry = False
-        except Exception as ex:
-            print(ex)
+        except Exception:
+            pass
 
     this.activity_manager = this.app.get_activity_manager()
     this.activity = dsdk.Activity()
@@ -231,6 +233,5 @@ def run_callbacks():
         while True:
             time.sleep(1 / 10)
             this.app.run_callbacks()
-    except Exception as ex:
-        print(ex)
+    except Exception:
         check_run(this.plugin_dir)

From ad98e522603e113a294acd05f9ceb3b9125e60e3 Mon Sep 17 00:00:00 2001
From: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com>
Date: Fri, 9 Jul 2021 18:33:38 +0530
Subject: [PATCH 3/3] feat: bump version

---
 load.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/load.py b/load.py
index 601c92a..9d75218 100644
--- a/load.py
+++ b/load.py
@@ -37,7 +37,7 @@
 
 CLIENT_ID = 386149818227097610
 
-VERSION = '3.0.0'
+VERSION = '3.1.0'
 
 # Add global var for Planet name (landing + around)
 planet = '<Hidden>'