Esempio n. 1
0
 private void GuildCreateEvents(JObject message)
 {
     DiscordGuildCreateEventArgs e = new DiscordGuildCreateEventArgs();
     e.RawJson = message;
     DiscordServer server = new DiscordServer();
     server.id = message["d"]["id"].ToString();
     server.name = message["d"]["name"].ToString();
     server.owner_id = message["d"]["owner_id"].ToString();
     server.members = new List<DiscordMember>();
     server.channels = new List<DiscordChannel>();
     foreach(var chn in message["d"]["channels"])
     {
         DiscordChannel tempChannel = new DiscordChannel();
         tempChannel.id = chn["id"].ToString();
         tempChannel.type = chn["type"].ToString();
         tempChannel.topic = chn["topic"].ToString();
         tempChannel.name = chn["name"].ToString();
         tempChannel.is_private = false;
         server.channels.Add(tempChannel);
     }
     foreach(var mbr in message["d"]["members"])
     {
         DiscordMember member = new DiscordMember();
         member.user.avatar = mbr["user"]["avatar"].ToString();
         member.user.username = mbr["user"]["username"].ToString();
         member.user.id = mbr["user"]["id"].ToString();
         member.user.discriminator = mbr["user"]["discriminator"].ToString();
         server.members.Add(member);
     }
     ServersList.Add(server);
     e.server = server;
     if (GuildCreated != null)
         GuildCreated(this, e);
 }
Esempio n. 2
0
        private void GuildCreateEvents(JObject message)
        {
            DiscordGuildCreateEventArgs e = new DiscordGuildCreateEventArgs();
            e.RawJson = message;
            DiscordServer server = new DiscordServer();
            server.id = message["d"]["id"].ToString();
            server.name = message["d"]["name"].ToString();
            server.members = new List<DiscordMember>();
            server.channels = new List<DiscordChannel>();
            server.roles = new List<DiscordRole>();
            foreach (var roll in message["d"]["roles"])
            {
                DiscordRole t = new DiscordRole
                {
                    color = new DiscordSharp.Color(roll["color"].ToObject<int>().ToString("x")),
                    name = roll["name"].ToString(),
                    permissions = new DiscordPermission(roll["permissions"].ToObject<uint>()),
                    position = roll["position"].ToObject<int>(),
                    managed = roll["managed"].ToObject<bool>(),
                    id = roll["id"].ToString(),
                    hoist = roll["hoist"].ToObject<bool>()
                };
                server.roles.Add(t);
            }
            foreach (var chn in message["d"]["channels"])
            {
                DiscordChannel tempChannel = new DiscordChannel();
                tempChannel.id = chn["id"].ToString();
                tempChannel.type = chn["type"].ToString();
                tempChannel.topic = chn["topic"].ToString();
                tempChannel.name = chn["name"].ToString();
                tempChannel.is_private = false;
                tempChannel.PermissionOverrides = new List<DiscordPermissionOverride>();
                foreach (var o in chn["permission_overwrites"])
                {
                    if (tempChannel.type == "voice")
                        continue;
                    DiscordPermissionOverride dpo = new DiscordPermissionOverride(o["allow"].ToObject<uint>(), o["deny"].ToObject<uint>());
                    dpo.id = o["id"].ToString();

                    if (o["type"].ToString() == "member")
                        dpo.type = DiscordPermissionOverride.OverrideType.member;
                    else
                        dpo.type = DiscordPermissionOverride.OverrideType.role;

                    tempChannel.PermissionOverrides.Add(dpo);
                }
                server.channels.Add(tempChannel);
            }
            foreach(var mbr in message["d"]["members"])
            {
                DiscordMember member = JsonConvert.DeserializeObject<DiscordMember>(mbr["user"].ToString());
                member.parentclient = this;
                member.Parent = server;
                
                foreach(var rollid in mbr["roles"])
                {
                    member.Roles.Add(server.roles.Find(x => x.id == rollid.ToString()));
                }
                if (member.Roles.Count == 0)
                    member.Roles.Add(server.roles.Find(x => x.name == "@everyone"));
                server.members.Add(member);
            }
            server.owner = server.members.Find(x => x.ID == message["d"]["owner_id"].ToString());

            ServersList.Add(server);
            e.server = server;
            if (GuildCreated != null)
                GuildCreated(this, e);
        }
Esempio n. 3
0
        private void GuildCreateEvents(JObject message)
        {
            DiscordGuildCreateEventArgs e = new DiscordGuildCreateEventArgs();
            e.RawJson = message;
            DiscordServer server = new DiscordServer();
            server.JoinedAt = message["d"]["joined_at"].ToObject<DateTime>();
            server.parentclient = this;
            server.ID = message["d"]["id"].ToString();
            server.Name = message["d"]["name"].ToString();
            server.Members = new Dictionary<ID, DiscordMember>();
            server.Channels = new List<DiscordChannel>();
            server.Roles = new List<DiscordRole>();
            foreach (var roll in message["d"]["roles"])
            {
                DiscordRole t = new DiscordRole
                {
                    Color = new DiscordSharp.Color(roll["color"].ToObject<int>().ToString("x")),
                    Name = roll["name"].ToString(),
                    Permissions = new DiscordPermission(roll["permissions"].ToObject<uint>()),
                    Position = roll["position"].ToObject<int>(),
                    Managed = roll["managed"].ToObject<bool>(),
                    ID = roll["id"].ToString(),
                    Hoist = roll["hoist"].ToObject<bool>()
                };
                server.Roles.Add(t);
            }
            foreach (var chn in message["d"]["channels"])
            {
                DiscordChannel tempChannel = new DiscordChannel();
                tempChannel.Client = this;
                tempChannel.ID = chn["id"].ToString();
                tempChannel.Type = chn["type"].ToObject<ChannelType>();

                if (!chn["topic"].IsNullOrEmpty())
                    tempChannel.Topic = chn["topic"].ToString();
                if (tempChannel.Type == ChannelType.Voice && !chn["bitrate"].IsNullOrEmpty())
                    tempChannel.Bitrate = chn["bitrate"].ToObject<int>();

                tempChannel.Name = chn["name"].ToString();
                tempChannel.Private = false;
                tempChannel.PermissionOverrides = new List<DiscordPermissionOverride>();
                tempChannel.Parent = server;
                foreach (var o in chn["permission_overwrites"])
                {
                    if (tempChannel.Type == ChannelType.Voice)
                        continue;
                    DiscordPermissionOverride dpo = new DiscordPermissionOverride(o["allow"].ToObject<uint>(), o["deny"].ToObject<uint>());
                    dpo.id = o["id"].ToString();

                    if (o["type"].ToString() == "member")
                        dpo.type = DiscordPermissionOverride.OverrideType.member;
                    else
                        dpo.type = DiscordPermissionOverride.OverrideType.role;

                    tempChannel.PermissionOverrides.Add(dpo);
                }
                server.Channels.Add(tempChannel);
            }
            foreach (var mbr in message["d"]["members"])
            {
                DiscordMember member = JsonConvert.DeserializeObject<DiscordMember>(mbr["user"].ToString());
                if(mbr["nick"] != null)
                    member.Nickname = mbr["nick"].ToString();

                member.parentclient = this;
                member.Parent = server;

                foreach (var rollid in mbr["roles"])
                {
                    member.Roles.Add(server.Roles.Find(x => x.ID == rollid.ToString()));
                }
                if (member.Roles.Count == 0)
                    member.Roles.Add(server.Roles.Find(x => x.Name == "@everyone"));
                server.AddMember(member);
            }
            foreach (var voiceStateJSON in message["d"]["voice_states"]) {
                DiscordVoiceState voiceState = JsonConvert.DeserializeObject<DiscordVoiceState>(voiceStateJSON.ToString());
                DiscordMember member = server.GetMemberByKey(voiceState.UserID);

                member.CurrentVoiceChannel = server.Channels.Find(x => x.ID == voiceState.ChannelID);
                member.VoiceState = voiceState;
            }
            server.Owner = server.GetMemberByKey(message["d"]["owner_id"].ToString());
            e.Server = server;

            if (!message["d"]["unavailable"].IsNullOrEmpty() && message["d"]["unavailable"].ToObject<bool>() == false)
            {
                var oldServer = ServersList.Find(x => x.ID == server.ID);
                if (oldServer != null && oldServer.Unavailable)
                    ServersList.Remove(oldServer);

                ServersList.Add(server);

                DebugLogger.Log($"Guild with ID {server.ID} ({server.Name}) became available.");
                GuildAvailable?.Invoke(this, e);
                return;
            }

            ServersList.Add(server);
            GuildCreated?.Invoke(this, e);
        }