Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (args.Parameters.Count > 0 && args.Guild != null && args.AccessLevel >= AccessLevel.Moderator)
            {
                if (args.AccessLevel >= AccessLevel.Host &&
                    args.Parameters[0].Equals("@@all", StringComparison.CurrentCultureIgnoreCase))
                {
                    args.Channel.SendMessageAsync("Started synchronizing all users").ConfigureAwait(false).GetAwaiter().GetResult();

                    int           errors = 0;
                    StringBuilder strb   = new StringBuilder();

                    foreach (var member in args.Guild.GetAllMembersAsync().ConfigureAwait(false).GetAwaiter().GetResult())
                    {
                        if (!VerificationManager.SynchronizeVerification(member.Id, args.Guild.Id, args.Config).ConfigureAwait(false).GetAwaiter().GetResult())
                        {
                            strb.Append($"{member.Id} ");
                            errors++;
                        }
                    }

                    if (errors == 0)
                    {
                        args.Channel.SendMessageAsync("Synchronized all users");
                    }
                    else
                    {
                        if (strb.Length > 1990)
                        {
                            strb.Length = 1990;
                        }

                        args.Channel.SendMessageAsync($"Synchronized users, failed to synchronize {errors} users:");
                        args.Channel.SendMessageAsync($"```\n{strb.ToString()}\n```");
                    }


                    return;
                }

                ulong mentionId;
                if ((mentionId = DiscordHandler.ExtractMentionId(args.ParameterString)) > 0)
                {
                    if (System.Threading.Tasks.Task.Run(async() => await VerificationManager.SynchronizeVerification(mentionId, args.Guild.Id, args.Config).ConfigureAwait(false)).ConfigureAwait(false).GetAwaiter().GetResult())
                    {
                        args.Channel.SendMessageAsync($"Synchronized {args.ParameterString}");
                    }
                    else
                    {
                        args.Channel.SendMessageAsync($"Failed to synchronize " + args.ParameterString);
                    }
                }
                else
                {
                    args.Channel.SendMessageAsync($"Failed to parse mention " + args.ParameterString);
                }

                return;
            }

            System.Threading.Tasks.Task.Run(async() => await VerificationManager.SynchronizeVerification((ulong)args.User.Id).ConfigureAwait(false)).ConfigureAwait(false).GetAwaiter().GetResult();


            args.Channel.SendMessageAsync($"Synchronized {args.User.Mention}");
        }