public async Task Commands(string _name = null) { var name = _name?.TrimStart('!').ToLower(); var sb = new StringBuilder(); if (string.IsNullOrWhiteSpace(name)) { sb.AppendLine($"**List of commands** (for usage examples type **!commands** <***name of command***>"); sb.AppendLine(@"***Tip:*** You can send commands to me in a private conversation if you wish."); sb.AppendLine(); foreach (var command in _commands.Commands.OrderBy(x => x.Name)) { if (CommandHiddenAttribute.IsHidden(command.Module.Attributes, command.Attributes)) { continue; } var context = new SocketCommandContext(Context.Client, Context.Message); var precRoleRestricted = command.Preconditions.OfType <RoleRestrictedPreconditionAttribute>().FirstOrDefault(); if (precRoleRestricted != null && !(await precRoleRestricted.CheckPermissionsAsync(context, command, _serviceProvider)).IsSuccess) { continue; } sb.AppendLine($"● **!{command.Name}**" + (!string.IsNullOrWhiteSpace(command.Summary) ? $": {command.Summary}" : "")); var syntaxHelp = command.SyntaxHelp(); if (!string.IsNullOrWhiteSpace(syntaxHelp)) { sb.AppendLine($"Syntax: **!{command.Name}** {syntaxHelp}"); } } } else { var command = _commands.Commands.FirstOrDefault(x => name.Equals(x.Name, StringComparison.OrdinalIgnoreCase) || x.Aliases.Contains(name, StringComparer.OrdinalIgnoreCase)); if (command == null) { sb.AppendLine($"**The specified command does not exist!**"); } else { var context = new SocketCommandContext(Context.Client, Context.Message); var usageExamples = command.UsageExamples(); var precRoleRestricted = command.Preconditions.OfType <RoleRestrictedPreconditionAttribute>().FirstOrDefault(); if (precRoleRestricted != null && !(await precRoleRestricted.CheckPermissionsAsync(context, command, _serviceProvider)).IsSuccess) { sb.AppendLine($"**The specified command is only available to some roles and using a private channel.**"); } else if (usageExamples == null || usageExamples.Length <= 0) { sb.AppendLine($"**The specified command does not have any usage examples :(**"); } else { sb.AppendLine($"**Example usage of !{name.ToLower()}**"); foreach (var usageExample in usageExamples) { if (string.IsNullOrWhiteSpace(usageExample)) { continue; } sb.AppendLine($"● **!{command.Name}**" + (usageExample[0] == ':' ? "" : " ") + usageExample); } } } } await CommandHelper.SendPartitioned(Context.Channel, sb.ToString()); }
private async Task HandleCommandAsync(SocketMessage messageParam) { try { var message = messageParam as SocketUserMessage; if (message == null) { return; } var argPos = 0; if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_discord.CurrentUser, ref argPos))) { return; } var context = new SocketCommandContext(_discord, message); var result = _commands.Search(context, argPos); if (result.IsSuccess && result.Commands.Count > 0) { if (result.Commands.Count > 1) { Logging.Log( $"Multiple commands registered for '{message.Content.Substring(argPos)}'! Skipping!", GetType(), LogLevel.WARN); return; } var cm = result.Commands.First(); var iCommand = cm.Command; var iModule = iCommand.Module; var isHidden = CommandHiddenAttribute.IsHidden(iModule.Attributes, iCommand.Attributes); //check if command is allowed in this channel if (!(context.Channel is ISocketPrivateChannel) && _config.Discord.EnabledChannels?.Count > 0 && !_config.Discord.EnabledChannels.Contains(context.Channel.Name, StringComparer.OrdinalIgnoreCase)) { return; } var preconditions = await cm.CheckPreconditionsAsync(context, _serviceProvider); if (!preconditions.IsSuccess) { if (preconditions.ErrorReason?.Equals(RoleRestrictedPreconditionAttribute.CommandDisabledErrorString) == true) { return; } Logging.Log( $"Command precondition(s) failed [command name: {iCommand.Name}, preconditions error: {preconditions.ErrorReason}]", GetType(), LogLevel.DEBUG); await messageParam.Channel.SendMessageAsync("**The specified command is not available to your role or may not be used in this channel.**"); return; } var parseResult = await cm.ParseAsync(context, result, preconditions, _serviceProvider); if (!parseResult.IsSuccess) { Logging.Log( $"Command parsing failed [command name: {iCommand.Name}, parseResult error: {parseResult.ErrorReason}]", GetType(), LogLevel.DEBUG); var syntaxHelp = iCommand.Attributes.OfType <SyntaxHelpAttribute>().FirstOrDefault()?.SyntaxHelp; var name = iCommand.Attributes.OfType <CommandAttribute>().FirstOrDefault()?.Text; await messageParam.Channel.SendMessageAsync(string.Join(Environment.NewLine, new string[] { $"**My logic circuits cannot process this command! I am just a bot after all... :(**", !string.IsNullOrWhiteSpace(syntaxHelp) ? $"Help me by following this syntax: **!{name}** {syntaxHelp}" : null }.Where(x => x != null))); return; } var commandResult = await cm.ExecuteAsync(context, parseResult, _serviceProvider); if (commandResult.IsSuccess) { if (isHidden) { return; } var sb = new StringBuilder(); sb.AppendLine($@"""!{message.Content.Substring(argPos)}"" command successful!"); Logging.Log(sb.ToString(), GetType(), LogLevel.INFO); } else { if (isHidden || preconditions.Error.HasValue && preconditions.Error.Value == CommandError.UnmetPrecondition) { if (!isHidden) { Logging.Log( $"Command unmet precondition(s) [command name: {iCommand.Name}, preconditions error: {preconditions.ErrorReason}]", GetType(), LogLevel.DEBUG); } return; } //if there is an exception log all information pertaining to it so that we can possibly fix it in the future var exception = commandResult is ExecuteResult ? ((ExecuteResult)commandResult).Exception : null; if (exception != null) { var errorMessage = $@"""!{message.Content.Substring(argPos)}"" command error..."; Logging.LogException(errorMessage, exception, GetType(), LogLevel.ERROR, ExceptionLevel.Unhandled); } Logging.Log( $"Command execution failed [command name: {iCommand.Name}, command error: {commandResult.ErrorReason}]", GetType(), LogLevel.DEBUG); } } //var result = await _commands.ExecuteAsync(context, argPos, _serviceProvider); } catch (Exception ex) { Logging.Log( $"Command handler unhandled exception [message: {ex.Message}]", GetType(), LogLevel.DEBUG); } }