Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 90f8dfe

Browse files
committed
Improve slash command module updates
1 parent b3fa461 commit 90f8dfe

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

TabletBot.Discord/SlashCommands/SlashCommandModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public async Task HandleInteraction(SocketInteraction interaction)
2727

2828
protected abstract IEnumerable<SlashCommand> GetSlashCommands();
2929

30-
public void BuildCommandHandlers()
30+
public IList<SlashCommand> BuildCommandHandlers()
3131
{
32-
CommandHandlers = new List<SlashCommand>(GetSlashCommands());
32+
return CommandHandlers = new List<SlashCommand>(GetSlashCommands());
3333
}
3434
}
3535
}

TabletBot.Discord/Watchers/Commands/SlashCommandInteractionWatcher.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
@@ -82,17 +83,25 @@ private async Task ApplyCommandPermissions(DiscordSocketClient client, IEnumerab
8283

8384
private async Task UpdateModule(SlashCommandModule module)
8485
{
85-
module.BuildCommandHandlers();
86+
var applicationCommands = await _client.Rest.GetGuildApplicationCommands(Settings.Current.GuildID);
87+
var handlers = module.BuildCommandHandlers();
8688

87-
foreach (var command in await _client.Rest.GetGuildApplicationCommands(Settings.Current.GuildID))
88-
await command.DeleteAsync();
89+
var commandsToUpdate = from handler in handlers
90+
let command = applicationCommands.FirstOrDefault(c => c.Name == handler.Name)
91+
where command != null
92+
select (handler, command);
8993

94+
Log.Write("SlashCmd", $"Updating slash commands...");
9095
var moderatorCommands = new List<RestGuildCommand>();
91-
foreach (var command in module.CommandHandlers)
96+
foreach (var updateable in commandsToUpdate)
9297
{
93-
var guildCommand = await _client.Rest.CreateGuildCommand(command.Build(), Settings.Current.GuildID);
98+
await updateable.command.DeleteAsync();
99+
100+
var guildCommand = await _client.Rest.CreateGuildCommand(updateable.handler.Build(), Settings.Current.GuildID);
94101
if (guildCommand.IsDefaultPermission == false)
95102
moderatorCommands.Add(guildCommand);
103+
104+
Log.Write("SlashCmd", $"Successfully updated slash command {updateable.handler.Name}.");
96105
}
97106

98107
await ApplyCommandPermissions(_client, moderatorCommands);

0 commit comments

Comments
 (0)