-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadarrCommand.cs
278 lines (213 loc) · 13.8 KB
/
RadarrCommand.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using DSharpPlus.Entities;
using DSharpPlus;
using DSharpPlus.SlashCommands;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using DSharpPlus.Interactivity.Extensions;
using System.Text;
namespace XilftenBot
{
/// <summary>
/// Application command for Radarr
/// </summary>
public class RadarrCommand : ApplicationCommandModule
{
//Application Command for Adding Movies to Radarr
[SlashCommand("Movie", "Search for and add Movie!")]
public async Task MovieCommand(InteractionContext ctx, [Option("Movie", "Name of Movie")] string movieName)
{
//Check if Radarr is Activated.
if (Xilften.radarrActivated)
{
//Creates a Delayed Response to the Interaction.(Allows us to Respond to the Interaction later by editing the response.)
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);
DiscordMember member = ctx.Member;
Console.WriteLine($"Movie Search Recieved:{movieName} by User {member.Username}");
//search to see if movie exists.
if (movieName != null)
{
try
{
// Create a new HttpClient and Radarr API request.
using (var httpClient = new HttpClient())
{
// Add API key to request headers
httpClient.DefaultRequestHeaders.Add("X-Api-Key", Xilften.radarrAPI);
string requestUrl = Xilften.radarrIP + $"api/v3/movie/lookup?term={movieName}";
// Send GET request to Radarr
var response = httpClient.GetAsync(requestUrl).Result;
var json = httpClient.GetAsync(requestUrl).Result.Content.ReadAsStringAsync().Result;
// Check if request was successful
if (response.IsSuccessStatusCode)
{
string jsonContent = json.ToString();
//Var to hold the JSON response to build Request.(Do not Modify)
string path = "";
string imdbId = "";
string status = "";
bool searchForMovie = true;
string monitor = "movieOnly";
bool monitored = true;
// Deserialize JSON response (many Movies) into an Array.
JArray objArray = JsonConvert.DeserializeObject<JArray>(json);
//Gets the first Movie in the Array and converts it to a json string.
var getMovieObj = objArray[0];
var getMovieString = getMovieObj.ToString();
//Use Try Catch to get the values from the JSON so empty doesnt Crash bot.
try { imdbId = getMovieObj["imdbId"].ToString(); } catch { }
try { path = getMovieObj["path"].ToString(); } catch { }
try { status = getMovieObj["status"].ToString(); } catch { }
// Deserialize JSON String into a Movie Object.
var movieDeserializedGet = JsonConvert.DeserializeObject<Movie>(getMovieString);
//Create a new Request Object.
var newRequest = new Request
{
memberName = member.Nickname,
request = movieName,
dateMade = DateTime.Now,
};
if (path != "") //If we do not have a path then the movie is not on the server, Check to see if we should add it and add it if we should.
{
//Create an Embed to send
var em = new DiscordEmbedBuilder
{
Color = DiscordColor.SpringGreen,
Title = $"{movieDeserializedGet.title}",
//Url = "https://www.imdb.com/title/" + imdbId,
Description = $"Movie is already on the server."
};
//If we have an IMDB ID, add it to the Embed.
if (imdbId != "")
{
em.WithUrl("https://www.imdb.com/title/" + imdbId);
}
em.AddField("Added on: ", $"{movieDeserializedGet.added}", true);
em.AddField("Status ", $"{status}", true);
em.AddField("IMDB Id: ", $"{movieDeserializedGet.imdbId}", true);
em.AddField("Release year: ", $"{movieDeserializedGet.year}", true);
em.AddField("Overview: ", $"{movieDeserializedGet.overview}", false);
em.Build();
//Create the Webhook and add the Embed.
var wh = new DiscordWebhookBuilder();
wh.AddEmbed(em);
await ctx.EditResponseAsync(wh);
//Update if Request was added, and add it to the Requests List.
newRequest.wasAdded = false;
Xilften.WriteToLog($"{member.Username} requested movie {movieDeserializedGet.title} it Existed on the Server.");
Xilften.requests.Add(DateTime.Now, newRequest);
}
else if (path == "")
{
// Building Embed
var em = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Title = $"{movieDeserializedGet.title}",
Url = "https://www.imdb.com/title/" + imdbId,
Description = $"This movie is not on the server."
};
if (imdbId != "")
{
em.WithUrl("https://www.imdb.com/title/" + imdbId);
}
var rootfolder= Xilften.radarrRootPath;
//Choose the Emoji that will be used to react to the Embed
var emoji = DiscordEmoji.FromName(ctx.Client, ":ok_hand:");
em.AddField("IMDB Id: ", $"{movieDeserializedGet.imdbId}", true);
em.AddField("Status ", $"{status}", true);
em.AddField("Release year: ", $"{movieDeserializedGet.year}", true);
em.AddField("Overview: ", $"{movieDeserializedGet.overview}", false);
//Creates a Blank Field to make the Embed look better
em.AddField("\u200b", "\u200b");
em.AddField($"{ctx.Member.Username},", $"React with {emoji} within 30 seconds to add {movieDeserializedGet.title} to the server!", false);
em.Build();
//adding Embed to Webhook
var wh = new DiscordWebhookBuilder();
wh.AddEmbed(em);
//Update the Interaction Response with the Embed
var message = await ctx.EditResponseAsync(wh);
//waiting for reaction
var result = await message.WaitForReactionAsync(ctx.Member, emoji);
//if reacted to in time Send Add Post to Server
if (!result.TimedOut)
{
string postUrl = Xilften.radarrIP + $"api/v3/movie";
movieDeserializedGet.rootFolderPath = rootfolder;
movieDeserializedGet.qualityProfileId = 1;
movieDeserializedGet.SetAddOptions(searchForMovie, monitor);
string updatedPost = JsonConvert.SerializeObject(movieDeserializedGet, Formatting.Indented);
var payload = new StringContent(updatedPost, Encoding.UTF8, "application/json");
var postResponse = httpClient.PostAsync(postUrl, payload).Result.Content.ReadAsStringAsync();
var postResults = postResponse.Result;
//Here we check to see if post returned successfully
if (postResponse.IsCompletedSuccessfully)
{
Console.WriteLine($"The movie {movieDeserializedGet.title} was added to Radarr.");
await ctx.Channel.SendMessageAsync($" The movie {movieDeserializedGet.title} is adding to the server. Allow 30 min to 1 hour");
newRequest.wasAdded = true;
Xilften.WriteToLog($"{member.Username} requested movie {movieDeserializedGet.title} it Existed on the Server.");
Xilften.requests.Add(DateTime.Now, newRequest);
}
else //If not successful send error message
{
Console.WriteLine("Post sent and response Recieved Error. ");
await ctx.Channel.SendMessageAsync("There was an error adding the movie");
newRequest.wasAdded = false;
Xilften.WriteToLog($"{member.Username} requested movie {movieDeserializedGet.title}, but there was an error. The Post did not complete Successfully.");
Xilften.requests.Add(DateTime.Now, newRequest);
}
}
}
}
}
}
catch(Exception ex)// There is no connection to Radarr Send Error Message.
{
Console.WriteLine($"Post sent and response Recieved Error.{ex.Message} ");
await ctx.Channel.SendMessageAsync("There was an error adding the show");
Xilften.WriteToLog($"{ctx.Member.Username} requested show {movieName} but there was an Error. Error Message : {ex.Message}");
}
}
}
else //Radarr Is not active on this server send error message.(Likely Missing the Radarr API Key or Radarr IP)
{
await ctx.Channel.SendMessageAsync("Radarr is not Activated on this Server.");
Xilften.WriteToLog($"{ctx.Member.Username} requested show {movieName} but Radarr is not activated on this Server.");
}
}
[SlashCommand("RestartRadarr", "Restart the Radarr Server!")]
public async Task RestartSonarrCommand(InteractionContext ctx)
{
DiscordMember user = ctx.Member;
DiscordChannel channel = ctx.Channel;
if (user.Permissions.HasPermission(Permissions.Administrator))
{
using (var httpClient = new HttpClient())
{
// Add API key to request headers
httpClient.DefaultRequestHeaders.Add("X-Api-Key", Xilften.radarrAPI);
// Send Post request to Radarr
string requestUrl = Xilften.radarrIP + $"api/v3/system/restart";
var response = httpClient.GetAsync(requestUrl).Result;
var json = httpClient.GetAsync(requestUrl).Result.Content.ReadAsStringAsync().Result;
// Check if request was successful
if (response.IsSuccessStatusCode)
{
var em = new DiscordEmbedBuilder
{
Color = DiscordColor.Azure,
Title = $"Radarr Reseting",
Description = $"Please Allow a Few Minutes."
};
em.Build();
await channel.SendMessageAsync(embed: em);
}
else
{
await channel.SendMessageAsync("Error Reseting RAdarr");
}
}
}
}
}
}