Browse Source

Add e-mail alerts for discord loss.

Stuart Johnson 1 year ago
parent
commit
69ec82deeb
3 changed files with 27 additions and 42 deletions
  1. 6 28
      .vs/TFA-Bot/xs/UserPrefs.xml
  2. 17 10
      TFA-Bot/Program.cs
  3. 4 4
      TFA-Bot/clsEmail.cs

+ 6 - 28
.vs/TFA-Bot/xs/UserPrefs.xml

@@ -1,6 +1,6 @@
 <Properties StartupConfiguration="{25609AB4-A77A-4B47-AAB8-B2DB39464A3F}|Default">
   <MonoDevelop.Ide.ItemProperties.TFA-Bot PreferredExecutionTarget="MonoDevelop.Default" />
-  <MonoDevelop.Ide.Workbench ActiveDocument="TFA-Bot/DataClasses/clsNode.cs">
+  <MonoDevelop.Ide.Workbench ActiveDocument="TFA-Bot Status">
     <Files>
       <File FileName="TFA-Bot/Spreadsheet/clsSpreadsheetReader.cs" Line="125" Column="1" />
       <File FileName="TFA-Bot/DiscordBot/Commands/clsHelp.cs" Line="24" Column="54" />
@@ -8,36 +8,14 @@
       <File FileName="TFA-Bot/DataClasses/clsNetwork.cs" Line="24" Column="21" />
       <File FileName="TFA-Bot/clsExtenstions.cs" Line="57" Column="30" />
       <File FileName="TFA-Bot/DiscordBot/Commands/clsListNodes.cs" Line="21" Column="65" />
-      <File FileName="TFA-Bot/Program.cs" Line="179" Column="22" />
+      <File FileName="TFA-Bot/Program.cs" Line="162" Column="84" />
       <File FileName="TFA-Bot/DataClasses/clsNodeGroup.cs" Line="73" Column="30" />
+      <File FileName="TFA-Bot/DataClasses/clsNode.cs" Line="97" Column="62" />
+      <File FileName="TFA-Bot/DiscordBot/clsBotClient.cs" Line="29" Column="30" />
+      <File FileName="TFA-Bot/clsEmail.cs" Line="96" Column="14" />
+      <File FileName="TFA-Bot/clsAlarm.cs" Line="107" Column="34" />
       <File FileName="TFA-Bot Status" />
-      <File FileName="TFA-Bot/DataClasses/clsNode.cs" Line="77" Column="21" />
     </Files>
-    <Pads>
-      <Pad Id="ProjectPad">
-        <State name="__root__">
-          <Node name="TFA-Bot" expanded="True">
-            <Node name="Solution Items" expanded="True" />
-            <Node name="TFA-Bot" expanded="True">
-              <Node name="Packages" expanded="True" />
-              <Node name="DataClasses" expanded="True">
-                <Node name="clsNode.cs" selected="True" />
-              </Node>
-              <Node name="Dialler" expanded="True" />
-              <Node name="DiscordBot" expanded="True">
-                <Node name="Commands" expanded="True" />
-              </Node>
-              <Node name="Git" expanded="True" />
-              <Node name="Spreadsheet" expanded="True" />
-              <Node name="Utils" expanded="True" />
-            </Node>
-          </Node>
-        </State>
-      </Pad>
-      <Pad Id="MonoDevelop.Debugger.WatchPad">
-        <State />
-      </Pad>
-    </Pads>
   </MonoDevelop.Ide.Workbench>
   <MonoDevelop.Ide.DebuggingService.PinnedWatches />
   <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />

+ 17 - 10
TFA-Bot/Program.cs

@@ -120,6 +120,7 @@ namespace TFABot
                 const int loopWait = 3000;
                 
                 DateTime LastBotStart = DateTime.UtcNow;
+                int BotErrorCounter = 0;
                 using (Bot = new clsBotClient(DiscordToken))
                 {
                     Bot.RunAsync();
@@ -156,19 +157,25 @@ namespace TFABot
                         }
                         
                        AlarmManager.Process();
-                       
-                       //Check the status of the Discord connection.  If it disconnects, it doesn't always restart.
-                       if (!Bot.LastHeartbeatd.HasValue || (DateTime.UtcNow - Bot.LastHeartbeatd.Value).TotalSeconds > 90)
-                       {
-                            if ( (DateTime.UtcNow - LastBotStart).TotalSeconds>120)
+
+                        //Check the status of the Discord connection.  If it disconnects, it doesn't always restart.
+                        if (!Bot.LastHeartbeatd.HasValue || (DateTime.UtcNow - Bot.LastHeartbeatd.Value).TotalSeconds > 90)
+                        {
+                            if ((DateTime.UtcNow - LastBotStart).TotalSeconds > 120)
                             {
-                                Console.WriteLine("Bot not connected. Restarting.....");
-                                Bot._client.DisconnectAsync();
-                                Thread.Sleep(1000);
+                                Console.WriteLine("Discord not connected. Restarting.....");
+                                BotErrorCounter++;
+                                if (BotErrorCounter == 2) clsEmail.EmailAlertList("Bot: Discord not connected.");
+                                Bot._client.Dispose();
                                 LastBotStart = DateTime.UtcNow;
-                                Bot._client.ConnectAsync();
+                                Bot = new clsBotClient(DiscordToken);
+                                Bot.RunAsync();
                             }
-                       }
+                        }
+                        else
+                        {
+                            BotErrorCounter = 0;
+                        }
                           
                        ApplicationHold.WaitOne(loopWait);
                     }

+ 4 - 4
TFA-Bot/clsEmail.cs

@@ -87,14 +87,14 @@ namespace TFABot
             
             return task;
         }
-        
-        
-        static public void EmailAlertList()
+
+
+        static public void EmailAlertList(String message = "")
         {
         
             if (!String.IsNullOrEmpty(SMTPHost))
             {
-                string alarmMessage = $"{Program.BotName} Alarm";
+                string alarmMessage = $"{Program.BotName} Alarm {message}";
                 foreach (var user in Program.UserList.Values.Where(x=>x.OnDuty && !String.IsNullOrEmpty(x.email)))
                 {
                     SendEmail(user.email,alarmMessage,alarmMessage);