Skip to content

Commit

Permalink
Merge pull request #94 from winglessraven/dev
Browse files Browse the repository at this point in the history
Add presence check to update on schedule if incorrect
  • Loading branch information
winglessraven authored May 4, 2024
2 parents 678b763 + ceae5e0 commit 0e9a4f0
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions DiscordBotPlugin/PluginMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class PluginMain : AMPPlugin
private List<PlayerPlayTime> playerPlayTimes = new List<PlayerPlayTime>();
private List<String> consoleOutput = new List<String>();
private SocketGuildUser currentUser;
private UserStatus userStatus;

//game specific variables
private string valheimJoinCode;
Expand Down Expand Up @@ -796,10 +795,21 @@ public void ApplicationStateChange(object sender, ApplicationStateChangeEventArg

public async void UpdatePresence(object sender, ApplicationStateChangeEventArgs args, bool force = false)
{
if(_settings.MainSettings.BotActive && (args == null || args.PreviousState != args.NextState || force))
if(_settings.MainSettings.BotActive && (args == null || args.PreviousState != args.NextState || force) && _client.ConnectionState == ConnectionState.Connected)
{
try
{

string currentActivity = _client.Activity?.Name ?? "";
if(currentActivity != "")
{
var customStatus = _client.Activity as CustomStatusGame;
if(customStatus != null)
{
currentActivity = customStatus.State;
}
}
UserStatus currentStatus = _client.Status;
UserStatus status;

// Get the current user and max user count
Expand Down Expand Up @@ -831,22 +841,27 @@ public async void UpdatePresence(object sender, ApplicationStateChangeEventArgs
ClearAllPlayTimes();
}

if(status != userStatus)
if(status != currentStatus)
{
await _client.SetStatusAsync(status);
userStatus = status;
}

string presenceString = OnlineBotPresenceString(onlinePlayers, maximumPlayers);

// Set the presence/activity based on the server state
if (application.State == ApplicationState.Ready)
{
//await _client.SetGameAsync(OnlineBotPresenceString(onlinePlayers, maximumPlayers), null, ActivityType.CustomStatus);
await _client.SetActivityAsync(new CustomStatusGame(OnlineBotPresenceString(onlinePlayers, maximumPlayers)));
if(currentActivity != presenceString)
{
await _client.SetActivityAsync(new CustomStatusGame(OnlineBotPresenceString(onlinePlayers, maximumPlayers)));
}
}
else
{
//await _client.SetGameAsync(application.State.ToString(), null, ActivityType.Playing);
await _client.SetActivityAsync(new CustomStatusGame(application.State.ToString()));
if(presenceString != application.State.ToString())
{
await _client.SetActivityAsync(new CustomStatusGame(application.State.ToString()));
}
}
}
catch (System.Net.WebException exception)
Expand Down Expand Up @@ -890,6 +905,9 @@ public async Task SetStatus()
{
_ = GetServerInfo(true, null, false);
}

//change presence if required
UpdatePresence(null, null, true);
}
catch (System.Net.WebException exception)
{
Expand Down

0 comments on commit 0e9a4f0

Please sign in to comment.