void RegisterModules() { // Help module has to be loaded first (TODO: Remove special case in favor of dependencies) var help = new HelpModule(this); Modules.Add(help); var moduleTypes = new List <Type>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in assembly.GetTypes().Where(t => typeof(Module).IsAssignableFrom(t) && !t.IsAbstract && t != typeof(HelpModule))) { moduleTypes.Add(type); } } // TODO: Sort modules by dependencies here moduleTypes.ToList().ForEach(t => { var module = (Module)Activator.CreateInstance(t, this); Modules.Add(module); }); ModulesLoaded(this, null); }
public CommandHandler(DiscordSocketClient c) { Client = c; Commands = new CommandService(); Services = new ServiceCollection() .AddSingleton(Client) .AddSingleton(Commands) .BuildServiceProvider(); Commands.AddModulesAsync(Assembly.GetEntryAssembly()); Directory.CreateDirectory("Data/Logs"); logFilePath = String.Format("Data/Logs/{0:yyyy-MM-dd}.log", DateTime.Now); if (!File.Exists(logFilePath)) { Console.WriteLine($"Creating log file {logFilePath}..."); File.Create(logFilePath); Task.Delay(2000); Console.WriteLine("Done."); } HelpModule.Create(Commands); Client.MessageReceived += HandleCommands; }
public MainWindow() { InitializeComponent(); this.projectModule = new ProjectModule(this); this.editModule = new EditModule(this); this.helpModule = new HelpModule(this); this.prepareEventHandler(); }
private async Task OnCommandExecutedAsync(Optional <CommandInfo> command, ICommandContext context, IResult result) { if (!result.IsSuccess) { if (result.Error == CommandError.BadArgCount) { await HelpModule.DisplayCommandHelpAsync(command.Value, context); } else if (result.Error == CommandError.UnknownCommand) { int prefixLength = _dataService.Configuration.Prefix.Length; string customCommandStr = context.Message.Content.Substring(prefixLength); string[] splitCommand = customCommandStr.Split(' '); string customCommandName = splitCommand[0]; string[] args = splitCommand.Length > 1 ? splitCommand[1..(splitCommand.Length - 1)] : new string[0];
private async Task HandleMessageAsync(SocketMessage incomingMessage) { if (!(incomingMessage is SocketUserMessage message) || incomingMessage.Author is SocketWebhookUser) { return; // Ignore web-hooks or system messages } if (incomingMessage.Author.IsBot) { return; } // Ignore bots var argPos = 0; if (!message.HasStringPrefix(_config.LittleBigBot.Prefix, ref argPos) && !message.HasMentionPrefix(_client.CurrentUser, ref argPos)) { return; } var context = new LittleBigBotExecutionContext(message, _services); try { var result = await _commandService.ExecuteAsync( message.Content.Substring(argPos) /* Remove prefix from string */, context, _services); if (result.IsSuccessful) { return; } Command command; switch (result) { case CommandNotFoundResult _: await context.Message.AddReactionAsync(UnknownCommandReaction); return; case ChecksFailedResult cfr: command = cfr.Command; await context.Channel.SendMessageAsync( $"The following check{(cfr.FailedChecks.Count == 0 ? "" : "s")} failed, so I couldn't execute the command: \n" + string.Join("\n", cfr.FailedChecks.Select(a => $"- {a.Error}"))); break; case ParseFailedResult pfr: command = pfr.Command; if (pfr.ParseFailure == ParseFailure.TooFewArguments) { await context.Channel.SendMessageAsync( $"Sorry, but you didn't supply enough information for this command! Here is the command listing for `{pfr.Command.Aliases.First()}`:", false, HelpModule.CreateCommandEmbed(pfr.Command, context)); break; } await context.Channel.SendMessageAsync( $"Parsing of your input failed: {pfr.ParseFailure.Humanize()}."); break; case TypeParserFailedResult tpfr: command = tpfr.Parameter.Command; await context.Channel.SendMessageAsync( $"Sorry, but \"{tpfr.Value}\" is not a valid form of {tpfr.Parameter.GetFriendlyName()}! Here is the command listing for `{tpfr.Parameter.Command.Aliases.First()}`:", false, HelpModule.CreateCommandEmbed(tpfr.Parameter.Command, context)); break; case ExecutionFailedResult _: return; case CommandOnCooldownResult cdr: command = cdr.Command; var msg = new StringBuilder(); msg.AppendLine("This command is on cooldown!"); foreach (var cdv in cdr.Cooldowns) { msg.AppendLine(); msg.AppendLine($"**Cooldown type:** {cdv.Cooldown.BucketType.Cast<CooldownType>()}"); msg.AppendLine($"**Limit:** {cdv.Cooldown.Amount} requests per {cdv.Cooldown.Per:g}"); msg.AppendLine($"**Retry after:** {cdv.RetryAfter:g}"); } await context.Channel.SendMessageAsync(msg.ToString()); break; case CommandResult _: return; default: await context.Channel.SendMessageAsync($"Generic failure: {result}"); return; } LogCommandGeneralFailure(context, command, result as FailedResult); } catch (Exception) { // Ignored - caught through CommandErrored event! // Should (theoretically) never happen, but Qmmands is an early library and errors could occur. // I don't know though. } }
private static List <HelpModule> DefaultHelpModules() { List <HelpModule> helpModules = new List <HelpModule>(); HelpModule basic = new HelpModule { Group = "Basic", Modules = new List <string> { nameof(Basic), nameof(Misc) } }; helpModules.Add(basic); HelpModule utils = new HelpModule { Group = "Utils", Modules = new List <string> { nameof(Utils) } }; helpModules.Add(utils); HelpModule voting = new HelpModule { Group = "Voting", Modules = new List <string> { nameof(Voting) } }; helpModules.Add(voting); HelpModule account = new HelpModule { Group = "Account", Modules = new List <string> { nameof(AccountDataManagement), nameof(AccountUtils) } }; helpModules.Add(account); HelpModule fun = new HelpModule { Group = "Fun", Modules = new List <string> { nameof(GiphySearch), nameof(GoogleSearch), nameof(YoutubeSearch), nameof(WikipediaSearch), nameof(RandomPerson), nameof(TronaldDump), nameof(SteamUserUtils) } }; helpModules.Add(fun); HelpModule audio = new HelpModule { Group = "Audio", Modules = new List <string> { nameof(Music) } }; helpModules.Add(audio); return(helpModules); }