public Task Commands(DiscordClient c, MessageCreateEventArgs e) { _ = Task.Run(async() => { if (e.Author.IsBot || string.IsNullOrEmpty(e.Message.Content)) { return; } CommandsNextExtension cnext = c.GetCommandsNext(); string prefix = _prefixCache.RetrievePrefix(e.Guild?.Id); int prefixLength = e.Channel.IsPrivate ? 0 : // No prefix in DMs, else try to get the string prefix length. // e.MentionedUsers.Any(u => u.Id == c.CurrentUser.Id) ? e.Message.GetMentionPrefixLength(c.CurrentUser) : e.Message.GetStringPrefixLength(prefix); if (prefixLength is - 1) { return; } string commandString = e.Message.Content.Substring(prefixLength); Command?command = cnext.FindCommand(commandString, out string arguments); if (command is null) { _logger.LogWarning($"Command not found: {e.Message.Content}"); return; } CommandContext context = cnext.CreateContext(e.Message, prefix, command, arguments); await cnext.ExecuteCommandAsync(context).ConfigureAwait(false); }); return(Task.CompletedTask); }
private Task Event_MessageCreated(DiscordClient d, MessageCreateEventArgs e) { d.Logger.LogDebug(BotEventId, "Event_MessageCreated."); CommandsNextExtension cnext = d.GetCommandsNext(); DiscordMessage msg = e.Message; // Check if message has valid prefix. // json file loaded... int cmdStart = msg.GetStringPrefixLength("!"); string gId = e.Guild.Id.ToString(); List <string> prefixes = BotSettings.GuildSettings["default"].Prefixes; // Check to see if the guild has settings. if (BotSettings.GuildSettings.ContainsKey(gId)) { // see if the guild wants global prefixes. if (BotSettings.GuildSettings[gId].UseGlobalPrefix) { prefixes = prefixes.Concat(BotSettings.GuildSettings[gId].Prefixes).ToList(); } else { prefixes = BotSettings.GuildSettings[gId].Prefixes; } } // check each prefix. break on the one that is evoked. foreach (string item in prefixes) { cmdStart = msg.GetStringPrefixLength(item); if (cmdStart != -1) { break; } } // we didn't find a command prefix... Break. if (cmdStart == -1) { return(Task.CompletedTask); } // Retrieve prefix. var prefix = msg.Content.Substring(0, cmdStart); var cmdString = msg.Content.Substring(cmdStart); // Retrieve full command string. var command = cnext.FindCommand(cmdString, out var args); if (command == null) { return(Task.CompletedTask); } var ctx = cnext.CreateContext(msg, prefix, command, args); Task.Run(async() => await cnext.ExecuteCommandAsync(ctx)); return(Task.CompletedTask); }
// TODO: Update to save guild config state. This will run as is, but will not hold any saved data between sessions. public async Task MessageReceivedAsync(CommandsNextExtension cnext, DiscordMessage msg, CancellationToken cancellationToken = new CancellationToken()) { try { cancellationToken.ThrowIfCancellationRequested(); if (this._commands is null) { return; } var model = cnext.Services.GetRequiredService <ShatterDatabaseContext>(); var guildConfig = await model.Configs.FindAsync(msg.Channel.GuildId); if (guildConfig is null) { guildConfig = new GuildConfig { GuildId = msg.Channel.GuildId.Value, Prefix = this._config.Prefix }; model.Configs.Add(guildConfig); await model.SaveChangesAsync(); } cancellationToken.ThrowIfCancellationRequested(); int prefixPos = await PrefixResolver(msg, guildConfig); if (prefixPos == -1) { return; // Prefix is wrong, dont respond to this message. } var prefix = msg.Content.Substring(0, prefixPos); string commandString = msg.Content.Replace(prefix, string.Empty); var command = cnext.FindCommand(commandString, out string args); cancellationToken.ThrowIfCancellationRequested(); if (command is null) { // Looks like that command does not exsist! await CommandResponder.RespondCommandNotFoundAsync(msg.Channel, prefix); } else { // We found a command, lets deal with it. if (guildConfig.DisabledCommands.Contains(command.Name)) { return; // Command is disabled. Dont do a thing. } var moduleAttribute = command.CustomAttributes.FirstOrDefault(x => x is ExecutionModuleAttribute); if (moduleAttribute != default) { var m = moduleAttribute as ExecutionModuleAttribute; if (m is not null && m.GroupName != "config" && !guildConfig.ActivatedCommands.Contains(command.Name) && guildConfig.DisabledModules.Contains(m.GroupName)) { await CommandResponder.RespondCommandDisabledAsync(msg.Channel, prefix); return; // Command is disabled, dont do a thing. } } var ctx = cnext.CreateContext(msg, prefix, command, args); // We are done here, its up to CommandsNext now. cancellationToken.ThrowIfCancellationRequested(); await cnext.ExecuteCommandAsync(ctx); } } finally { if (!(DiscordBot.CommandsInProgress is null)) { if (DiscordBot.CommandsInProgress.TryRemove(this, out var taskData)) { taskData.Item2.Dispose(); taskData.Item1.Dispose(); } } } }