예제 #1
0
        private async Task _client_UserJoined(SocketGuildUser user)
        {
            if (Config.Load().AutoSave)
            {
                if (ServerPairs.PairList.Any(x => x.LoadedServer == user.Guild.Id))
                {
                    var server = ServerPairs.PairList.Where(x => x.LoadedServer == user.Guild.Id)
                                 .OrderBy(x => x.TimeLoaded).ToList();
                    var max  = server.Max(x => x.TimeLoaded);
                    var serv = ServerObject.GetSave(server.First(x => x.TimeLoaded == max).SavedServer);
                    if (serv != null)
                    {
                        if (serv.Roles.Any())
                        {
                            var list = new List <string>();
                            foreach (var role in serv.Roles)
                            {
                                if (!role.RoleMembers.Contains(user.Id))
                                {
                                    continue;
                                }
                                try
                                {
                                    var NewRole = user.Guild.Roles.FirstOrDefault(x => x.Name == role.RoleName);
                                    if (NewRole != null)
                                    {
                                        await user.AddRoleAsync(NewRole);

                                        list.Add($"{NewRole.Name}");
                                    }
                                }
                                catch
                                {
                                    //
                                }
                            }

                            if (list.Count > 0)
                            {
                                var embed = new EmbedBuilder {
                                    Color = Color.Blue
                                };
                                embed.AddField($"{user.Username} Roles Updated in {user.Guild.Name}", $"{string.Join("\n", list)}");
                                await user.SendMessageAsync("", false, embed.Build());
                            }
                        }
                    }
                }
                await ServerObject.SaveServer(user.Guild.DefaultChannel, false);

                Logger.LogInfo($"Autosaved {user.Guild.Name}");
            }
        }
        public static async Task SaveServer(SocketTextChannel Channel, bool ShowMessage)
        {
            var Guild = Channel.Guild;

            try
            {
                //normally you would use an object initialiser for this, however in a try catch loop we would miss any errors
                //so set each value indivisually for the object.
                var ns = new ServerObject();
                ns.LastSave = DateTime.Now;
                //save the guild name
                ns.ServerName = Guild.Name;
                //save the @everyone role permissions
                ns.EveryonePerms = Guild.EveryoneRole.Permissions.RawValue;
                //save each category with its name, position and permissions for each role
                ns.Categories = Guild.CategoryChannels.Select(x =>
                                                              new Category
                {
                    CategoryName        = x.Name,
                    Position            = x.Position,
                    CategoryPermissions = x.PermissionOverwrites.Where(y => y.TargetType == PermissionTarget.Role)
                                          .Select(y => new Permissions
                    {
                        AChannelPermissions = y.Permissions.AllowValue,
                        DChannelPermissions = y.Permissions.DenyValue,
                        PRole = Guild.GetRole(y.TargetId).Name
                    })
                });
                //save each text channel with name, permissions, NSFW Status, position, topic and category (if applicable)
                ns.TextChannels = Guild.TextChannels.Where(x => !Guild.CategoryChannels.Any(y =>
                                                                                            string.Equals(y.Name, x.Name, StringComparison.CurrentCultureIgnoreCase) &&
                                                                                            y.Position == x.Position)).Select(x => new TextChannel
                {
                    ChannelName        = x.Name,
                    ChannelPermissions = x.PermissionOverwrites.Where(z => z.TargetType == PermissionTarget.Role)
                                         .Select(z => new Permissions
                    {
                        AChannelPermissions = z.Permissions.AllowValue,
                        DChannelPermissions = z.Permissions.DenyValue,
                        PRole = Guild.GetRole(z.TargetId).Name
                    }),
                    IsNSFW       = x.Name.ToLower().Contains("nsfw"),
                    Position     = x.Position,
                    category     = x.Category?.Name,
                    topic        = x.Topic,
                    LastMessages = GetMessagesAsync(x).Result
                });
                //save each audio channel with its name, permissions, position and category (if applicable)
                ns.AudioChannels = Guild.VoiceChannels.Where(x => !Guild.CategoryChannels.Any(y =>
                                                                                              string.Equals(y.Name, x.Name, StringComparison.CurrentCultureIgnoreCase) &&
                                                                                              y.Position == x.Position)).Select(x => new AudioChannel
                {
                    ChannelName        = x.Name,
                    ChannelPermissions = x.PermissionOverwrites.Where(z => z.TargetType == PermissionTarget.Role)
                                         .Select(z => new Permissions
                    {
                        AChannelPermissions = z.Permissions.AllowValue,
                        DChannelPermissions = z.Permissions.DenyValue,
                        PRole = Guild.GetRole(z.TargetId).Name
                    }),
                    Position  = x.Position,
                    category  = x.Category?.Name,
                    UserLimit = x.UserLimit
                });

                //save each role with name, colour, members, permissions & position
                ns.Roles = Guild.Roles.Where(x => !x.IsEveryone && !x.IsManaged).Select(x => new Role
                {
                    RoleName          = x.Name,
                    RawColour         = x.Color.RawValue,
                    RoleMembers       = x.Members.Select(y => y.Id).ToList(),
                    GuildPermissions  = x.Permissions.RawValue,
                    position          = x.Position,
                    AllowMention      = x.IsMentionable,
                    DisplaySeperately = x.IsHoisted
                });
                //save all bans for the server
                ns.Bans = Guild.GetBansAsync().Result.Select(x => x.User.Id);

                //save a list of all the user IDs from the last server
                ns.Users = Guild.Users.Select(x => new UserC
                {
                    Nickname = x.Nickname,
                    UserID   = x.Id
                }).ToList();

                //serialise the server object to a json string and save to txt file for later use.
                var serverobj = JsonConvert.SerializeObject(ns, Formatting.Indented);
                File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"setup/{Guild.Id}.txt"), serverobj);
                if (ShowMessage)
                {
                    await Channel.SendMessageAsync("Server Saved.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }