Beispiel #1
0
        public static async Task <Model> ModifyAsync(IRole role, BaseDiscordClient client,
                                                     Action <RoleProperties> func, RequestOptions options)
        {
            var args = new RoleProperties();

            func(args);
            var apiArgs = new API.Rest.ModifyGuildRoleParams
            {
                Color       = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create <uint>(),
                Hoist       = args.Hoist,
                Mentionable = args.Mentionable,
                Name        = args.Name,
                Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue.ToString() : Optional.Create <string>()
            };
            var model = await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, apiArgs, options).ConfigureAwait(false);

            if (args.Position.IsSpecified)
            {
                var bulkArgs = new[] { new BulkParams(role.Id, args.Position.Value) };
                await client.ApiClient.ModifyGuildRolesAsync(role.Guild.Id, bulkArgs, options).ConfigureAwait(false);

                model.Position = args.Position.Value;
            }
            return(model);
        }
        private void HandlePropertyRefElement(XmlReader reader)
        {
            var property = new PropertyRefElement(ParentElement);

            property.Parse(reader);
            RoleProperties.Add(property);
        }
Beispiel #3
0
        public static async Task ModifyAndWaitAsync(this IRole role, DiscordSocketClient client, Action <RoleProperties> action)
        {
            RoleProperties props = null;

            action.Invoke(props);

            var tcs = new TaskCompletionSource <bool>();

            Task RoleUpdatedAsync(SocketRole before, SocketRole after)
            {
                if (before.Id == role.Id)
                {
                    tcs.SetResult(true);
                }

                return(Task.CompletedTask);
            }

            client.RoleUpdated += RoleUpdatedAsync;

            await role.ModifyAsync(action);

            await tcs.Task;

            client.RoleUpdated -= RoleUpdatedAsync;
        }
Beispiel #4
0
 private void ChangeColour(RoleProperties obj)
 {
     if (i >= colors.Count)
     {
         i = 0;
     }
     obj.Color = colors[i];
     i++;
 }
        public static async Task <Model> ModifyAsync(IRole role, BaseDiscordClient client,
                                                     Action <RoleProperties> func, RequestOptions options)
        {
            var args = new RoleProperties();

            func(args);

            if (args.Icon.IsSpecified || args.Emoji.IsSpecified)
            {
                role.Guild.Features.EnsureFeature(GuildFeature.RoleIcons);

                if (args.Icon.IsSpecified && args.Emoji.IsSpecified)
                {
                    throw new ArgumentException("Emoji and Icon properties cannot be present on a role at the same time.");
                }
            }

            var apiArgs = new API.Rest.ModifyGuildRoleParams
            {
                Color       = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create <uint>(),
                Hoist       = args.Hoist,
                Mentionable = args.Mentionable,
                Name        = args.Name,
                Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue.ToString() : Optional.Create <string>(),
                Icon        = args.Icon.IsSpecified ? args.Icon.Value.Value.ToModel() : Optional <API.Image?> .Unspecified,
                Emoji       = args.Emoji.GetValueOrDefault()?.Name ?? Optional <string> .Unspecified
            };

            if (args.Icon.IsSpecified && role.Emoji != null)
            {
                apiArgs.Emoji = null;
            }

            if (args.Emoji.IsSpecified && !string.IsNullOrEmpty(role.Icon))
            {
                apiArgs.Icon = null;
            }

            var model = await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, apiArgs, options).ConfigureAwait(false);

            if (args.Position.IsSpecified)
            {
                var bulkArgs = new[] { new BulkParams(role.Id, args.Position.Value) };
                await client.ApiClient.ModifyGuildRolesAsync(role.Guild.Id, bulkArgs, options).ConfigureAwait(false);

                model.Position = args.Position.Value;
            }
            return(model);
        }
Beispiel #6
0
        public static async Task <Model> ModifyAsync(IRole role, BaseDiscordClient client,
                                                     Action <RoleProperties> func, RequestOptions options)
        {
            var args = new RoleProperties();

            func(args);
            var apiArgs = new API.Rest.ModifyGuildRoleParams
            {
                Color       = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create <uint>(),
                Hoist       = args.Hoist,
                Mentionable = args.Mentionable,
                Name        = args.Name,
                Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue : Optional.Create <ulong>(),
                Position    = args.Position
            };

            return(await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, apiArgs, options).ConfigureAwait(false));
        }
Beispiel #7
0
 public static void SetPermissions(this RoleProperties properties, GuildPermission permissions)
 {
     properties.Permissions = new GuildPermissions((ulong)permissions);
 }
Beispiel #8
0
        public static async Task EnsureRolesAsync(this SocketGuild guild, string roleName, RoleProperties roleProperties)
        {
            var role = guild.Roles.FirstOrDefault(r => r.Name == roleName);

            if (role == null)
            {
                await guild.CreateRoleAsync(roleName, roleProperties.Permissions.Value, roleProperties.Color.Value, roleProperties.Hoist.Value).ConfigureAwait(false);

                return;
            }

            await role.ModifyAsync(x =>
            {
                x.Color       = roleProperties.Color;
                x.Hoist       = roleProperties.Hoist;
                x.Permissions = roleProperties.Permissions;
                x.Mentionable = roleProperties.Mentionable;
                x.Name        = roleProperties.Name;
                x.Position    = roleProperties.Position;
            }).ConfigureAwait(false);
        }
Beispiel #9
0
        private static void Client_OnLoggedIn(DiscordSocketClient client, LoginEventArgs args)
        {
            Console.Write("Guild ID: ");
            ulong guildId = ulong.Parse(Console.ReadLine());

            try
            {
                var targetGuild = client.GetCachedGuild(guildId);

                Console.WriteLine("Creating guild...");
                var guild = client.CreateGuild(targetGuild.Name, targetGuild.Icon == null ? null : targetGuild.Icon.Download(), targetGuild.Region);

                Console.WriteLine("Creating roles...");
                Dictionary <ulong, ulong> dupedRoles = new Dictionary <ulong, ulong>();
                foreach (var role in targetGuild.Roles.OrderBy(r => r.Position).Reverse())
                {
                    var properties = new RoleProperties()
                    {
                        Name = role.Name, Color = role.Color, Mentionable = role.Mentionable, Permissions = role.Permissions, Seperated = role.Seperated
                    };

                    if (role.Name == "@everyone")
                    {
                        dupedRoles[role.Id] = guild.EveryoneRole.Modify(properties).Id;
                    }
                    else
                    {
                        dupedRoles[role.Id] = guild.CreateRole(new RoleProperties()
                        {
                            Name = role.Name, Color = role.Color, Mentionable = role.Mentionable, Permissions = role.Permissions, Seperated = role.Seperated
                        }).Id;
                    }
                }

                Console.WriteLine("Creating emojis...");
                var dupedEmojis = new Dictionary <ulong, ulong>();
                foreach (var emoji in targetGuild.Emojis)
                {
                    try
                    {
                        var created = guild.CreateEmoji(new EmojiProperties()
                        {
                            Name = emoji.Name, Image = emoji.Icon.Download()
                        });
                        dupedEmojis[emoji.Id.Value] = created.Id.Value;
                    }
                    catch (DiscordHttpException ex)
                    {
                        if (ex.Code == DiscordError.InvalidFormBody || ex.Code == DiscordError.MaximumEmojis)
                        {
                            break;
                        }
                        else if (ex.Code != DiscordError.InvalidFormBody)
                        {
                            throw;
                        }
                    }
                    catch (RateLimitException)
                    {
                        break; // Discord likes to throw this after us after duping most of a guild's emojis, and it's so big that we might as well stop trying.
                    }
                }

                Console.WriteLine("Removing default channels...");
                foreach (var channel in guild.GetChannels())
                {
                    channel.Delete();
                }

                Console.WriteLine("Creating channels...");
                Dictionary <ulong, ulong> dupedChannels = new Dictionary <ulong, ulong>();
                foreach (var channel in targetGuild.Channels.OrderBy(c => c.Type != ChannelType.Category))
                {
                    var ourChannel = guild.CreateChannel(channel.Name, TranslateChannelType(channel), channel.ParentId.HasValue ? dupedChannels[channel.ParentId.Value] : (ulong?)null);
                    ourChannel.Modify(new GuildChannelProperties()
                    {
                        Position = channel.Position
                    });

                    if (ourChannel.Type == ChannelType.Text)
                    {
                        var channelAsText = (TextChannel)channel;
                        ((TextChannel)ourChannel).Modify(new TextChannelProperties()
                        {
                            Nsfw = channelAsText.Nsfw, SlowMode = channelAsText.SlowMode, Topic = channelAsText.Topic
                        });
                    }
                    else if (ourChannel.Type == ChannelType.Voice)
                    {
                        var channelAsVoice = (VoiceChannel)channel;
                        ((VoiceChannel)ourChannel).Modify(new VoiceChannelProperties()
                        {
                            Bitrate = Math.Min(96000, channelAsVoice.Bitrate), UserLimit = Math.Min(99, channelAsVoice.UserLimit)
                        });
                    }

                    foreach (var overwrite in channel.PermissionOverwrites)
                    {
                        if (overwrite.Type == PermissionOverwriteType.Role)
                        {
                            ourChannel.AddPermissionOverwrite(dupedRoles[overwrite.AffectedId], PermissionOverwriteType.Role, overwrite.Allow, overwrite.Deny);
                        }
                    }

                    dupedChannels[channel.Id] = ourChannel.Id;
                }

                bool hasWelcomeScreen    = TryGetWelcomeScreen(targetGuild, out var welcomeScreen);
                bool hasVerificationForm = TryGetVerificationForm(targetGuild, out var verificationForm);

                if (hasWelcomeScreen || hasVerificationForm)
                {
                    Console.WriteLine("Enabling Community...");

                    guild.Modify(new GuildProperties()
                    {
                        DefaultNotifications = GuildDefaultNotifications.OnlyMentions,
                        VerificationLevel    = GuildVerificationLevel.Low,
                        Features             = new List <string>()
                        {
                            "COMMUNITY", "WELCOME_SCREEN_ENABLED"
                        },
                        PublicUpdatesChannelId = dupedChannels[targetGuild.PublicUpdatesChannel.Id],
                        RulesChannelId         = dupedChannels[targetGuild.RulesChannel.Id],
                        Description            = targetGuild.Description,
                        ExplicitContentFilter  = ExplicitContentFilter.KeepMeSafe
                    });

                    if (hasWelcomeScreen)
                    {
                        List <WelcomeChannelProperties> channels = new List <WelcomeChannelProperties>();

                        foreach (var channel in welcomeScreen.Channels)
                        {
                            ulong?emojiId = null;

                            if (channel.EmojiId.HasValue)
                            {
                                if (dupedEmojis.TryGetValue(channel.EmojiId.Value, out ulong emId))
                                {
                                    emojiId = emId;
                                }
                                else
                                {
                                    continue;  // this relates to discord's ratelimit issue
                                }
                            }

                            channels.Add(new WelcomeChannelProperties()
                            {
                                ChannelId   = dupedChannels[channel.Channel.Id],
                                Description = channel.Description,
                                EmojiId     = emojiId,
                                EmojiName   = channel.EmojiName
                            });
                        }

                        guild.ModifyWelcomeScreen(new WelcomeScreenProperties()
                        {
                            Enabled = true, Description = welcomeScreen.Description, Channels = channels
                        });
                    }

                    if (hasVerificationForm)
                    {
                        // this doesnt work for some reason
                        guild.ModifyVerificationForm(new VerificationFormProperties()
                        {
                            Description = verificationForm.Description,
                            Enabled     = true,
                            Fields      = verificationForm.Fields.ToList()
                        });
                    }

                    Console.WriteLine("Updating news channels...");
                    foreach (var channel in targetGuild.Channels)
                    {
                        if (channel.Type == ChannelType.News)
                        {
                            client.ModifyGuildChannel(dupedChannels[channel.Id], new TextChannelProperties()
                            {
                                News = true
                            });
                        }
                    }
                }

                Console.WriteLine("Done!");
            }
            catch (DiscordHttpException ex)
            {
                if (ex.Code == DiscordError.UnknownGuild)
                {
                    Console.WriteLine("You are not in this guild");
                }
                else
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Beispiel #10
0
        public void CheckGame()
        {
            try
            {
                Color  myColor = new Color(25, 202, 226);
                string hex     = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");

                foreach (var guild in _client.Guilds)
                {
                    var          roles  = guild.Roles;
                    var          users  = guild.Users;
                    List <Roles> Rooles = new List <DTO.Roles>();

                    foreach (var role in guild.Roles)
                    {
                        Roles roleitem = new Roles();
                        roleitem.Role = role;
                        List <IGuildUser> Roleusers = new List <IGuildUser>();

                        foreach (var user in users)
                        {
                            bool contains = false;
                            foreach (var userrole in user.Roles)
                            {
                                if (userrole.Id == role.Id)
                                {
                                    contains = true;
                                }
                            }
                            if (contains)
                            {
                                Roleusers.Add(user);
                            }
                        }
                        roleitem.Users = Roleusers;
                        Rooles.Add(roleitem);
                    }
                    foreach (var role in Rooles)
                    {
                        if (role.Users.Count == 0)
                        {
                            role.Role.DeleteAsync();
                        }
                    }

                    foreach (var user in users)
                    {
                        bool FoundRoleServer = false;

                        bool FoundRole = false;
                        foreach (var role in user.Roles)
                        {
                            if (role.Name == user.Game.ToString())
                            {
                                FoundRole       = true;
                                FoundRoleServer = true;
                            }

                            else if (role.Color.RawValue == myColor.RawValue)
                            {
                                user.RemoveRoleAsync(role);
                            }
                        }

                        foreach (var role in roles)
                        {
                            if (user.Roles.Contains(role))
                            {
                            }


                            if (!FoundRole)
                            {
                                if (role.Name == user.Game.ToString())
                                {
                                    FoundRoleServer = true;
                                    user.AddRoleAsync(role);
                                }
                                else
                                {
                                }
                            }
                        }
                        if (user.Game.ToString() != "" && !user.IsBot)
                        {
                            if (!FoundRoleServer)
                            {
                                var roleperm = new GuildPermissions(
                                    createInstantInvite: false,
                                    sendMessages: false,
                                    readMessageHistory: false,
                                    readMessages: false,
                                    changeNickname: false,
                                    mentionEveryone: false,
                                    useExternalEmojis: false,
                                    addReactions: false,
                                    connect: false,
                                    useVoiceActivation: false,
                                    sendTTSMessages: false,
                                    attachFiles: false,
                                    embedLinks: false,
                                    speak: false,
                                    muteMembers: false,
                                    manageRoles: false);


                                var role = guild.CreateRoleAsync(user.Game.ToString(), roleperm, myColor);

                                var rolepropp = new RoleProperties();
                                rolepropp.Mentionable = true;

                                role.Result.ModifyAsync(x =>
                                {
                                    x.Mentionable = true;
                                });

                                user.AddRoleAsync(role.Result);
                            }
                        }
                    }
                }
            }
            catch (Exception e) { }
        }
Beispiel #11
0
 public void AddRole(LoginProperties login, RoleProperties role)
 {
     _listener.ForEachAgInstance(server => server.AddRole(login, role));
 }