示例#1
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            using WebClient wc = new WebClient();

            Uri downloadUri;

            try
            {
                downloadUri = new Uri(args.Parameters[2].TrimStart('<').TrimEnd('>'));
            }
            catch (UriFormatException)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Invalid link");
                return;
            }

            string json = wc.DownloadString(downloadUri);

            if (string.IsNullOrEmpty(json))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Embed json not found");
                return;
            }

            EmbedJson ej = Newtonsoft.Json.JsonConvert.DeserializeObject <EmbedJson>(json);

            if (ej == null ||
                (string.IsNullOrEmpty(ej.Content) && ej.Embed == null))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse embed json");
                return;
            }

            DiscordEmbed embed = ej.BuildEmbed();

            if (embed == null)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse embed json");
                return;
            }

            try
            {
                switch (args.Parameters[0].ToLower(CultureInfo.CurrentCulture))
                {
                case "webhook":
                {
                    if (args.Parameters.Count < 4)
                    {
                        HelpCommand.ShowHelp(args.Channel, this);
                        return;
                    }

                    ulong chId = DiscordHandler.ExtractMentionId(args.Parameters[1], true);

                    if (chId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "channel id");
                        return;
                    }

                    string avatarUri = null;

                    if (args.Parameters.Count > 4)
                    {
                        avatarUri = args.Parameters[4].TrimStart('<').TrimEnd('>');
                    }

                    SendEmbed(client, this, args, embed, ej.Content, chId, args.Guild, true, args.Parameters[3], avatarUri);
                }
                break;

                default:
                case "create":
                {
                    ulong chId = DiscordHandler.ExtractMentionId(args.Parameters[1], true);

                    if (chId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "channel id");
                        return;
                    }

                    SendEmbed(client, this, args, embed, ej.Content, chId, args.Guild, false, null, null);
                }
                break;

                case "edit":
                    DiscordMessageLink msgLink = DiscordHandler.ExtractMessageLink(args.Parameters[1]);

                    if (msgLink.DiscordGuildId == 0 ||
                        msgLink.DiscordChannelId == 0 ||
                        msgLink.DiscordMessageId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "message uri");
                        return;
                    }

                    EditEmbed(client, args, embed, ej.Content, msgLink.DiscordChannelId, msgLink.DiscordMessageId, args.Guild);
                    break;
                }
            }
            catch (ReadableCmdException rce)
            {
                HelpCommand.ShowHelp(args.Channel, this, rce.Message);
            }
        }