-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
DiscordApiClientHook.cs
167 lines (144 loc) · 6.16 KB
/
DiscordApiClientHook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections.Generic;
using System.Threading.Tasks;
using DisCatSharp.Attributes;
using DisCatSharp.Entities;
using DisCatSharp.Experimental.Entities;
using DisCatSharp.Experimental.Payloads;
using DisCatSharp.Net;
using DisCatSharp.Net.Serialization;
using Newtonsoft.Json.Linq;
namespace DisCatSharp.Experimental;
/// <summary>
/// Represents a hook for the discord api client.
/// </summary>
internal sealed class DiscordApiClientHook
{
/// <summary>
/// Gets the api client.
/// </summary>
internal DiscordApiClient ApiClient { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="DiscordApiClientHook"/> class.
/// </summary>
/// <param name="apiClient">The api client.</param>
internal DiscordApiClientHook(DiscordApiClient apiClient)
{
this.ApiClient = apiClient;
}
/// <summary>
/// Gets the clyde profile for the given <paramref name="profileId"/>.
/// </summary>
/// <param name="profileId">The profile id to get.</param>
[DiscordDeprecated]
internal async Task<ClydeProfile> GetClydeProfileAsync(ulong profileId)
{
var route = $"{Endpoints.CLYDE_PROFILES}/:profile_id";
var bucket = this.ApiClient.Rest.GetBucket(RestRequestMethod.GET, route, new
{
profile_id = profileId
}, out var path);
var url = Utilities.GetApiUriFor(path, this.ApiClient.Discord.Configuration);
var res = await this.ApiClient.DoRequestAsync(this.ApiClient.Discord, bucket, url, RestRequestMethod.GET, route).ConfigureAwait(false);
var profile = DiscordJson.DeserializeObject<ClydeProfile>(res.Response, this.ApiClient.Discord);
return profile;
}
/// <summary>
/// Gets the clyde settings for the given <paramref name="guildId"/>.
/// </summary>
/// <param name="guildId">The guild id to get clyde's settings for.</param>
[DiscordDeprecated]
internal async Task<ClydeSettings> GetClydeSettingsAsync(ulong guildId)
{
var route = $"{Endpoints.GUILDS}/:guild_id{Endpoints.CLYDE_SETTINGS}";
var bucket = this.ApiClient.Rest.GetBucket(RestRequestMethod.GET, route, new
{
guild_id = guildId
}, out var path);
var url = Utilities.GetApiUriFor(path, this.ApiClient.Discord.Configuration);
var res = await this.ApiClient.DoRequestAsync(this.ApiClient.Discord, bucket, url, RestRequestMethod.GET, route).ConfigureAwait(false);
var settings = DiscordJson.DeserializeObject<ClydeSettings>(res.Response, this.ApiClient.Discord);
return settings;
}
/// <summary>
/// Modifies the clyde settings for the given <paramref name="guildId"/> by applying a <paramref name="profileId"/>.
/// </summary>
/// <param name="guildId">The guild id to modify clyde's settings for.</param>
/// <param name="profileId">The profile id to apply.</param>
[DiscordDeprecated]
internal async Task<ClydeSettings> ModifyClydeSettingsAsync(ulong guildId, ulong profileId)
{
ClydeSettingsProfileIdOnlyUpdatePayload pld = new()
{
ClydeProfileId = profileId
};
var route = $"{Endpoints.GUILDS}/:guild_id{Endpoints.CLYDE_SETTINGS}";
var bucket = this.ApiClient.Rest.GetBucket(RestRequestMethod.PATCH, route, new
{
guild_id = guildId
}, out var path);
var url = Utilities.GetApiUriFor(path, this.ApiClient.Discord.Configuration);
var res = await this.ApiClient.DoRequestAsync(this.ApiClient.Discord, bucket, url, RestRequestMethod.PATCH, route, payload: DiscordJson.SerializeObject(pld)).ConfigureAwait(false);
var obj = JObject.Parse(res.Response);
var settingsString = obj.GetValue("settings")!.ToString();
var settings = DiscordJson.DeserializeObject<ClydeSettings>(settingsString, this.ApiClient.Discord);
return settings;
}
/// <summary>
/// Modifies the clyde settings for the given <paramref name="guildId"/>.
/// </summary>
/// <param name="guildId">The guild id to modify clyde's settings for.</param>
/// <param name="name">The new name.</param>
/// <param name="personality">The new basePersonality.</param>
/// <param name="avatarBase64">The new avatar.</param>
/// <param name="bannerBase64">The new banner.</param>
/// <param name="themeColors">The new theme colors.</param>
[DiscordDeprecated]
internal async Task<ClydeSettings> ModifyClydeSettingsAsync(
ulong guildId,
Optional<string?> name,
Optional<string> personality,
Optional<string?> avatarBase64,
Optional<string?> bannerBase64,
Optional<List<int>?> themeColors
)
{
ClydeSettingsProfileUpdatePayload pld = new()
{
Nick = name,
Personality = personality,
Avatar = avatarBase64,
Banner = bannerBase64,
ThemeColors = themeColors
};
var route = $"{Endpoints.GUILDS}/:guild_id{Endpoints.CLYDE_SETTINGS}";
var bucket = this.ApiClient.Rest.GetBucket(RestRequestMethod.PATCH, route, new
{
guild_id = guildId
}, out var path);
var url = Utilities.GetApiUriFor(path, this.ApiClient.Discord.Configuration);
var res = await this.ApiClient.DoRequestAsync(this.ApiClient.Discord, bucket, url, RestRequestMethod.PATCH, route, payload: DiscordJson.SerializeObject(pld)).ConfigureAwait(false);
var obj = JObject.Parse(res.Response);
var settingsString = obj.GetValue("settings")!.ToString();
var settings = DiscordJson.DeserializeObject<ClydeSettings>(settingsString, this.ApiClient.Discord);
return settings;
}
/// <summary>
/// Generates a basePersonality for clyde based on the given <paramref name="basePersonality"/>.
/// </summary>
/// <param name="basePersonality">The base base personality to generate a new one from.</param>
[DiscordDeprecated]
internal async Task<string> GenerateClydePersonalityAsync(string? basePersonality = null)
{
PersonalityGenerationPayload pld = new()
{
Personality = basePersonality ?? string.Empty
};
var route = $"{Endpoints.CLYDE_PROFILES}{Endpoints.GENERATE_PERSONALITY}";
var bucket = this.ApiClient.Rest.GetBucket(RestRequestMethod.POST, route, new
{ }, out var path);
var url = Utilities.GetApiUriFor(path, this.ApiClient.Discord.Configuration);
var res = await this.ApiClient.DoRequestAsync(this.ApiClient.Discord, bucket, url, RestRequestMethod.POST, route, payload: DiscordJson.SerializeObject(pld)).ConfigureAwait(false);
var generatedPersonality = DiscordJson.DeserializeObject<PersonalityGenerationPayload>(res.Response, this.ApiClient.Discord);
return generatedPersonality.Personality;
}
}