diff --git a/DiscordSharp/DiscordClient.cs b/DiscordSharp/DiscordClient.cs index 4590016..4c80c1c 100644 --- a/DiscordSharp/DiscordClient.cs +++ b/DiscordSharp/DiscordClient.cs @@ -253,6 +253,7 @@ public class DiscordClient /// For use when connected to voice only. /// public event EventHandler UserLeftVoiceChannel; + public event EventHandler UserJoinedVoiceChannel; /// /// Occurs when the voice client is fully connected to voice. /// @@ -1840,11 +1841,6 @@ public void Connect() ws = new WebSocketSharpSocket(CurrentGatewayURL); DebugLogger.Log("Using WebSocketSharp websocket.."); - //catch (PlatformNotSupportedException) //Win7 doesn't support this. - //{ - // ws = new NetWebSocket(CurrentGatewayURL); - // DebugLogger.Log("Using .Net's built in WebSocket.."); - //} ws.MessageReceived += (sender, e) => { var message = JObject.Parse(e.Message); @@ -2909,6 +2905,10 @@ private void GuildCreateEvents(JObject message) } server.Channels.Add(tempChannel); } + + // Must set this after setting the Channels + server.AFKChannelID = message["d"]["afk_channel_id"].ToString(); + foreach (var mbr in message["d"]["members"]) { DiscordMember member = JsonConvert.DeserializeObject(mbr["user"].ToString()); @@ -3092,8 +3092,11 @@ private void MessageDeletedEvents(JObject message) private void VoiceStateUpdateEvents(JObject message) { + DiscordVoiceStateUpdateEventArgs e = new DiscordVoiceStateUpdateEventArgs(); + e.Guild = ServersList.Find(x => x.ID == message["d"]["guild_id"].ToString()); + DiscordMember memberToUpdate = e.Guild.GetMemberByKey(message["d"]["user_id"].ToString()); var f = message["d"]["channel_id"]; - if (f.ToString() == null) + if (f.ToString().IsNullOrEmpty()) { DiscordLeftVoiceChannelEventArgs le = new DiscordLeftVoiceChannelEventArgs(); DiscordServer inServer = ServersList.Find(x => x.ID == message["d"]["guild_id"].ToString()); @@ -3105,15 +3108,13 @@ private void VoiceStateUpdateEvents(JObject message) VoiceClient.MemberRemoved(le.User); if (UserLeftVoiceChannel != null) UserLeftVoiceChannel(this, le); + memberToUpdate.CurrentVoiceChannel = e.Channel; return; } - DiscordVoiceStateUpdateEventArgs e = new DiscordVoiceStateUpdateEventArgs(); - e.Guild = ServersList.Find(x => x.ID == message["d"]["guild_id"].ToString()); - DiscordMember memberToUpdate = e.Guild.GetMemberByKey(message["d"]["user_id"].ToString()); if (memberToUpdate != null) { e.Channel = e.Guild.Channels.Find(x => x.ID == message["d"]["channel_id"].ToString()); - memberToUpdate.CurrentVoiceChannel = e.Channel; + if (!message["d"]["self_deaf"].IsNullOrEmpty()) e.SelfDeaf = message["d"]["self_deaf"].ToObject(); e.Deaf = message["d"]["deaf"].ToObject(); @@ -3127,6 +3128,14 @@ private void VoiceStateUpdateEvents(JObject message) e.User = memberToUpdate; + if (memberToUpdate.CurrentVoiceChannel != e.Channel) { + if (memberToUpdate.ID != Me.ID) { + UserJoinedVoiceChannel?.Invoke(this, e); + } + } + + memberToUpdate.CurrentVoiceChannel = e.Channel; + if (VoiceClient != null && VoiceClient.Connected) VoiceClient.MemberAdded(e.User); diff --git a/DiscordSharp/Objects/DiscordChannel.cs b/DiscordSharp/Objects/DiscordChannel.cs index e633f48..33bcb27 100644 --- a/DiscordSharp/Objects/DiscordChannel.cs +++ b/DiscordSharp/Objects/DiscordChannel.cs @@ -46,6 +46,8 @@ public class DiscordChannel : DiscordChannelBase { [JsonProperty("type")] public ChannelType Type { get; set; } + + public bool IsAFKChannel { get { return Parent.AFKChannelID == ID; } } [JsonProperty("name")] public string Name { get; set; } diff --git a/DiscordSharp/Objects/DiscordServer.cs b/DiscordSharp/Objects/DiscordServer.cs index fcc1301..85e7b57 100644 --- a/DiscordSharp/Objects/DiscordServer.cs +++ b/DiscordSharp/Objects/DiscordServer.cs @@ -54,7 +54,17 @@ internal set [JsonProperty("channels")] public List Channels { get; internal set; } + + [JsonProperty("afk_channel_id")] + internal ID AFKChannelID { + get { return AFKChannel?.ID; } + set { + AFKChannel = Channels.Find(x => x.ID == value); + } + } + public DiscordChannel AFKChannel { get; private set; } + [JsonProperty("members")] private List membersAsList { @@ -354,5 +364,14 @@ public DiscordServer ShallowCopy() { return (DiscordServer)this.MemberwiseClone(); } + + public void ChangeMemberNickname(DiscordMember member, string newNickname) { + if (Unavailable) + throw new Exception("Server is currently unavailable!"); + + string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{this.ID}" + Endpoints.Members + $"/{member.ID}"; + string message = JsonConvert.SerializeObject(new { nick = newNickname }); + Console.WriteLine(WebWrapper.Patch(url, DiscordClient.token, message)); + } } }