internal DiscordIntegration(DiscordHttpClient http, DiscordApiData data) : base(data) { this.http = http; Name = data.GetString("name"); Type = data.GetString("type"); IsEnabled = data.GetBoolean("enabled"); IsSyncing = data.GetBoolean("syncing"); ExpireBehavior = data.GetInteger("expire_behavior"); ExpireGracePeriod = data.GetInteger("expire_grace_period"); SyncedAt = data.GetDateTime("synced_at"); RoleId = data.GetSnowflake("role_id"); DiscordApiData userData = data.Get("user"); if (userData != null) { User = new DiscordUser(false, userData); } DiscordApiData accountData = data.Get("account"); if (accountData != null) { Account = new DiscordIntegrationAccount(accountData); } }
internal DiscordGuildStoreChannel(DiscordHttpClient http, DiscordApiData data, Snowflake?guildId = null) : base(http, data, DiscordChannelType.GuildStore, guildId) { this.http = http; Nsfw = data.GetBoolean("nsfw") ?? false; ParentId = data.GetSnowflake("parent_id"); }
internal DiscordUserPresence(Snowflake userId, DiscordApiData data) { UserId = userId; // Game DiscordApiData gameData = data.Get("game"); if (gameData != null) { if (gameData.IsNull) { Game = null; } else { Game = new DiscordGame(gameData); } } // Status string statusStr = data.GetString("status"); if (statusStr != null) { DiscordUserStatus?status = Utils.ParseUserStatus(statusStr); if (!status.HasValue) { // If we don't have a value for the status yet, // we at least know that they aren't offline. Status = DiscordUserStatus.Online; // However, this should issue a warning. DiscoreLogger.Global.LogWarning($"[DiscordUserPresence] Failed to deserialize status for user {UserId}. " + $"status = {statusStr}"); } else { Status = status.Value; } } // Client status DiscordApiData clientStatusData = data.Get("client_status"); if (clientStatusData != null) { ClientStatus = new DiscordClientStatus(clientStatusData); } // Activites IList <DiscordApiData> activitiesArray = data.GetArray("activities"); if (activitiesArray != null) { Activities = activitiesArray.Select(a => new DiscordGame(a)).ToArray(); } }
/// <summary> /// Sets a snowflake value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData SetSnowflake(string key, Snowflake snowflake) { AssertContainer(); DiscordApiData apiValue = new DiscordApiData(snowflake); data[key] = apiValue; return(apiValue); }
/// <summary> /// Sets a snowflake value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData SetSnowflake(string key, Snowflake?snowflake) { AssertContainer(); DiscordApiData apiValue = snowflake.HasValue ? new DiscordApiData(snowflake.Value) : new DiscordApiData(value: null); data[key] = apiValue; return(apiValue); }
/// <summary> /// Sets a DiscordColor value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData Set(string key, DiscordColor color) { AssertContainer(); DiscordApiData apiData = new DiscordApiData(color.ToHexadecimal()); data[key] = apiData; return(apiData); }
internal DiscordGuildVoiceChannel(DiscordHttpClient http, DiscordApiData data, Snowflake?guildId = null) : base(http, data, DiscordChannelType.GuildVoice, guildId) { this.http = http; Bitrate = data.GetInteger("bitrate").Value; UserLimit = data.GetInteger("user_limit").Value; ParentId = data.GetSnowflake("parent_id"); }
/// <summary> /// Sets a DiscordColor value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData Set(string key, DiscordColor?color) { AssertContainer(); DiscordApiData apiData = color.HasValue ? new DiscordApiData(color.Value) : new DiscordApiData(value: null); data[key] = apiData; return(apiData); }
internal DiscordGuildEmbed(DiscordHttpClient http, Snowflake guildId, DiscordApiData data) { this.http = http; GuildId = guildId; Enabled = data.GetBoolean("enabled").Value; ChannelId = data.GetSnowflake("channel_id").Value; }
public DiscordApiData Set(string key, DateTime dateTime) { AssertContainer(); DiscordApiData apiData = new DiscordApiData(dateTime); data[key] = apiData; return(apiData); }
/// <summary> /// Sets a value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData Set(string key, object value) { AssertContainer(); DiscordApiData apiValue = new DiscordApiData(value); data[key] = apiValue; return(apiValue); }
public DiscordApiData Set(string key, DiscordImageData avatar) { AssertContainer(); DiscordApiData apiData = new DiscordApiData(avatar.ToDataUriScheme()); data[key] = apiData; return(apiData); }
/// <summary> /// Updates this data with the specified data, overwriting any conflicts with the new data. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data or the given data is not a container.</exception> public void OverwriteUpdate(DiscordApiData data) { AssertContainer(); data.AssertContainer(); foreach (KeyValuePair <string, DiscordApiData> pair in data.Entries) { this.data[pair.Key] = pair.Value; } }
internal DiscordGame(DiscordApiData data) { Name = data.GetString("name"); Type = (DiscordGameType)(data.GetInteger("type") ?? 0); Url = data.GetString("url"); CreatedAt = data.GetInt64("created_at"); ApplicationId = data.GetSnowflake("application_id"); Details = data.GetString("details"); State = data.GetString("state"); Instance = data.GetBoolean("instance"); Flags = (DiscordActivityFlag)(data.GetInteger("flags") ?? 0); DiscordApiData timestampsData = data.Get("timestamps"); if (timestampsData != null) { Timestamps = new DiscordActivityTimestamps(timestampsData); } DiscordApiData emojiData = data.Get("emoji"); if (emojiData != null) { Emoji = new DiscordActivityEmoji(emojiData); } DiscordApiData partyData = data.Get("party"); if (partyData != null) { Party = new DiscordActivityParty(partyData); } DiscordApiData assetsData = data.Get("assets"); if (assetsData != null) { Assets = new DiscordActivityAssets(assetsData); } DiscordApiData secretsData = data.Get("secrets"); if (secretsData != null) { Secrets = new DiscordActivitySecrets(secretsData); } IList <DiscordApiData> buttonsArray = data.GetArray("buttons"); if (buttonsArray != null) { Buttons = buttonsArray.Select(d => d.ToString()).ToArray(); } }
internal DiscordGuildNewsChannel(DiscordHttpClient http, DiscordApiData data, Snowflake?guildId = null) : base(http, data, DiscordChannelType.GuildNews, guildId) { this.http = http; Topic = data.GetString("topic"); Nsfw = data.GetBoolean("nsfw") ?? false; ParentId = data.GetSnowflake("parent_id"); lastMessageId = data.GetSnowflake("last_message_id") ?? default(Snowflake); }
internal DiscordDMChannel(DiscordHttpClient http, DiscordApiData data) : base(http, data, DiscordChannelType.DirectMessage) { this.http = http; lastMessageId = data.GetSnowflake("last_message_id") ?? default(Snowflake); // Normal DM should only ever have exactly one recipient DiscordApiData recipientData = data.GetArray("recipients").First(); Recipient = new DiscordUser(false, recipientData); }
/// <summary> /// Attempts to create a DiscordApiData object from a JSON string. /// </summary> /// <returns>Returns whether the conversion was successful.</returns> public static bool TryParseJson(string json, out DiscordApiData data) { try { data = ParseJson(json); return(true); } catch (Exception) { data = null; return(false); } }
void ApiDataArrayToJson(IList <DiscordApiData> apiDataArray, JsonTextWriter writer) { writer.WriteStartArray(); for (int i = 0; i < apiDataArray.Count; i++) { DiscordApiData apiData = apiDataArray[i]; ApiDataToJson(apiData, writer); } writer.WriteEndArray(); }
void ApiDataToJson(DiscordApiData data, JsonTextWriter writer) { if (data.Type == DiscordApiDataType.Container) { ApiDataContainerToJson(data, writer); } else if (data.Type == DiscordApiDataType.Array) { ApiDataArrayToJson(data.values, writer); } else { writer.WriteValue(data.value); } }
void ApiDataContainerToJson(DiscordApiData apiData, JsonTextWriter writer) { writer.WriteStartObject(); foreach (KeyValuePair <string, DiscordApiData> pair in apiData.data) { string key = pair.Key; DiscordApiData data = pair.Value; writer.WritePropertyName(key); ApiDataToJson(data, writer); } writer.WriteEndObject(); }
/// <summary> /// Sets a value in this api data container. /// </summary> /// <exception cref="InvalidOperationException">Thrown if this data is not a container.</exception> public DiscordApiData Set <T>(string key, IList <T> array) { AssertContainer(); List <DiscordApiData> dataList = new List <DiscordApiData>(); for (int i = 0; i < array.Count; i++) { dataList.Add(new DiscordApiData(array[i])); } DiscordApiData arrayValue = new DiscordApiData(dataList); data[key] = arrayValue; return(arrayValue); }
internal DiscordInviteMetadata(DiscordHttpClient http, DiscordApiData data) : base(http, data) { DiscordApiData inviterData = data.Get("inviter"); if (inviterData != null) { Inviter = new DiscordUser(false, inviterData); } Uses = data.GetInteger("uses").Value; MaxUses = data.GetInteger("max_uses").Value; MaxAge = data.GetInteger("max_age").Value; IsTemporary = data.GetBoolean("temporary").Value; CreatedAt = data.GetDateTime("created_at").Value; IsRevoked = data.GetBoolean("revoked") ?? false; }
internal DiscordUser(bool isWebhookUser, DiscordApiData data) : base(data) { IsWebhookUser = isWebhookUser; Username = data.GetString("username"); Discriminator = data.GetString("discriminator"); IsBot = data.GetBoolean("bot") ?? false; HasTwoFactorAuth = data.GetBoolean("mfa_enabled") ?? false; IsVerified = data.GetBoolean("verified") ?? false; Email = data.GetString("email"); string avatarHash = data.GetString("avatar"); if (avatarHash != null) { Avatar = DiscordCdnUrl.ForUserAvatar(Id, avatarHash); } }
internal DiscordInvite(DiscordHttpClient http, DiscordApiData data) { this.http = http; Code = data.GetString("code"); DiscordApiData guildData = data.Get("guild"); if (guildData != null) { Guild = new DiscordInviteGuild(guildData); } DiscordApiData channelData = data.Get("channel"); if (channelData != null) { Channel = new DiscordInviteChannel(channelData); } }
internal DiscordEmoji(DiscordApiData data) : base(data) { Name = data.GetString("name"); UserId = data.LocateSnowflake("user.id"); RequireColons = data.GetBoolean("require_colons") ?? false; IsManaged = data.GetBoolean("managed") ?? false; IsAnimated = data.GetBoolean("animated") ?? false; IList <DiscordApiData> roles = data.GetArray("roles"); Snowflake[] roleIds = new Snowflake[roles.Count]; for (int i = 0; i < roleIds.Length; i++) { roleIds[i] = (roles[i].ToSnowflake().Value); } RoleIds = roleIds; }
internal DiscordGuildChannel(DiscordHttpClient http, DiscordApiData data, DiscordChannelType type, Snowflake?guildId) : base(http, data, type) { this.http = http; GuildId = guildId ?? data.GetSnowflake("guild_id").Value; Name = data.GetString("name"); Position = data.GetInteger("position").Value; IList <DiscordApiData> overwrites = data.GetArray("permission_overwrites"); Dictionary <Snowflake, DiscordOverwrite> permissionOverwrites = new Dictionary <Snowflake, DiscordOverwrite>(); for (int i = 0; i < overwrites.Count; i++) { DiscordOverwrite overwrite = new DiscordOverwrite(http, Id, overwrites[i]); permissionOverwrites.Add(overwrite.Id, overwrite); } PermissionOverwrites = permissionOverwrites; }
internal DiscordMessageMember(DiscordApiData data) { Nickname = data.GetString("nick"); JoinedAt = data.GetDateTime("joined_at").Value; IsDeaf = data.GetBoolean("deaf") ?? false; IsMute = data.GetBoolean("mute") ?? false; IList <DiscordApiData> rolesArray = data.GetArray("roles"); if (rolesArray != null) { Snowflake[] roleIds = new Snowflake[rolesArray.Count]; for (int i = 0; i < rolesArray.Count; i++) { roleIds[i] = rolesArray[i].ToSnowflake().Value; } RoleIds = roleIds; } }
internal DiscordRole(DiscordHttpClient http, Snowflake guildId, DiscordApiData data) : base(data) { this.http = http; GuildId = guildId; Name = data.GetString("name"); IsHoisted = data.GetBoolean("hoist").Value; Position = data.GetInteger("position").Value; IsManaged = data.GetBoolean("managed").Value; IsMentionable = data.GetBoolean("mentionable").Value; int color = data.GetInteger("color").Value; Color = DiscordColor.FromHexadecimal(color); long permissions = data.GetInt64("permissions").Value; Permissions = (DiscordPermission)permissions; }
/// <summary> /// Updates a message with a newer partial version of the same message. This is primarily used /// for obtaining the full message from a message update event, which only supplies the changes /// rather than the full message. /// </summary> /// <exception cref="ArgumentException">Thrown if the IDs of each message do not match.</exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="DiscordHttpApiException"></exception> public static DiscordMessage Update(DiscordMessage message, DiscordMessage withPartial) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (withPartial == null) { throw new ArgumentNullException(nameof(withPartial)); } if (message.Id != withPartial.Id) { throw new ArgumentException("Cannot update one message with another. The message IDs must match."); } DiscordApiData updatedData = message.originalData.Clone(); updatedData.OverwriteUpdate(withPartial.originalData); return(new DiscordMessage(message.http, updatedData)); }
internal DiscordOverwrite(DiscordHttpClient http, Snowflake channelId, DiscordApiData data) : base(data) { this.http = http; ChannelId = channelId; string typeStr = data.GetString("type"); DiscordOverwriteType type; if (Enum.TryParse(typeStr, true, out type)) { Type = type; } long allow = data.GetInt64("allow").Value; Allow = (DiscordPermission)allow; long deny = data.GetInt64("deny").Value; Deny = (DiscordPermission)deny; }