Exemplo n.º 1
0
        public async Task <DiscordRole> GetOrCreateMuteRoleAsync(DiscordGuild guild)
        {
            DiscordRole muteRole = null;

            await this.csem.WaitAsync();

            try
            {
                using (var dc = this.shard.Database.CreateContext())
                {
                    DatabaseGuildConfiguration gcfg = guild.GetGuildConfiguration(dc);
                    muteRole = guild.GetRole(gcfg.MuteRoleId);
                    if (muteRole is null)
                    {
                        muteRole = guild.Roles.Values.FirstOrDefault(r => r.Name.ToLowerInvariant() == "f_mute");
                    }
                    if (muteRole is null)
                    {
                        muteRole = await guild.CreateRoleAsync("f_mute", hoist : false, mentionable : false);

                        foreach (var channel in guild.Channels.Values.Where(c => c.Type == ChannelType.Text))
                        {
                            await channel.AddOverwriteAsync(muteRole, deny : Permissions.SendMessages | Permissions.SendTtsMessages | Permissions.AddReactions);

                            await Task.Delay(100);
                        }
                        gcfg.MuteRoleId = muteRole.Id;
                        dc.GuildConfiguration.Update(gcfg);
                        await dc.SaveChangesAsync();
                    }
                }
            } finally { this.csem.Release(); }

            return(muteRole);
        }
Exemplo n.º 2
0
        public async Task <DiscordRole> GetOrCreateMuteRoleAsync(DiscordGuild guild)
        {
            DiscordRole muteRole = null;

            await this.csem.WaitAsync();

            try {
                muteRole = await this.shard.DatabaseService.GetMuteRoleAsync(guild);

                if (muteRole == null)
                {
                    muteRole = guild.Roles.FirstOrDefault(r => r.Name.ToLowerInvariant() == "gf_mute");
                }
                if (muteRole == null)
                {
                    muteRole = await guild.CreateRoleAsync("gf_mute", hoist : false, mentionable : false);

                    await this.shard.DatabaseService.SetMuteRoleAsync(guild.Id, muteRole.Id);

                    foreach (DiscordChannel channel in guild.Channels.Where(c => c.Type == ChannelType.Text))
                    {
                        await channel.AddOverwriteAsync(muteRole, deny : Permissions.SendMessages | Permissions.SendTtsMessages | Permissions.AddReactions);

                        await Task.Delay(200);
                    }
                }
            } finally {
                this.csem.Release();
            }

            return(muteRole);
        }
Exemplo n.º 3
0
        public async Task Do(DiscordMessage msg, DiscordGuild server)
        {
            string pronoun = msg.Content.Substring(cmd.Length).ToLower();

            if (!string.IsNullOrWhiteSpace(pronoun) && char.IsLetter(pronoun[0]))
            {
                string      pronounRole = "Pronoun: " + pronoun;
                bool        roleFound   = false;
                DiscordRole rl          = null;
                foreach (var role in server.Roles)
                {
                    if (role.Name == pronounRole)
                    {
                        rl        = role;
                        roleFound = true;
                        break;
                    }
                }

                if (!roleFound)
                {
                    rl = await server.CreateRoleAsync(pronounRole);
                }

                await server.GrantRoleAsync(msg.Author as DiscordMember, rl);
            }
        }
Exemplo n.º 4
0
            public async Task SetColor(DiscordMember user, DiscordGuild server, DiscordChannel channel, DiscordColor color)
            {
                IReadOnlyDictionary <ulong, DiscordRole> roles = server.Roles;
                DiscordRole existingRole = null;
                string      UserId       = user.Id.ToString();

                if (roles.Any(xr => xr.Value.Name == UserId))
                {
                    existingRole = roles.First(xr => xr.Value.Name == UserId).Value;
                    await existingRole.ModifyAsync(color : color, reason : "Coloring role edit");
                }
                else
                {
                    DiscordRole CreatedRole = await server.CreateRoleAsync(user.Id.ToString(), Permissions.None, color, null, null, "Coloring role creation");

                    await user.GrantRoleAsync(CreatedRole, "Coloring role assignment");
                }

                await channel.SendMessageAsync("Color set successfully.");

                Console.WriteLine("User {0}'s color was set to {1}", user.Username, color.R + ", " + color.G + ", " + color.B);
                return;
            }
Exemplo n.º 5
0
        public async Task <DiscordRole> GetOrCreateMuteRoleAsync(DiscordGuild guild)
        {
            DiscordRole?muteRole = null;

            await this.csem.WaitAsync();

            try {
                using TheGodfatherDbContext db = this.dbb.CreateContext();
                GuildConfig gcfg = await this.gcs.GetConfigAsync(guild.Id);

                muteRole = guild.GetRole(gcfg.MuteRoleId);
                if (muteRole is null)
                {
                    muteRole = guild.Roles.Select(kvp => kvp.Value).FirstOrDefault(r => r.Name.ToLowerInvariant() == "gf_mute");
                }
                if (muteRole is null)
                {
                    muteRole = await guild.CreateRoleAsync("gf_mute", hoist : false, mentionable : false);

                    IEnumerable <DiscordChannel> overwriteTargets = guild.Channels
                                                                    .Select(kvp => kvp.Value)
                                                                    .Where(c => c.Type == ChannelType.Category ||
                                                                           ((c.Type == ChannelType.Text || c.Type == ChannelType.Voice) && c.Parent is null)
                                                                           );

                    await Task.WhenAll(overwriteTargets.Select(c => AddOverwrite(c, muteRole)));

                    gcfg.MuteRoleId = muteRole.Id;
                    db.Configs.Update(gcfg);
                    await db.SaveChangesAsync();
                }
            } finally {
                this.csem.Release();
            }

            return(muteRole);
Exemplo n.º 6
0
        /// <summary>
        /// Tries to find, through enterprise-level heuristic analysis, a valid mute role. If not found, creates one.
        /// </summary>
        /// <param name="guild"></param>
        /// <param name="callee"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public static async Task <(DiscordRole Role, string Message)> SetupMuteRole(DiscordGuild guild,
                                                                                    DiscordMember callee, DiscordMember member)
        {
            var textChannels   = guild.Channels.Where(e => e.Value.Type == ChannelType.Text).Select(e => e.Value).ToArray();
            var candidateRoles = new List <DiscordRole>();

            foreach (var role in guild.Roles)
            {
                AddPotentialCandidate(member, textChannels, role.Value, candidateRoles);
            }

            var lastRole = candidateRoles.OrderByDescending(e => e.Position).FirstOrDefault();

            if (lastRole != default && callee.Roles.All(e => e.Id != lastRole.Id))
            {
                return(lastRole,
                       $"using existing role {lastRole.Name} since callee does not have it and cannot speak in any channel."
                       );
            }

            var channels = textChannels.Select(e => (channel: e, overwrites: e.GetPermissionOverwrites())).ToArray();

            foreach (var role in guild.Roles)
            {
                if (channels.All(e => e.overwrites.Any(ew =>
                                                       ew.Type == OverwriteType.Role && ew.Id == role.Value.Id &&
                                                       ew.Deny.HasPermission(Permissions.SendMessages))))
                {
                    return(role.Value, $"using existing role {role.Value.Name} since has override to not speak in any channel");
                }
            }
            foreach (var role in guild.Roles)
            {
                if (role.Value.CheckPermission(Permissions.SendMessages) != PermissionLevel.Unset ||
                    !role.Value.Name.ToLowerInvariant().Contains("mute"))
                {
                    continue;
                }

                foreach (var(channel, overwrites) in channels)
                {
                    // don't tamper with existing overwrites
                    if (overwrites.Any(e => e.Id == role.Value.Id))
                    {
                        continue;
                    }

                    await channel.AddOverwriteAsync(role.Value, Permissions.None, Permissions.SendMessages,
                                                    "ModCore automatic mute role channel overwrite for preexisting role");
                }
                return(role.Value, $"using existing role `{role.Value.Name}`");
            }
            var arole = await guild.CreateRoleAsync("ModCore Chat Mute", null, null, false, false,
                                                    "ModCore automatic mute role configuration");

            foreach (var(channel, _) in channels)
            {
                await channel.AddOverwriteAsync(arole, Permissions.None, Permissions.SendMessages,
                                                "ModCore automatic mute role channel overwrite");
            }
            return(arole, "automatically created it");
        }
Exemplo n.º 7
0
 private async Task <DiscordRole> GetMutedRole(DiscordGuild guild) =>
 guild.Roles.Values.FirstOrDefault(role => role.Name.ToLowerInvariant() == "muted") ?? await guild.CreateRoleAsync("Muted", mentionable : true);
Exemplo n.º 8
0
        private async Task <ulong> CreateRankRole(DiscordGuild guild, Rank rank, string title)
        {
            var role = await guild.CreateRoleAsync(title, permissions : DSharpPlus.Permissions.None, color : rank.GetColor());

            return(role.Id);
        }