Skip to content

Commit 85f3b48

Browse files
committed
listadd: allow adding to private lists and auto update lists
1 parent 0a5c4da commit 85f3b48

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

Commands/ListCmds.cs

+29-11
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,21 @@ public async Task ListAdd(
6767
[RemainingText, Description("The text to add the list. Can be in a codeblock and across multiple line.")] string content
6868
)
6969
{
70-
if (Environment.GetEnvironmentVariable("CLIPTOK_GITHUB_TOKEN") is null)
70+
var githubToken = Environment.GetEnvironmentVariable("CLIPTOK_GITHUB_TOKEN");
71+
var githubTokenPrivate = Environment.GetEnvironmentVariable("CLIPTOK_GITHUB_TOKEN_PRIVATE");
72+
73+
if (githubToken is null)
7174
{
7275
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} `CLIPTOK_GITHUB_TOKEN` was not set, so GitHub API commands cannot be used.");
7376
return;
7477
}
75-
7678
if (!fileName.EndsWith(".txt"))
7779
fileName += ".txt";
7880

81+
7982
if (content.Length < 3)
8083
{
81-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Your input is too short, please reconsider.");
84+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Your input is too short, please don't add this to the list.");
8285
return;
8386
}
8487

@@ -88,28 +91,43 @@ public async Task ListAdd(
8891
content = content.Replace("```", "").Trim();
8992

9093
string[] lines = content.Split(
91-
new string[] { "\r\n", "\r", "\n" },
94+
["\r\n", "\r", "\n"],
9295
StringSplitOptions.None
9396
);
9497

9598
if (lines.Any(line => line.Length < 4))
9699
{
97-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} One of your lines is shorter than 4 characters.\nFor safety reasons I can't accept this input over command. Please submit a PR manually.");
100+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} One of your lines is shorter than 4 characters.\nTo prevent accidents I can't accept this input over command. Please submit a PR manually.");
98101
return;
99102
}
100103

101104
string nameToSend = $"{DiscordHelpers.UniqueUsername(ctx.User)}";
102105

106+
var workflowId = Program.cfgjson.GitHubWorkflow.WorkflowId;
107+
var refName = Program.cfgjson.GitHubWorkflow.Ref;
108+
var repoName = Program.cfgjson.GitHubWorkflow.Repo;
109+
110+
if (
111+
Program.cfgjson.GitHubWorkflowPrivate is not null
112+
&& githubTokenPrivate is not null
113+
&& Directory.GetFiles($"Lists/{Program.cfgjson.GitListDirectory}").Any(x => x.EndsWith(fileName)) )
114+
{
115+
workflowId = Program.cfgjson.GitHubWorkflowPrivate.WorkflowId;
116+
refName = Program.cfgjson.GitHubWorkflowPrivate.Ref;
117+
repoName = Program.cfgjson.GitHubWorkflowPrivate.Repo;
118+
githubToken = githubTokenPrivate;
119+
}
120+
103121
using HttpClient httpClient = new()
104122
{
105123
BaseAddress = new Uri("https://api.github.com/")
106124
};
107125

108-
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
109-
httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("Cliptok", "1.0"));
110-
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", Environment.GetEnvironmentVariable("CLIPTOK_GITHUB_TOKEN"));
126+
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
127+
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Cliptok", "1.0"));
128+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", githubToken);
111129

112-
HttpRequestMessage request = new(HttpMethod.Post, $"/repos/{Program.cfgjson.GitHubWorkFlow.Repo}/actions/workflows/{Program.cfgjson.GitHubWorkFlow.WorkflowId}/dispatches");
130+
HttpRequestMessage request = new(HttpMethod.Post, $"/repos/{repoName}/actions/workflows/{workflowId}/dispatches");
113131

114132
GitHubDispatchInputs inputs = new()
115133
{
@@ -120,7 +138,7 @@ public async Task ListAdd(
120138

121139
GitHubDispatchBody body = new()
122140
{
123-
Ref = Program.cfgjson.GitHubWorkFlow.Ref,
141+
Ref = refName,
124142
Inputs = inputs
125143
};
126144

@@ -131,7 +149,7 @@ public async Task ListAdd(
131149

132150
if (response.IsSuccessStatusCode)
133151
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Success} The request was successful.\n" +
134-
$"You should see the result in <#{Program.cfgjson.HomeChannel}> soon or can check at <https://github.com/{Program.cfgjson.GitHubWorkFlow.Repo}/actions>");
152+
$"You should see the result in <#{Program.cfgjson.HomeChannel}> soon or can check at <https://github.com/{repoName}/actions>");
135153
else
136154
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} An error with code `{response.StatusCode}` was returned when trying to request the Action run.\n" +
137155
$"Body: ```json\n{responseText}```");

Events/MessageEvent.cs

+28
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,34 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
289289
}
290290
}
291291

292+
// automatic listupdate for private lists
293+
if (
294+
Program.cfgjson.GitListDirectory is not null
295+
&& Program.cfgjson.GitListDirectory != ""
296+
&& message.Channel.Id == Program.cfgjson.HomeChannel
297+
&& message.Author.Discriminator == "0000"
298+
&& message.Embeds is not null
299+
&& message.Embeds.Count > 0
300+
&& message.Embeds[0].Title.StartsWith("[lists] fileappend success"))
301+
{
302+
string command = $"cd Lists/{Program.cfgjson.GitListDirectory} && git pull";
303+
var msg = await LogChannelHelper.LogMessageAsync("home", $"{Program.cfgjson.Emoji.Loading} Updating private lists..");
304+
305+
ShellResult finishedShell = RunShellCommand(command);
306+
307+
string result = Regex.Replace(finishedShell.result, "(?:ghp)|(?:github_pat)_[0-9a-zA-Z_]+", "ghp_REDACTED").Replace(Environment.GetEnvironmentVariable("CLIPTOK_TOKEN"), "REDACTED");
308+
309+
if (finishedShell.proc.ExitCode != 0)
310+
{
311+
await msg.ModifyAsync($"{Program.cfgjson.Emoji.Error} An error occurred trying to update private lists!\n```\n{result}\n```");
312+
}
313+
else
314+
{
315+
Program.UpdateLists();
316+
await msg.ModifyAsync($"{Program.cfgjson.Emoji.Success} Successfully updated and reloaded private lists!\n```\n{result}\n```");
317+
}
318+
}
319+
292320
// Skip DMs, external guilds, and messages from bots, beyond this point.
293321
if (channel.IsPrivate || channel.Guild.Id != Program.cfgjson.ServerID || message.Author.IsBot)
294322
return;

Structs.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ public class ConfigJson
216216
public string GiveawayTriggerMessage { get; private set; }
217217

218218
[JsonProperty("githubWorkflow")]
219-
public WorkflowConfig GitHubWorkFlow { get; private set; }
219+
public WorkflowConfig GitHubWorkflow { get; private set; }
220+
221+
[JsonProperty("githubWorkflowPrivate")]
222+
public WorkflowConfig GitHubWorkflowPrivate { get; private set; }
220223

221224
[JsonProperty("everyoneExcludedChannels")]
222225
public List<ulong> EveryoneExcludedChannels { get; private set; } = new();
@@ -330,6 +333,9 @@ public ulong InsidersChannel
330333

331334
[JsonProperty("pingBotOwnersOnBadErrors")]
332335
public bool PingBotOwnersOnBadErrors { get; private set; } = false;
336+
337+
[JsonProperty("githubWorkflowSucessString")]
338+
public string HithubWorkflowSucessString { get; private set; } = "";
333339
}
334340

335341
public enum Level { Information, Warning, Error, Debug, Verbose }

config.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@
252252
"ref": "main",
253253
"workflowId": "append-lists-txt.yml"
254254
},
255+
"githubWorkflowPrivate": {
256+
"repo": "Microsoft-community/lists",
257+
"ref": "main",
258+
"workflowId": "append-lists-txt.yml"
259+
},
255260
"everyoneFilter": true,
256261
"logChannels": {
257262
"mod": {
@@ -327,7 +332,7 @@
327332
"logLevel": "Debug",
328333
"lokiURL": "http://100.79.19.82:3100",
329334
"lokiServiceName": "cliptok",
330-
"voiceChannelPurge": true,
335+
"voiceChannelPurge": true,
331336
"forumChannelAutoWarnFallbackChannel": 150662382874525696,
332337
"rulesAllowedPublicChannels": [
333338
150909451270881280,
@@ -342,5 +347,6 @@
342347
593381041629298688,
343348
450181490345508884
344349
],
345-
"pingBotOwnersOnBadErrors": true
350+
"pingBotOwnersOnBadErrors": true,
351+
"githubWorkflowSucessString": "[lists:main] 1 new commit"
346352
}

0 commit comments

Comments
 (0)