Exemplo n.º 1
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            if (e.Executable == null)
            {
                Log.Warning("No command was selected, discontinue the flow.");
                return;
            }

            if (!(e.Executable is Node node))
            {
                Log.Warning("Executable was not made from a default Node.");
                return;
            }

            var scopesRequired = node.Attributes
                                 .OfType <RequiresScopeAttribute>()
                                 .Select(x => x.ScopeId)
                                 .ToList();

            var scopesGranted = await service.HasScopeAsync(
                (long)e.GetMessage().Author.Id, scopesRequired)
                                .ConfigureAwait(false);

            if (!scopesGranted)
            {
                Log.Warning("User tried to access scoped command, failed scope check.");
                return;
            }

            await next().ConfigureAwait(false);
        }
Exemplo n.º 2
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            if (e.Executable == null)
            {
                Log.Warning("Command not found, state check cancelling current pipeline instance.");
                return;
            }

            string commandId = e.Executable.ToString();

            if (string.IsNullOrWhiteSpace(commandId))
            {
                throw new Exception("command ID not set.");
            }

            var dbContext = e.GetService <DbContext>();

            if (dbContext == null)
            {
                throw new Exception("No Entity database set.");
            }

            var state = await GetCommandStateAsync(
                dbContext,
                commandId,
                (long)data.ChannelId);

            if (!state.State)
            {
                throw new Exception("State was denied");
            }

            await next();
        }
Exemplo n.º 3
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            if (e.Executable != null)
            {
                await next();

                return;
            }

            if (e == null)
            {
                return;
            }

            if (e.GetMessage().Type != DiscordMessageType.DEFAULT)
            {
                await next();

                return;
            }

            var service     = e.GetService <ICustomCommandsService>();
            var startIndex  = e.GetPrefixMatch().Length;
            var message     = e.GetMessage().Content;
            var endIndex    = message.IndexOf(' ', startIndex);
            var commandName = endIndex == -1
                ? message.Substring(startIndex)
                : message.Substring(startIndex, endIndex - startIndex);

            if (!await service.ExecuteAsync(e, commandName))
            {
                await next();
            }
        }
Exemplo n.º 4
0
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member 'MiddlewareStage.CheckAsync(IDiscordMessage, IMutableContext, Func<ValueTask>)'
        public ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member 'MiddlewareStage.CheckAsync(IDiscordMessage, IMutableContext, Func<ValueTask>)'
        {
            if (fn != null)
            {
                return(fn(data, e, next));
            }
            return(default);
Exemplo n.º 5
0
 /// <inheritdoc/>
 public async ValueTask CheckAsync(
     IDiscordMessage message, IMutableContext e, Func <ValueTask> next)
 {
     e.SetContext <ITypedArgumentPack>(
         ArgumentKey,
         new TypedArgumentPack(
             new ArgumentPack(
                 e.GetQuery().Split(' ').Where(x => !string.IsNullOrWhiteSpace(x))), provider));
     await next();
 }
Exemplo n.º 6
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            if (e == null)
            {
                return;
            }

            if (e.GetMessage().Type != DiscordMessageType.DEFAULT)
            {
                return;
            }

            var channel = e.GetChannel();

            if (!(channel is IDiscordGuildChannel guildChannel))
            {
                return;
            }

            var guild = await guildChannel.GetGuildAsync();

            var cache = e.GetService <IExtendedCacheClient>();
            IEnumerable <Token> tokens = null;

            string[] args        = e.GetMessage().Content.Substring(e.GetPrefixMatch().Length).Split(' ');
            string   commandName = args.FirstOrDefault().ToLowerInvariant();

            var cachePackage = await cache.HashGetAsync <ScriptPackage>(
                CommandCacheKey, commandName + ":" + guild.Id);

            if (cachePackage != null)
            {
                tokens = ScriptPacker.Unpack(cachePackage);
            }
            else
            {
                var db         = e.GetService <IUnitOfWork>();
                var repository = db.GetRepository <CustomCommand>();

                var command = await repository.GetAsync((long)guild.Id, commandName);

                if (command != null)
                {
                    tokens = new Tokenizer().Tokenize(command.CommandBody);
                }
            }

            if (tokens != null)
            {
                var context = CreateContext(e);
                e.GetChannel().QueueMessage(e, null, new Parser(tokens).Parse(context));
            }
        }
        public async ValueTask CheckAsync(IDiscordMessage msg, IMutableContext e, Func <ValueTask> next)
        {
            var result = await service.MatchAsync(e);

            if (result == null)
            {
                Log.Debug("No prefix found matched query.");
                return;
            }

            e.SetContext(PipelineBuilderExtensions.PrefixMatchKey, result);
            e.SetQuery(e.GetQuery().Substring(result.Length).TrimStart());
            await next();
        }
Exemplo n.º 8
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            var channel = await e.GetMessage().GetChannelAsync();

            if (channel == null)
            {
                throw new InvalidOperationException("This channel is not supported");
            }
            e.SetContext(ChannelArgumentKey, channel);
            if (channel is IDiscordGuildChannel gc)
            {
                e.SetContext(GuildArgumentKey, await gc.GetGuildAsync());
            }
            await next();
        }
        /// <inheritdoc/>
        public async ValueTask CheckAsync(
            IDiscordMessage data, [NotNull] IMutableContext e, [NotNull] Func <ValueTask> next)
        {
            if (e?.Executable == null)
            {
                Log.Debug("No executable found to perform permission check on.");
                return;
            }

            var message = e.GetMessage();

            if (message.Author is IDiscordGuildUser)
            {
                var permission = await service.GetPriorityPermissionAsync(e);

                if (permission == null)
                {
                    var defaultStatus = FetchPermissionStatusFrom(e.Executable);
                    permission = new Permission
                    {
                        GuildId     = (long)e.GetGuild().Id,
                        CommandName = e.Executable.ToString(),
                        EntityId    = 0,
                        Status      = defaultStatus,
                        Type        = 0
                    };
                }

                Log.Debug(permission.ToString());

                if (permission.Status == PermissionStatus.Allow)
                {
                    await next();
                }
            }
            else
            {
                if (FetchPermissionStatusFrom(e.Executable) == PermissionStatus.Deny)
                {
                    throw new InvalidOperationException(
                              "Denied request due to default setting set to Deny");
                }

                await next();
            }
        }
Exemplo n.º 10
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            Log.Debug($"Starting command aggregation with query '{e.GetQuery()}'");

            var command = map.GetCommand(e.GetArgumentPack().Pack);

            if (command == null)
            {
                Log.Warning($"No command was found with query '{string.Join(" ", e.GetQuery())}'");
                return;
            }

            if (command is IExecutable exec)
            {
                e.SetExecutable(exec);
                await next();
            }
        }
Exemplo n.º 11
0
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, [NotNull] Func <ValueTask> next)
        {
            if (e.Executable != null)
            {
                await next();

                return;
            }

            foreach (var handler in commandHandlers)
            {
                await handler.CheckAsync(data, e, () => default);

                if (e.Executable != null)
                {
                    await next();

                    return;
                }
            }
        }
        public async ValueTask CheckAsync(IDiscordMessage data, IMutableContext e, Func <ValueTask> next)
        {
            var channel = e.GetChannel();

            if (channel is IDiscordGuildChannel)
            {
                var locale = await service.GetLocaleAsync((long)channel.Id);

                e.SetContext(LocaleContextKey, locale);
            }
            else
            {
                // TODO: add GetDefaultLocale to ILocalizationService.
                if (!(service is LocalizationService extService))
                {
                    throw new NotSupportedException("Cannot fetch default locale from service");
                }

                var locale = extService.GetDefaultLocale();
                e.SetContext(LocaleContextKey, locale);
            }

            await next();
        }
Exemplo n.º 13
0
 /// <summary>
 /// Sets the query.
 /// </summary>
 public static void SetQuery(this IMutableContext context, string query)
 {
     context.SetContext(CorePipelineStage.QueryContextKey, query);
 }
Exemplo n.º 14
0
 public ValueTask CheckAsync(IDiscordMessage msg, IMutableContext e, Func <ValueTask> next)
 {
     e.SetContext(MessageContextKey, msg);
     e.SetContext(QueryContextKey, msg.Content);
     return(next());
 }
Exemplo n.º 15
0
 public static IMutableContext WithConfigurationProvider(this IMutableContext C, IConfigurationProvider provider)
 {
     //was AppSettings
     C.InjectService <IConfigurationProvider>(provider);
     return(C);
 }