Skip to content

Commit

Permalink
add ability to disable slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyGalbreath committed Oct 23, 2023
1 parent aebc5dd commit 8f261bd
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 183 deletions.
2 changes: 1 addition & 1 deletion src/Bot.cs
Expand Up @@ -108,7 +108,7 @@ public class Bot {

client.MessageReceived += DiscordMessageReceived;

commandHandler.Register(chatChannel!.Guild);
commandHandler.RegisterAllCommands(chatChannel!.Guild);
}

if (Config.ConsoleChannel != 0) {
Expand Down
14 changes: 9 additions & 5 deletions src/Command/Command.cs
Expand Up @@ -6,17 +6,21 @@
namespace DiscordBot.Command;

public abstract class Command {
protected readonly Bot Bot;

public readonly string Name;
public readonly string Description;

public readonly List<Option> Options = new();

protected Command(string name, string description) {
protected Command(Bot bot, string name) {
Bot = bot;
Name = name;
Description = description;
}

public abstract Task HandleCommand(Bot bot, SocketSlashCommand command);
public abstract bool IsEnabled();

public abstract string GetHelp();

public abstract Task HandleCommand(SocketSlashCommand command);

public class Option {
public required string Name;
Expand Down
34 changes: 26 additions & 8 deletions src/Command/CommandHandler.cs
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using DiscordBot.Config;
using DiscordBot.Extensions;
using Vintagestory.API.Util;

Expand All @@ -25,36 +26,53 @@ public class CommandHandler {
commands.Add(command.Name.ToLower(), command);
}

public void Register(SocketGuild guild) {
public async void RegisterAllCommands(SocketGuild guild) {
IReadOnlyCollection<SocketApplicationCommand> registeredCommands = guild.GetApplicationCommandsAsync().Result;

foreach (Command command in commands.Values) {
if (registeredCommands.Contains(command)) {
SocketApplicationCommand? registeredCommand = registeredCommands.Get(command);
if (registeredCommand != null) {
if (!command.IsEnabled()) {
await registeredCommand.DeleteAsync();
}

continue;
}

if (!command.IsEnabled()) {
continue;
}

SlashCommandBuilder builder = new SlashCommandBuilder()
.WithName(command.Name)
.WithDescription(command.Description);
.WithDescription(command.GetHelp());

foreach (Command.Option option in command.Options) {
builder.AddOption(option.Name, option.Type, option.Description, option.IsRequired);
}

guild.CreateApplicationCommandAsync(builder.Build());
await guild.CreateApplicationCommandAsync(builder.Build());
}
}

public async Task HandleSlashCommands(SocketSlashCommand command) {
try {
await commands!.Get(command.Data.Name.ToLower())!.HandleCommand(Bot, command);
Command? registeredCommand = commands!.Get(command.Data.Name.ToLower());

if (registeredCommand?.IsEnabled() ?? false) {
await registeredCommand.HandleCommand(command);
return;
}

await command.DeleteOriginalResponseAsync();
}
catch (Exception e) {
Bot.Logger.Error(e);
CommandError error = Bot.Config.Commands.Error;
await command.RespondAsync(embed: new EmbedBuilder()
.WithTitle(Bot.Config.Commands.Error.Title)
.WithDescription(Bot.Config.Commands.Error.Description)
.WithColor(Bot.Config.Commands.Error.Color)
.WithTitle(error.Title)
.WithDescription(error.Description)
.WithColor(error.Color)
.WithCurrentTimestamp()
.Build(),
ephemeral: true
Expand Down
22 changes: 16 additions & 6 deletions src/Command/NextTempStormCommand.cs
Expand Up @@ -8,17 +8,27 @@
namespace DiscordBot.Command;

public class NextTempStormCommand : Command {
public NextTempStormCommand(Bot bot) : base("nexttempstorm", bot.Config.Commands.NextTempStorm.Help) { }
private readonly ConfigNextTempStormCommand config;

public override Task HandleCommand(Bot bot, SocketSlashCommand command) {
BotConfig.ConfigCommands.ConfigNextTempStormCommand config = bot.Config.Commands.NextTempStorm;
public NextTempStormCommand(Bot bot) : base(bot, "nexttempstorm") {
config = bot.Config.Commands.NextTempStorm;
}

public override bool IsEnabled() {
return config.Enabled;
}

public override string GetHelp() {
return config.Help;
}

public override Task HandleCommand(SocketSlashCommand command) {
EmbedBuilder embed = new();

TemporalStormRunTimeData data = bot.Api.ModLoader.GetModSystem<SystemTemporalStability>().StormData;
TemporalStormRunTimeData data = Bot.Api.ModLoader.GetModSystem<SystemTemporalStability>().StormData;

if (data.nowStormActive) {
double days = data.stormActiveTotalDays - bot.Api.World.Calendar.TotalDays;
double days = data.stormActiveTotalDays - Bot.Api.World.Calendar.TotalDays;
double hours = days * 24f;
double minutes = (hours - (int)hours) * 60f;

Expand All @@ -35,7 +45,7 @@ public class NextTempStormCommand : Command {
}
}
else {
double days = data.nextStormTotalDays - bot.Api.World.Calendar.TotalDays;
double days = data.nextStormTotalDays - Bot.Api.World.Calendar.TotalDays;
double hours = (days - (int)days) * 24f;
double minutes = (hours - (int)hours) * 60f;

Expand Down
20 changes: 15 additions & 5 deletions src/Command/PlayersCommand.cs
Expand Up @@ -10,7 +10,11 @@
namespace DiscordBot.Command;

public class PlayersCommand : Command {
public PlayersCommand(Bot bot) : base("players", bot.Config.Commands.Players.Help) {
private readonly ConfigPlayersCommand config;

public PlayersCommand(Bot bot) : base(bot, "players") {
config = bot.Config.Commands.Players;

Options.Add(new Option {
Name = "ping",
Type = ApplicationCommandOptionType.Boolean,
Expand All @@ -19,17 +23,23 @@ public class PlayersCommand : Command {
});
}

public override async Task HandleCommand(Bot bot, SocketSlashCommand command) {
BotConfig.ConfigCommands.ConfigPlayersCommand config = bot.Config.Commands.Players;
public override bool IsEnabled() {
return config.Enabled;
}

public override string GetHelp() {
return config.Help;
}

public override async Task HandleCommand(SocketSlashCommand command) {
bool ping = command.Data.Options.Get<bool?>("ping") ?? false;
var list = (IServerPlayer[])bot.Api.World.AllOnlinePlayers;
var list = (IServerPlayer[])Bot.Api.World.AllOnlinePlayers;

EmbedBuilder? embed = new EmbedBuilder()
.WithColor(config.Color);

if (config.Title is { Length: > 0 }) {
embed.WithTitle(config.Title.Format(list.Length, bot.Api.Server.Config.MaxClients));
embed.WithTitle(config.Title.Format(list.Length, Bot.Api.Server.Config.MaxClients));
}

if (config.PlayersFields && list.Length > 0) {
Expand Down
18 changes: 14 additions & 4 deletions src/Command/TimeCommand.cs
Expand Up @@ -10,15 +10,25 @@
namespace DiscordBot.Command;

public class TimeCommand : Command {
public TimeCommand(Bot bot) : base("time", bot.Config.Commands.Time.Help) { }
private readonly ConfigTimeCommand config;

public override Task HandleCommand(Bot bot, SocketSlashCommand command) {
BotConfig.ConfigCommands.ConfigTimeCommand config = bot.Config.Commands.Time;
public TimeCommand(Bot bot) : base(bot, "time") {
config = bot.Config.Commands.Time;
}

public override bool IsEnabled() {
return config.Enabled;
}

public override string GetHelp() {
return config.Help;
}

public override Task HandleCommand(SocketSlashCommand command) {
EmbedBuilder? embed = new EmbedBuilder()
.WithColor(config.Color);

GameCalendar calendar = (GameCalendar)bot.Api.World.Calendar;
GameCalendar calendar = (GameCalendar)Bot.Api.World.Calendar;

int day = calendar.DayOfMonth;
int month = calendar.Month;
Expand Down

0 comments on commit 8f261bd

Please sign in to comment.