Exemplo n.º 1
0
        public void ReloadConfig(string dbConnectionString, ServerContext dbContext, Dictionary <string, Command> allCommands)
        {
            this.DbConnectionString = dbConnectionString;

            if (this.Commands?.Count != allCommands.Count)
            {
                this.Commands = new Dictionary <string, Command>(allCommands);
            }

            this.Config = dbContext.ServerConfigurations.FirstOrDefault(c => c.ServerId == this.Id);
            if (this.Config == null)
            {
                this.Config = new ServerConfig()
                {
                    ServerId = this.Id, Name = this.Guild.Name
                };
                dbContext.ServerConfigurations.Add(this.Config);
                dbContext.SaveChanges();
            }

            this.CustomCommands?.Clear();
            this.CustomAliases?.Clear();
            this.Roles?.Clear();
            this.ReactionAssignedRoles?.Clear();

            this.CustomCommands        = dbContext.CustomCommands.Where(c => c.ServerId == this.Id).ToDictionary(c => c.CommandId);
            this.CustomAliases         = dbContext.CustomAliases.Where(c => c.ServerId == this.Id).ToDictionary(c => c.Alias);
            this.Roles                 = dbContext.Roles.Where(c => c.ServerId == this.Id).ToDictionary(c => c.RoleId);
            this.ReactionAssignedRoles = dbContext.ReactionAssignedRoles.Where(c => c.ServerId == this.Id).ToDictionary(c => c.RoleId);

            List <ChannelConfig> channels = dbContext.Channels.Where(c => c.ServerId == this.Id).ToList();

            this.IgnoredChannels = channels.Where(c => c.Ignored).Select(c => c.ChannelId).ToList();

            SocketRole role;

            if (this.Config.MuteRoleId != 0 && (role = this.Guild.GetRole(this.Config.MuteRoleId)) != null)
            {
                foreach (SocketTextChannel channel in this.Guild.TextChannels)
                {
                    if (this.Config.MuteIgnoreChannelId == channel.Id ||
                        channel.PermissionOverwrites.Any(p => p.TargetId == role.Id))
                    {
                        continue;
                    }

                    try{
                        channel.AddPermissionOverwriteAsync(role, new OverwritePermissions(sendMessages: PermValue.Deny, addReactions: PermValue.Deny)).GetAwaiter().GetResult();
                    } catch (Exception) { }
                }
            }
        }
Exemplo n.º 2
0
        public void ReloadConfig(string dbConnectionString)
        {
            this.DbConnectionString = dbConnectionString;
            ServerContext dbContext = ServerContext.Create(dbConnectionString);

            this.Config = dbContext.ServerConfigurations.FirstOrDefault(c => c.ServerId == this.Id);
            if (this.Config == null)
            {
                this.Config = new ServerConfig()
                {
                    ServerId = this.Id, Name = this.Guild.Name
                };
                dbContext.ServerConfigurations.Add(this.Config);
                dbContext.SaveChanges();
            }

            this.CustomCommands?.Clear();
            this.CustomAliases?.Clear();
            this.Roles?.Clear();

            this.CustomCommands = dbContext.CustomCommands.Where(c => c.ServerId == this.Id).ToDictionary(c => c.CommandId);
            this.CustomAliases  = dbContext.CustomAliases.Where(c => c.ServerId == this.Id).ToDictionary(c => c.Alias);
            this.Roles          = dbContext.Roles.Where(c => c.ServerId == this.Id).ToDictionary(c => c.RoleId);

            List <ChannelConfig> channels = dbContext.Channels.Where(c => c.ServerId == this.Id).ToList();

            this.IgnoredChannels   = channels.Where(c => c.Ignored).Select(c => c.ChannelId).ToList();
            this.TemporaryChannels = channels.Where(c => c.Temporary).Select(c => c.ChannelId).ToList();
            this.MutedChannels     = channels.Where(c => c.MutedUntil > DateTime.MinValue).Select(c => c.ChannelId).ToList();

            dbContext.Dispose();

            SocketRole role;

            if (this.Config.MuteRoleId != 0 && (role = this.Guild.GetRole(this.Config.MuteRoleId)) != null)
            {
                foreach (SocketTextChannel channel in this.Guild.TextChannels)
                {
                    if (this.Config.MuteIgnoreChannelId == channel.Id)
                    {
                        continue;
                    }

                    try{
                        channel.AddPermissionOverwriteAsync(role, new OverwritePermissions(sendMessages: PermValue.Deny)).GetAwaiter().GetResult();
                    } catch (Exception) { }
                }
            }
        }