Example #1
0
        protected override async Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context)
        {
            MacroIdentifier = context.Arguments.First;

            if (!StoredMessagesService.IsValidMacroName(MacroIdentifier))
            {
                return(new ArgumentParseResult(Arguments[0], "Not a valid macro name!"));
            }

            context.Arguments.Index++;

            if (context.Arguments.First.ToLower() == "remove")
            {
                Delete        = true;
                SelectedMacro = null;
                return(ArgumentParseResult.SuccessfullParse);
            }
            else
            {
                Delete = false;
                JSONContainer json;
                if (context.Arguments.First.StartsWith("http"))
                {
                    string[] argSections = context.Arguments.First.Split("/", StringSplitOptions.RemoveEmptyEntries);
                    if (argSections.Length < 3)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    if (!(ulong.TryParse(argSections[argSections.Length - 3], out ulong guildId) && ulong.TryParse(argSections[argSections.Length - 2], out ulong channelId) && ulong.TryParse(argSections[argSections.Length - 1], out ulong messageId)))
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SocketGuild guild = BotCore.Client.GetGuild(guildId);
                    if (guild == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SocketTextChannel channel = guild.GetTextChannel(channelId);
                    if (channel == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    IMessage message = await channel.GetMessageAsync(messageId);

                    if (message == null)
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    EmbedHelper.GetJSONFromUserMessage(message, out json);
                    SelectedMacro = new Macro(MacroIdentifier, json);
                }
                else
                {
                    string embedText = context.RemoveArgumentsFront(1).Replace("[3`]", "```");
                    if (!JSONContainer.TryParse(embedText, out json, out string error))
                    {
                        return(new ArgumentParseResult(Arguments[1]));
                    }

                    SelectedMacro = new Macro(MacroIdentifier, json);
                    if (!SelectedMacro.Build(out _, out _, out error))
                    {
                        return(new ArgumentParseResult(Arguments[1], error));
                    }
                }

                return(ArgumentParseResult.SuccessfullParse);
            }
        }
        protected override async Task Execute(IDMCommandContext context, object argObject)
        {
            ArgumentContainer args = argObject as ArgumentContainer;

            JSONContainer json = EmbedHelper.GetJSONFromUserMessage(args.message);

            IReadOnlyCollection <IAttachment> attachments = args.message.Attachments;

            bool pretty = args.options.Contains(ExecutionOptions.pretty);
            bool remove = args.options.Contains(ExecutionOptions.remove);

            EmbedBuilder embed;

            if (pretty)
            {
                embed = new EmbedBuilder()
                {
                    Color       = BotCore.EmbedColor,
                    Title       = $"Message JSON for original message in {args.guild.Name} - {args.channel.Name} by {args.message.Author}",
                    Description = ("```json\n" + json.Build(true).Replace("```", "[3`]")).MaxLength(EmbedHelper.EMBEDDESCRIPTION_MAX - 8) + "```",
                };
            }
            else
            {
                embed = new EmbedBuilder()
                {
                    Color  = BotCore.EmbedColor,
                    Title  = $"Message JSON for original message in {args.guild.Name} - {args.channel.Name} by {args.message.Author}",
                    Footer = new EmbedFooterBuilder()
                    {
                        Text = json.Build(false).MaxLength(EmbedHelper.EMBEDFOOTERTEXT_MAX)
                    }
                };
            }
            if (attachments.Count > 0)
            {
                StringBuilder attachments_str = new StringBuilder();
                foreach (IAttachment attachment in attachments)
                {
                    if (attachment.Url.IsValidImageURL() && string.IsNullOrEmpty(embed.ImageUrl))
                    {
                        embed.ImageUrl = attachment.Url;
                    }
                    attachments_str.AppendLine($"[{attachment.Filename}]({attachment.Url})");
                }
                embed.AddField("Attachments", attachments_str.ToString());
            }
            await context.Channel.SendEmbedAsync(embed);

            if (remove)
            {
                try
                {
                    await args.channel.DeleteMessageAsync(args.message);
                }
                catch (Exception e)
                {
                    await context.Channel.SendEmbedAsync($"Failed to remove the message. Probably missing permissions! Exception: {e.GetType()} - {e.Message}", true);
                }
            }
        }