示例#1
0
 public ValueTask <IEnumerable <IPrefix> > GetPrefixesAsync(IGatewayUserMessage message)
 {
     return(ValueTask.FromResult <IEnumerable <IPrefix> >(new IPrefix[]
     {
         new StringPrefix(_config.GetValue <string>("prefix")),
         new MentionPrefix(_config.GetValue <ulong>("userId"))
     }));
 }
示例#2
0
 public override DiscordCommandContext CreateCommandContext(
     IPrefix prefix,
     string input,
     IGatewayUserMessage message,
     CachedTextChannel channel)
 {
     return(new MilkmanCommandContext(message, input, this, prefix));
 }
        public async ValueTask <IEnumerable <IPrefix> > GetPrefixesAsync(IGatewayUserMessage message)
        {
            if (message.GuildId is { } guildId)
            {
                return(await _guildSettings.GetGuildPrefixesAsync(guildId));
            }

            //DMs will use defaults
            return(_defaultGuildSettingsProvider.DefaultPrefixes);
        }
示例#4
0
        /// <inheritdoc/>
        public bool TryFind(IGatewayUserMessage message, out string output)
        {
            var contentSpan = message.Content.AsSpan();

            if (contentSpan.Length > 17 && contentSpan[0] == '<' && contentSpan[1] == '@')
            {
                var closingBracketIndex = contentSpan.IndexOf('>');
                if (closingBracketIndex != -1)
                {
                    var idSpan = contentSpan[2] == '!'
                        ? contentSpan.Slice(3, closingBracketIndex - 3)
                        : contentSpan.Slice(2, closingBracketIndex - 2);
                    if (Snowflake.TryParse(idSpan, out var id) && id == UserId)
                    {
                        output = new string(contentSpan.Slice(closingBracketIndex + 1));
                        return(true);
                    }
                }
            }

            output = null;
            return(false);
        }
示例#5
0
 public ValueTask <IEnumerable <IPrefix> > GetPrefixesAsync(IGatewayUserMessage message)
 => new(new[]
 /// <summary>
 ///     Checks if the received message is valid.
 ///     By default ensures the message author is not a bot.
 /// </summary>
 /// <param name="message"> The message to check. </param>
 /// <returns>
 ///     A <see cref="ValueTask{TResult}"/> where the result indicates whether the message is valid.
 /// </returns>
 protected virtual ValueTask <bool> CheckMessageAsync(IGatewayUserMessage message)
 => new(!message.Author.IsBot);
示例#7
0
 /// <inheritdoc/>
 public bool TryFind(IGatewayUserMessage message, out string output)
 => CommandUtilities.HasPrefix(message.Content, Value, Comparison, out output);
示例#8
0
        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);
            }
        }
 public MilkmanCommandContext(IGatewayUserMessage message, string input, Milkman bot, IPrefix prefix) : base(bot, prefix, input, message, message.GetChannel(), bot.Services)
     => (Bot, HttpClient) = (bot, bot.Services.GetRequiredService <HttpClient>());
示例#10
0
 public VCommandContext(VerificationBot bot, IPrefix prefix, IGatewayUserMessage message, CachedTextChannel channel, IServiceProvider services)
     : base(bot, prefix, message, channel, services)
     => Bot = bot;
示例#11
0
 protected override DiscordCommandContext CreateCommandContext(IPrefix prefix, IGatewayUserMessage message, CachedTextChannel channel)
 => new VCommandContext(this, prefix, message, channel, Services);