Skip to content

Commit

Permalink
the bot's reconnect bug has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
RED1cat committed Apr 11, 2024
1 parent c6f157f commit e3620a4
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions SkyCoopDedicatedServer/DiscordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static class DiscordManager
{
private static string ConfigPath = AppDomain.CurrentDomain.BaseDirectory + @"/" + "botconfig";
private static string ConfigTemplate = "token=\ninfochannelid=\nfeedchannelid=\nlastmessageid=\ntimetoupdatemessage=5";
public static bool Connected = false;
private static bool Ready = false;
private static bool Connected = false;
private static DiscordSocketClient _client;
private static string Token;
private static ulong InfoChannelId;
Expand All @@ -37,7 +38,9 @@ public static async Task Init()

_client.Log += Log;


_client.Ready += _client_Ready;
_client.Connected += _client_Connected;
_client.Disconnected += _client_Disconnected;

await _client.LoginAsync(TokenType.Bot, Token);
Expand Down Expand Up @@ -87,7 +90,7 @@ private static bool LoadConfig()

public static void SaveConfig()
{
if (!Connected)
if (!Ready)
return;
try
{
Expand All @@ -113,7 +116,7 @@ public static void SaveConfig()

private static Task _client_Ready()
{
Connected = true;
Ready = true;

if (Program.ServerGettingError)
{
Expand All @@ -124,6 +127,13 @@ private static Task _client_Ready()
return Task.CompletedTask;
}

private static Task _client_Connected()
{
Connected = true;

return Task.CompletedTask;
}

private static Task _client_Disconnected(Exception arg)
{
Connected = false;
Expand All @@ -133,7 +143,7 @@ private static Task _client_Disconnected(Exception arg)

public static async Task ServerInfoUpdate(string serverName, int players, int maxPlayers, string serverIp, string serverVersion, bool recreate = false, bool online = true)
{
if (!Connected)
if (!Ready || !Connected)
return;

CurrentMinuteToUpdateMessage++;
Expand Down Expand Up @@ -180,7 +190,7 @@ public static async Task ServerInfoUpdate(string serverName, int players, int ma

public static async Task SendMessage(string Message)
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down Expand Up @@ -208,7 +218,7 @@ public static async Task SendMessage(string Message)

public static async Task PlayerJoined(string PlayerName, int Players)
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down Expand Up @@ -236,7 +246,7 @@ public static async Task PlayerJoined(string PlayerName, int Players)

public static async Task PlayerLeave(string PlayerName, int Players)
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down Expand Up @@ -264,7 +274,7 @@ public static async Task PlayerLeave(string PlayerName, int Players)

public static async Task TodayStats(string Stats)
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down Expand Up @@ -292,7 +302,7 @@ public static async Task TodayStats(string Stats)

public static async Task CrashSiteSpawn(string Text)
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand All @@ -319,7 +329,7 @@ public static async Task CrashSiteSpawn(string Text)
}
public static async Task CrashSiteFound()
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down Expand Up @@ -347,7 +357,7 @@ public static async Task CrashSiteFound()

public static async Task CrashSiteTimeOver()
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand All @@ -374,7 +384,7 @@ public static async Task CrashSiteTimeOver()
}
public static async Task ServerError()
{
if (!Connected)
if (!Ready || !Connected)
return;

if (await _client.GetChannelAsync(FeedChannelId) is IMessageChannel chnl) //get channel
Expand Down

0 comments on commit e3620a4

Please sign in to comment.