Ejemplo n.º 1
0
        public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context,
                                                                              CommandInfo command, IServiceProvider services)
        {
            if (featureService.CheckFeature(featureName, context.Guild.Id.ToString()))
            {
                return(PreconditionResult.FromSuccess());
            }

            return(PreconditionResult.FromError("This feature has not been enabled."));
        }
Ejemplo n.º 2
0
        private async Task MessageReceived(SocketMessage rawMessage)
        {
            // Ignore system messages and messages from bots
            if (!(rawMessage is SocketUserMessage message))
            {
                return;
            }
            if (message.Source != MessageSource.User)
            {
                return;
            }
            var context = new SocketCommandContext(_discord, message);

            if (hasLatexMathQuotes(message.Content))
            {
                var    latexTypingState = context.Channel.EnterTypingState();
                string formattedLatex   = formatLatexString(message.Content);
                var    pictureService   = new PictureService(new System.Net.Http.HttpClient());
                var    image            = pictureService.GetLatexImage(formattedLatex).Result;
                image.Seek(0, SeekOrigin.Begin);
                await context.Channel.SendFileAsync(image, "latex.png");

                latexTypingState.Dispose();
            }

            if (featureToggleService.CheckFeature("auto-jfif-to-jpeg", context.Guild.Id.ToString()))
            {
                foreach (Attachment attachment in context.Message.Attachments)
                {
                    if (attachment.Filename.EndsWith(".jfif") || attachment.Filename.EndsWith(".JFIF") ||
                        attachment.Filename.EndsWith(".jfif-large"))
                    {
                        pictureService.ConvertJfifToJpeg(context, attachment.Url);
                    }
                }
            }

            int argPos = 0;

            if (!(message.HasMentionPrefix(_discord.CurrentUser, ref argPos) || message.HasStringPrefix(Program._config["prefix"] + " ", ref argPos)))
            {
                return;
            }


            //var typingState = context.Channel.EnterTypingState();
            var result = await _commands.ExecuteAsync(context, argPos, _provider);

            if (result.Error.HasValue &&
                result.Error.Value != CommandError.UnknownCommand)
            {
                await context.Channel.SendMessageAsync(result.ToString());

                return;
            }

            if (result.Error.HasValue &&
                result.Error.Value == CommandError.UnknownCommand)
            {
                string[] parts = message.Content.Split(' ');
                if (parts.Length >= 2)
                {
                    string textResponseLookup = parts[1];
                    Regex  userIdCheck        = new Regex(@"<@![0-9]+>", RegexOptions.Compiled);
                    if (userIdCheck.IsMatch(textResponseLookup))
                    {
                        textResponseLookup = textResponseLookup.Replace("!", string.Empty);
                    }

                    string response = StaticTextResponseService.getGlobalResponse(textResponseLookup)
                                      ?? staticTextResponseService.getResponse(context.Guild.Id, textResponseLookup);

                    if (response != null)
                    {
                        await context.Channel.SendMessageAsync(response);
                    }
                }
            }

            //typingState.Dispose();
        }