//eh private void GetChannelsList(JObject m) { if (ServersList == null) { ServersList = new List <DiscordServer>(); } foreach (var j in m["d"]["guilds"]) { DiscordServer temp = new DiscordServer(); temp.id = j["id"].ToString(); temp.name = j["name"].ToString(); temp.owner_id = j["owner_id"].ToString(); List <DiscordChannel> tempSubs = new List <DiscordChannel>(); foreach (var u in j["channels"]) { DiscordChannel tempSub = new DiscordChannel(); tempSub.id = u["id"].ToString(); tempSub.name = u["name"].ToString(); tempSub.type = u["type"].ToString(); tempSubs.Add(tempSub); } temp.channels = tempSubs; foreach (var mm in j["members"]) { DiscordMember member = new DiscordMember(); member.user.id = mm["user"]["id"].ToString(); member.user.username = mm["user"]["username"].ToString(); temp.members.Add(member); } ServersList.Add(temp); } }
private void MessageUpdateEvents(JObject message) { DiscordServer pserver = ServersList.Find(x => x.channels.Find(y => y.id == message["d"]["channel_id"].ToString()) != null); DiscordChannel pchannel = pserver.channels.Find(x => x.id == message["d"]["channel_id"].ToString()); if (pchannel != null) { if (message["d"]["author"] != null) { if (MessageEdited != null) { MessageEdited(this, new DiscordMessageEditedEventArgs { author = pserver.members.Find(x => x.user.id == message["d"]["author"]["id"].ToString()), Channel = pchannel, message = message["d"]["content"].ToString(), MessageType = DiscordMessageType.CHANNEL, MessageEdited = new DiscordMessage { author = pserver.members.Find(x => x.user.id == message["d"]["author"]["id"].ToString()), content = MessageLog.Find(x => x.Key == message["d"]["id"].ToString()).Value["d"]["content"].ToString(), } }); } var toRemove = MessageLog.Find(x => x.Key == message["d"]["id"].ToString()); var jsonToEdit = toRemove.Value; jsonToEdit["d"]["content"].Replace(JToken.FromObject(message["d"]["content"].ToString())); } else //I know they say assume makes an ass out of you and me...but we're assuming it's Discord's weird auto edit of a just URL message { if (URLMessageAutoUpdate != null) { DiscordURLUpdateEventArgs asdf = new DiscordURLUpdateEventArgs(); //I'm running out of clever names and should probably split these off into different internal voids soon... asdf.id = message["d"]["id"].ToString(); asdf.channel = ServersList.Find(x => x.channels.Find(y => y.id == message["d"]["channel_id"].ToString()) != null).channels.Find(x => x.id == message["d"]["channel_id"].ToString()); foreach (var embed in message["d"]["embeds"]) { DiscordEmbeds temp = new DiscordEmbeds(); temp.url = embed["url"].ToString(); temp.description = embed["description"].ToString(); temp.provider_name = embed["provider"]["name"].ToString(); temp.provider_url = embed["provider"]["url"].ToString(); temp.title = embed["title"].ToString(); temp.type = embed["type"].ToString(); asdf.embeds.Add(temp); } URLMessageAutoUpdate(this, asdf); } } } }
//eh private void GetChannelsList(JObject m) { if (ServersList == null) ServersList = new List<DiscordServer>(); foreach(var j in m["d"]["guilds"]) { DiscordServer temp = new DiscordServer(); temp.id = j["id"].ToString(); temp.name = j["name"].ToString(); //temp.owner_id = j["owner_id"].ToString(); List<DiscordChannel> tempSubs = new List<DiscordChannel>(); List<DiscordRole> tempRoles = new List<DiscordRole>(); foreach(var u in j["roles"]) { DiscordRole t = new DiscordRole { color = new DiscordSharp.Color(u["color"].ToObject<int>().ToString("x")), name = u["name"].ToString(), permissions = new DiscordPermission(u["permissions"].ToObject<uint>()), position = u["position"].ToObject<int>(), managed = u["managed"].ToObject<bool>(), id = u["id"].ToString(), hoist = u["hoist"].ToObject<bool>() }; tempRoles.Add(t); } temp.roles = tempRoles; foreach(var u in j["channels"]) { DiscordChannel tempSub = new DiscordChannel(); tempSub.id = u["id"].ToString(); tempSub.name = u["name"].ToString(); tempSub.type = u["type"].ToString(); tempSub.topic = u["topic"].ToString(); tempSub.parent = temp; List<DiscordPermissionOverride> permissionoverrides = new List<DiscordPermissionOverride>(); foreach(var o in u["permission_overwrites"]) { 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; permissionoverrides.Add(dpo); } tempSub.PermissionOverrides = permissionoverrides; tempSubs.Add(tempSub); } temp.channels = tempSubs; foreach(var mm in j["members"]) { DiscordMember member = JsonConvert.DeserializeObject<DiscordMember>(mm["user"].ToString()); //member.ID = mm["user"]["id"].ToString(); //member.Username = mm["user"]["username"].ToString(); //member.Avatar = mm["user"]["avatar"].ToString(); //member.Discriminator = mm["user"]["discriminator"].ToString(); member.Roles = new List<DiscordRole>(); JArray rawRoles = JArray.Parse(mm["roles"].ToString()); if(rawRoles.Count > 0) { foreach(var role in rawRoles.Children()) { member.Roles.Add(temp.roles.Find(x => x.id == role.Value<string>())); } } else { member.Roles.Add(temp.roles.Find(x => x.name == "@everyone")); } member.Parent = temp; temp.members.Add(member); } temp.owner = temp.members.Find(x => x.ID == j["owner_id"].ToString()); ServersList.Add(temp); } }
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); }
private void GuildUpdateEvents(JObject message) { DiscordServer oldServer = ServersList.Find(x => x.id == message["d"]["id"].ToString()); DiscordServer newServer = new DiscordServer { name = message["d"]["name"].ToString(), id = message["d"]["name"].ToString() }; newServer.roles = new List<DiscordRole>(); if (!message["d"]["roles"].IsNullOrEmpty()) { 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>() }; newServer.roles.Add(t); } } newServer.channels = new List<DiscordChannel>(); if (!message["d"]["channels"].IsNullOrEmpty()) { foreach (var u in message["d"]["channels"]) { DiscordChannel tempSub = new DiscordChannel(); tempSub.id = u["id"].ToString(); tempSub.name = u["name"].ToString(); tempSub.type = u["type"].ToString(); tempSub.topic = u["topic"].ToString(); tempSub.parent = newServer; List<DiscordPermissionOverride> permissionoverrides = new List<DiscordPermissionOverride>(); foreach (var o in u["permission_overwrites"]) { DiscordPermissionOverride dpo = new DiscordPermissionOverride(o["allow"].ToObject<uint>(), o["deny"].ToObject<uint>()); dpo.type = o["type"].ToObject<DiscordPermissionOverride.OverrideType>(); dpo.id = o["id"].ToString(); permissionoverrides.Add(dpo); } tempSub.PermissionOverrides = permissionoverrides; newServer.channels.Add(tempSub); } } if (!message["d"]["members"].IsNullOrEmpty()) { foreach (var mm in message["d"]["members"]) { DiscordMember member = JsonConvert.DeserializeObject<DiscordMember>(mm["user"].ToString()); member.parentclient = this; member.Parent = newServer; JArray rawRoles = JArray.Parse(mm["roles"].ToString()); if (rawRoles.Count > 0) { foreach (var role in rawRoles.Children()) { member.Roles.Add(newServer.roles.Find(x => x.id == role.Value<string>())); } } else { member.Roles.Add(newServer.roles.Find(x => x.name == "@everyone")); } newServer.members.Add(member); } } if (!message["d"]["owner_id"].IsNullOrEmpty()) { newServer.owner = newServer.members.Find(x => x.ID == message["d"]["owner_id"].ToString()); DebugLogger.Log($"Transferred ownership from user '{oldServer.owner.Username}' to {newServer.owner.Username}."); } ServersList.Remove(oldServer); ServersList.Add(newServer); DiscordServerUpdateEventArgs dsuea = new DiscordServerUpdateEventArgs { NewServer = newServer, OldServer = oldServer }; if (GuildUpdated != null) GuildUpdated(this, dsuea); }
public void DeleteRole(DiscordServer guild, DiscordRole role) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}" + Endpoints.Roles + $"/{role.id}"; try { WebWrapper.Delete(url, token); } catch(Exception ex) { DebugLogger.Log($"Error ocurred while deleting role ({role.name}): {ex.Message}", MessageLevel.Error); } }
public DiscordRole EditRole(DiscordServer guild, DiscordRole newRole) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}" + Endpoints.Roles + $"/{newRole.id}"; string request = JsonConvert.SerializeObject( new { color = decimal.Parse(newRole.color.ToDecimal().ToString()), hoist = newRole.hoist, name = newRole.name, permissions = newRole.permissions.GetRawPermissions() } ); try { var result = JObject.Parse(WebWrapper.Patch(url, token, request)); if (result != null) { DiscordRole d = new DiscordRole { color = new Color(result["color"].ToObject<int>().ToString("x")), hoist = result["hoist"].ToObject<bool>(), id = result["id"].ToString(), managed = result["managed"].ToObject<bool>(), name = result["name"].ToString(), permissions = new DiscordPermission(result["permissions"].ToObject<uint>()), position = result["position"].ToObject<int>() }; ServersList.Find(x => x.id == guild.id).roles.Remove(d); ServersList.Find(x => x.id == guild.id).roles.Add(d); return d; } } catch(Exception ex) { DebugLogger.Log($"Error ocurred while editing role ({newRole.name}): {ex.Message}", MessageLevel.Error); } return null; }
public DiscordRole CreateRole(DiscordServer guild) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}" + Endpoints.Roles; try { var result = JObject.Parse(WebWrapper.Post(url, token, "")); if (result != null) { DiscordRole d = new DiscordRole { color = new Color(result["color"].ToObject<int>().ToString("x")), hoist = result["hoist"].ToObject<bool>(), id = result["id"].ToString(), managed = result["managed"].ToObject<bool>(), name = result["name"].ToString(), permissions = new DiscordPermission(result["permissions"].ToObject<uint>()), position = result["position"].ToObject<int>() }; ServersList.Find(x => x.id == guild.id).roles.Add(d); return d; } } catch(Exception ex) { DebugLogger.Log($"Error ocurred while creating role in guild {guild.name}: {ex.Message}", MessageLevel.Error); } return null; }
public void AssignRoleToMember(DiscordServer guild, DiscordRole role, DiscordMember member) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}" + Endpoints.Members + $"/{member.ID}"; string message = JsonConvert.SerializeObject(new { roles = new string[] { role.id } }); try { Console.WriteLine(WebWrapper.Patch(url, token, message)); } catch(Exception ex) { DebugLogger.Log($"Exception ocurred while assigning role ({role.name}) to member ({member.Username}): " + ex.Message, MessageLevel.Error); } }
public void EditGuildName(DiscordServer guild, string NewGuildName) { string editGuildUrl = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}"; var newNameJson = JsonConvert.SerializeObject(new { name = NewGuildName }); try { WebWrapper.Patch(editGuildUrl, token, newNameJson); } catch(Exception ex) { DebugLogger.Log($"Exception ocurred while editing guild ({guild.name}) name: " + ex.Message, MessageLevel.Error); } }
public DiscordServer CreateGuild(string GuildName) { string createGuildUrl = Endpoints.BaseAPI + Endpoints.Guilds; string req = JsonConvert.SerializeObject(new { name = GuildName }); try { var response = JObject.Parse(WebWrapper.Post(createGuildUrl, token, req)); if (response != null) { DiscordServer server = new DiscordServer(); server.id = response["id"].ToString(); server.name = response["name"].ToString(); string channelGuildUrl = createGuildUrl + $"/{server.id}" + Endpoints.Channels; var channelRespone = JArray.Parse(WebWrapper.Get(channelGuildUrl, token)); foreach (var item in channelRespone.Children()) { server.channels.Add(new DiscordChannel { name = item["name"].ToString(), id = item["id"].ToString(), topic = item["topic"].ToString(), is_private = item["is_private"].ToObject<bool>(), type = item["type"].ToString() }); } server.members.Add(Me); server.owner = server.members.Find(x => x.ID == response["owner_id"].ToString()); if (server.owner == null) DebugLogger.Log("Owner is null in CreateGuild!", MessageLevel.Critical); ServersList.Add(server); return server; } } catch(Exception ex) { DebugLogger.Log("Exception ocurred while creating guild: " + ex.Message, MessageLevel.Error); } return null; }
public DiscordChannel CreateChannel(DiscordServer server, string ChannelName, bool voice) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{server.id}" + Endpoints.Channels; var reqJson = JsonConvert.SerializeObject(new { name = ChannelName, type = voice ? "voice" : "text" }); try { var result = JObject.Parse(WebWrapper.Post(url, token, reqJson)); if (result != null) { DiscordChannel dc = new DiscordChannel { name = result["name"].ToString(), id = result["id"].ToString(), type = result["type"].ToString(), is_private = result["is_private"].ToObject<bool>(), topic = result["topic"].ToString() }; server.channels.Add(dc); return dc; } } catch(Exception ex) { DebugLogger.Log("Exception ocurred while creating channel: " + ex.Message, MessageLevel.Error); } return null; }
//eh private void GetChannelsList(JObject m) { if (ServersList == null) ServersList = new List<DiscordServer>(); foreach(var j in m["d"]["guilds"]) { DiscordServer temp = new DiscordServer(); temp.id = j["id"].ToString(); temp.name = j["name"].ToString(); temp.owner_id = j["owner_id"].ToString(); List<DiscordChannel> tempSubs = new List<DiscordChannel>(); foreach(var u in j["channels"]) { DiscordChannel tempSub = new DiscordChannel(); tempSub.id = u["id"].ToString(); tempSub.name = u["name"].ToString(); tempSub.type = u["type"].ToString(); tempSub.topic = u["topic"].ToString(); tempSubs.Add(tempSub); } temp.channels = tempSubs; foreach(var mm in j["members"]) { DiscordMember member = new DiscordMember(); member.user.id = mm["user"]["id"].ToString(); member.user.username = mm["user"]["username"].ToString(); member.user.avatar = mm["user"]["avatar"].ToString(); member.user.discriminator = mm["user"]["discriminator"].ToString(); temp.members.Add(member); } ServersList.Add(temp); } }
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); }
public void ConnectAndReadMessages() { ws = new WebSocket("wss://discordapp.com/hub"); ws.OnMessage += (sender, e) => { var message = JObject.Parse(e.Data); switch (message["t"].ToString()) { case ("READY"): using (var sw = new StreamWriter("READY")) sw.Write(message); Me = new DiscordMember { user = new DiscordUser { username = message["d"]["user"]["username"].ToString(), id = message["d"]["user"]["id"].ToString(), verified = message["d"]["user"]["verified"].ToObject <bool>(), avatar = message["d"]["user"]["avatar"].ToString(), discriminator = message["d"]["user"]["discriminator"].ToString() } }; HeartbeatInterval = int.Parse(message["d"]["heartbeat_interval"].ToString()); GetChannelsList(message); if (Connected != null) { Connected(this, new DiscordConnectEventArgs { user = Me }); //Since I already know someone will ask for it. } break; case ("PRESENCE_UPDATE"): PresenceUpdateEvents(message); break; case ("MESSAGE_UPDATE"): MessageUpdateEvents(message); break; case ("TYPING_START"): DiscordServer server = ServersList.Find(x => x.channels.Find(y => y.id == message["d"]["channel_id"].ToString()) != null); if (server != null) { DiscordChannel channel = server.channels.Find(x => x.id == message["d"]["channel_id"].ToString()); DiscordMember uuser = server.members.Find(x => x.user.id == message["d"]["user_id"].ToString()); if (UserTypingStart != null) { UserTypingStart(this, new DiscordTypingStartEventArgs { user = uuser, channel = channel, timestamp = int.Parse(message["d"]["timestamp"].ToString()) }); } } break; case ("MESSAGE_CREATE"): MessageCreateEvents(message); break; case ("CHANNEL_CREATE"): ChannelCreateEvents(message); break; #if DEBUG default: ni.BalloonTipText = "Check console! New message type!"; ni.ShowBalloonTip(10 * 1000); Console.WriteLine(message); break; #endif } }; ws.OnOpen += (sender, e) => { DiscordInitObj initObj = new DiscordInitObj(); initObj.op = 2; initObj.d.token = this.token; string json = initObj.AsJson(); ws.Send(json); if (SocketOpened != null) { SocketOpened(this, null); } Thread keepAlivetimer = new Thread(KeepAlive); keepAlivetimer.Start(); }; ws.OnClose += (sender, e) => { DiscordSocketClosedEventArgs scev = new DiscordSocketClosedEventArgs(); scev.Code = e.Code; scev.Reason = e.Reason; scev.WasClean = e.WasClean; if (SocketClosed != null) { SocketClosed(this, scev); } }; ws.Connect(); }
public void ChangeGuildIcon(Bitmap image, DiscordServer guild) { Bitmap resized = new Bitmap((Image)image, 200, 200); string base64 = Convert.ToBase64String(Utils.ImageToByteArray(resized)); string type = "image/jpeg;base64"; string req = $"data:{type},{base64}"; string guildjson = JsonConvert.SerializeObject(new { icon = req, name = guild.name}); string url = Endpoints.BaseAPI + Endpoints.Guilds + "/" + guild.id; try { var result = JObject.Parse(WebWrapper.Patch(url, token, guildjson)); } catch(Exception ex) { DebugLogger.Log($"Error ocurred while changing guild {guild.name}'s icon: {ex.Message}", MessageLevel.Error); } }
public List<DiscordRole> GetRoles(DiscordServer server) { return null; }
public void AssignRoleToMember(DiscordServer guild, List<DiscordRole> roles, DiscordMember member) { string url = Endpoints.BaseAPI + Endpoints.Guilds + $"/{guild.id}" + Endpoints.Members + $"/{member.ID}"; List<string> rolesAsIds = new List<string>(); roles.ForEach(x => rolesAsIds.Add(x.id)); string message = JsonConvert.SerializeObject(new { roles = rolesAsIds.ToArray() }); try { WebWrapper.Patch(url, token, message); } catch(Exception ex) { DebugLogger.Log($"Exception ocurred while assigning {roles.Count} role(s) to member ({member.Username}): " + ex.Message, MessageLevel.Error); } }