private async Task SetupCommands() { foreach (SocketApplicationCommand sac in await _client.GetGlobalApplicationCommandsAsync()) { //await sac.DeleteAsync(); if (sac.Name == "randomimage") { Log(LogSeverity.Error, $"RandomImage command is already set up"); return; } } SlashCommandBuilder scb = new SlashCommandBuilder(); scb.WithName("randomimage"); scb.WithDescription("Post a random image"); List <ChannelType> channelTypes = new List <ChannelType>(); channelTypes.Add(ChannelType.Text); channelTypes.Add(ChannelType.DM); scb.AddOption("channel", ApplicationCommandOptionType.Channel, "Channel source", isRequired: true, channelTypes: channelTypes); try { Log(LogSeverity.Error, $"RandomImage command set up"); await _client.CreateGlobalApplicationCommandAsync(scb.Build()); } catch (Exception e) { Log(LogSeverity.Error, $"Error setting up slash command: {e.Message}"); } }
private async Task Ready() { if (!_commandManager.AreCommandsLoaded) { _ = Task.Run(async() => { var debugGuild = _commandManager.GetDebugGuild(client); var commands = _commandManager.LoadCommands(); foreach (var cmd in commands) { if (debugGuild != null) { await debugGuild.CreateApplicationCommandAsync(cmd.SlashCommand); } else { await client.CreateGlobalApplicationCommandAsync(cmd.SlashCommand); } } if (debugGuild != null) { await debugGuild.BulkOverwriteApplicationCommandAsync(commands.Select(x => x.SlashCommand).ToArray()); } else { await client.BulkOverwriteGlobalApplicationCommandsAsync(commands.Select(x => x.SlashCommand).ToArray()); } }); } }
/// <summary> /// Handles registering the global slash commands /// </summary> /// <returns></returns> public async Task HandleGlobalCommands() { var globalCommands = _commands .Commands .Where(t => t.Value != null && t.Value.Guilds?.Length == 0) .Select(t => (t.Value.Builder ?? new SlashCommandBuilder()).Build()) .Concat( _commands .ReflectionCommands .Where(t => t.Value.Attribute is not GuildCommandAttribute) .Select(t => t.Value.Builder.Build()) ) .ToList(); var currentGlobalCommands = await _client.GetGlobalApplicationCommandsAsync(); foreach (var cmd in currentGlobalCommands) { var exists = globalCommands.FirstOrDefault(t => t.Name.IsSpecified && t.Name.Value == cmd.Name); if (exists == null) { await cmd.DeleteAsync(); continue; } globalCommands.Remove(exists); var validate = Validate(exists, cmd); if (validate) { continue; } await _client.CreateGlobalApplicationCommandAsync(exists); _logger.LogInformation($"Global Application Command Update: {exists.Name}"); } foreach (var cmd in globalCommands) { await _client.CreateGlobalApplicationCommandAsync(cmd); _logger.LogInformation($"Global Application Command Created: {cmd.Name}"); } }