Skip to content

Commit

Permalink
Merge pull request #71 from GravityWolfNotAmused/players-command
Browse files Browse the repository at this point in the history
Players command
  • Loading branch information
GravityWolfNotAmused authored Oct 19, 2024
2 parents 7873c22 + 592fef3 commit a2146db
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions DiscordPlayerCountBot/Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Bot : LoggableClass
public readonly BotInformation Information;
public readonly Dictionary<DataProvider, IServerInformationProvider> DataProviders = new();
public readonly Dictionary<string, string> ApplicationTokens = new();
public string LastKnownStatus = string.Empty;

public Bot(BotInformation info, Dictionary<string, string> applicationTokens, IServiceProvider services)
{
Expand All @@ -25,10 +26,52 @@ public Bot(BotInformation info, Dictionary<string, string> applicationTokens, IS
HandlerTimeout = null
});

DiscordClient.Ready += DiscordClient_Ready;
DiscordClient.SlashCommandExecuted += DiscordClient_SlashCommandExecuted;


DataProviders = services.GetServices<IServerInformationProvider>()
.ToDictionary(value => value.GetRequiredProviderType());
}

private async Task DiscordClient_SlashCommandExecuted(SocketSlashCommand command)
{
// NOTE: I only have logic for one slash command, if I am going to add more functionality.
// I will want to create a way to register commands, so people can expand it.

if (string.IsNullOrEmpty(LastKnownStatus))
{
await command.RespondAsync("Bot does not have a status to display.");
return;
}

var embed = new EmbedBuilder
{
Title = $"Server: {Information.Name}",
Fields = new()
{
new()
{
Name = "Players",
Value = LastKnownStatus
}
}
};

await command.RespondAsync(embeds: new[] { embed.Build() }, ephemeral: true);
}

private async Task DiscordClient_Ready()
{
var globalCommand = new SlashCommandBuilder()
{
Name = "players",
Description = $"This will show the player count for the server: {Information.Name}"
};

await DiscordClient.CreateGlobalApplicationCommandAsync(globalCommand.Build());
}

public async Task StartAsync(bool shouldStart)
{
if (Information!.Address.Contains("hostname") || Information.Address.Contains("localhost"))
Expand Down Expand Up @@ -74,10 +117,10 @@ public async Task UpdateAsync()
return;
}

var gameStatus = serverInformation.ReplaceTagsWithValues(Information.StatusFormat, Information.UseNameAsLabel, Information.Name);
LastKnownStatus = serverInformation.ReplaceTagsWithValues(Information.StatusFormat, Information.UseNameAsLabel, Information.Name);

await DiscordClient.SetGameAsync(gameStatus, null, (ActivityType)activityInteger);
await DiscordClient.SetChannelName(Information.ChannelID, gameStatus);
await DiscordClient.SetGameAsync(LastKnownStatus, null, (ActivityType)activityInteger);
await DiscordClient.SetChannelName(Information.ChannelID, LastKnownStatus);
}
}
}

0 comments on commit a2146db

Please sign in to comment.