internal async ValueTask <bool> ProcessCommandsAsync(IGatewayUserMessage message, CachedMessageGuildChannel channel) { // We check if the message is suitable for execution. // By default excludes bot messages. try { if (!await CheckMessageAsync(message).ConfigureAwait(false)) { return(false); } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while executing the check message callback."); return(false); } // We get the prefixes from the prefix provider. IEnumerable <IPrefix> prefixes; try { prefixes = await Prefixes.GetPrefixesAsync(message).ConfigureAwait(false); if (prefixes == null) { return(false); } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while getting the prefixes."); return(false); } // We try to find a prefix in the message. IPrefix foundPrefix = null; string output = null; try { foreach (var prefix in prefixes) { if (prefix == null) { continue; } if (prefix.TryFind(message, out output)) { foundPrefix = prefix; break; } } if (foundPrefix == null) { return(false); } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while finding the prefixes in the message."); return(false); } // We create a command context for Qmmands. DiscordCommandContext context; try { context = CreateCommandContext(foundPrefix, output, message, channel); } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while creating the command context."); return(false); } // We check the before execution callback, by default returns true. try { if (!await BeforeExecutedAsync(context).ConfigureAwait(false)) { return(false); } } catch (Exception ex) { await DisposeContextAsync(context).ConfigureAwait(false); Logger.LogError(ex, "An exception occurred while executing the before executed callback."); return(false); } // We post the execution to the command queue. // See the Post() method in the default queue for more information. try { Queue.Post(context, context => context.Bot.ExecuteAsync(context)); return(true); } catch (Exception ex) { await DisposeContextAsync(context).ConfigureAwait(false); Logger.LogError(ex, "An exception occurred while posting the execution to the command queue."); return(false); } }
private async ValueTask MessageReceivedAsync(object sender, MessageReceivedEventArgs e) { await Task.Yield(); if (e.Message is not IGatewayUserMessage message) { return; } try { if (!await CheckMessageAsync(message).ConfigureAwait(false)) { return; } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while executing the check message callback."); return; } IEnumerable <IPrefix> prefixes; try { prefixes = await Prefixes.GetPrefixesAsync(message).ConfigureAwait(false); if (prefixes == null) { return; } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while getting the prefixes."); return; } IPrefix foundPrefix = null; string output = null; try { foreach (var prefix in prefixes) { if (prefix == null) { continue; } if (prefix.TryFind(message, out output)) { foundPrefix = prefix; break; } } if (foundPrefix == null) { return; } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while finding the prefixes in the message."); return; } DiscordCommandContext context; try { context = CreateCommandContext(foundPrefix, message, e.Channel); } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while creating the command context."); return; } try { if (!await BeforeExecutedAsync(context).ConfigureAwait(false)) { return; } } catch (Exception ex) { Logger.LogError(ex, "An exception occurred while executing the before executed callback."); return; } try { Queue.Post(output, context, static (input, context) => context.Bot.ExecuteAsync(input, context));