Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Players command #71

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading