Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GuildMemberNickname, AFKChannel, and UserJoinedVoiceChannel #59

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions DiscordSharp/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public class DiscordClient
/// For use when connected to voice only.
/// </summary>
public event EventHandler<DiscordLeftVoiceChannelEventArgs> UserLeftVoiceChannel;
public event EventHandler<DiscordVoiceStateUpdateEventArgs> UserJoinedVoiceChannel;
/// <summary>
/// Occurs when the voice client is fully connected to voice.
/// </summary>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<DiscordMember>(mbr["user"].ToString());
Expand Down Expand Up @@ -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());
Expand All @@ -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<bool>();
e.Deaf = message["d"]["deaf"].ToObject<bool>();
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions DiscordSharp/Objects/DiscordChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
19 changes: 19 additions & 0 deletions DiscordSharp/Objects/DiscordServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ internal set

[JsonProperty("channels")]
public List<DiscordChannel> 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<DiscordMember> membersAsList
{
Expand Down Expand Up @@ -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));
}
}
}