Beispiel #1
0
        public override async ValueTask <TypeParserResult <IMessage> > ParseAsync(
            Parameter parameter,
            string value,
            EspeonCommandContext context)
        {
            if (ulong.TryParse(value, out var messageId))
            {
                return(await context.Channel.GetOrFetchMessageAsync(messageId) is
                {
                }

                       message
                    ? TypeParserResult <IMessage> .Successful(message)
                    : new EspeonTypeParserFailedResult <IMessage>(INVALID_MESSAGE_ID_PATH));
            }

            if (!TryParseJumpUrl(value, out var ids))
            {
                return(new EspeonTypeParserFailedResult <IMessage>(INVALID_MESSAGE_ID_PATH));
            }

            if (context.Bot.GetChannel(ids.ChannelId) is ICachedMessageChannel channel)
            {
                return(await channel.GetOrFetchMessageAsync(ids.MessageId) is
                {
                }

                       message
                    ? TypeParserResult <IMessage> .Successful(message)
                    : new EspeonTypeParserFailedResult <IMessage>(INVALID_MESSAGE_ID_PATH));
            }

            return(new EspeonTypeParserFailedResult <IMessage>(INVALID_MESSAGE_ID_PATH));
        }
Beispiel #2
0
        private static async Task GlobalTagCallbackAsync(EspeonCommandContext context)
        {
            await using var dbContext = context.ServiceProvider.GetRequiredService <EspeonDbContext>();
            var tag = await dbContext.GlobalTags
                      .AsQueryable()
                      .FirstOrDefaultAsync(globalTag => globalTag.Key == context.Command.Name);

            await context.Channel.SendMessageAsync(tag.Value);

            tag.Uses++;
            await dbContext.UpdateAsync(tag);
        }
Beispiel #3
0
        public override ValueTask <TypeParserResult <IEnumerable <Command> > > ParseAsync(
            Parameter parameter,
            string value,
            EspeonCommandContext context)
        {
            var commandService   = (ICommandService)context.Bot;
            var commands         = commandService.GetAllCommands();
            var matchingCommands = commands.Where(command => IsMatchingCommand(value, command)).ToList();

            return(matchingCommands.Count > 0
                ? TypeParserResult <IEnumerable <Command> > .Successful(matchingCommands)
                : new EspeonTypeParserFailedResult <IEnumerable <Command> >(NO_MATCHING_COMMANDS));
        }
Beispiel #4
0
        public override ValueTask <TypeParserResult <Module> > ParseAsync(
            Parameter parameter,
            string value,
            EspeonCommandContext context)
        {
            var commandService = (ICommandService)context.Bot;
            var modules        = commandService.GetAllModules();
            var foundModule    = modules.FirstOrDefault(module => IsMatchingModule(value, module));

            return(foundModule is null
                ? new EspeonTypeParserFailedResult <Module>(MODULE_NOT_FOUND)
                : TypeParserResult <Module> .Successful(foundModule));
        }
Beispiel #5
0
        public override async ValueTask <TypeParserResult <IMember> > ParseAsync(
            Parameter parameter,
            string value,
            EspeonCommandContext context)
        {
            var cachedResult = await this._cachedParser.ParseAsync(parameter, value, context);

            if (cachedResult.IsSuccessful)
            {
                return(TypeParserResult <IMember> .Successful(cachedResult.Value));
            }

            if (!Disqord.Discord.TryParseUserMention(value, out var id) && !Snowflake.TryParse(value, out id))
            {
                return(new EspeonTypeParserFailedResult <IMember>(MEMBER_NOT_IN_CACHE_NO_MENTION));
            }

            var member = await context.Guild.GetMemberAsync(id);

            return(member is null
                ? new EspeonTypeParserFailedResult <IMember>(MEMBER_NOT_IN_GUILD)
                : TypeParserResult <IMember> .Successful(member));
        }
Beispiel #6
0
        private async Task ExecutionFailedAsync(EspeonCommandContext context, FailedResult result)
        {
            this._logger.LogInformation(
                (result as ExecutionFailedResult)?.Exception,
                "Execution failed of {command} for {user} in {guild}/{channel} because of {reason}",
                context.Command?.Name,
                context.Member.DisplayName,
                context.Guild.Name,
                context.Channel.Name,
                result.Reason);

            if (result is TypeParseFailedResult parseFailedResult)
            {
                var localisationKey = this._localisationService.GetKey(parseFailedResult.Reason);
                var response        = await this._localisationService.GetResponseAsync(context.Member.Id, context.Guild.Id, localisationKey);

                await context.Channel.SendMessageAsync(response);
            }
            else if (!(result is CommandNotFoundResult))
            {
                await context.Channel.SendMessageAsync(result.Reason);
            }
            context.ServiceScope.Dispose();
        }
Beispiel #7
0
 public abstract ValueTask <TypeParserResult <T> > ParseAsync(
     Parameter parameter,
     string value,
     EspeonCommandContext context);