Skip to content

Commit 3e3529e

Browse files
authored
Upgrade to dotnet 9 and latest Nuget packages (#3)
1 parent e64bdaa commit 3e3529e

8 files changed

Lines changed: 89 additions & 94 deletions

File tree

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:8.0 as builder
1+
FROM mcr.microsoft.com/dotnet/sdk:9.0 as builder
22
ARG TARGETPLATFORM
33
ARG BUILDPLATFORM
44

55
# Copy the source to docker container
66
COPY ./src /usr/src
77
RUN dotnet publish -c Release /usr/src/netdaemonbot.csproj -o "/publish"
88

9-
FROM mcr.microsoft.com/dotnet/aspnet:8.0
9+
FROM mcr.microsoft.com/dotnet/aspnet:9.0
1010
# RUN mkdir /app
1111
# Set the working directory to /app
1212
WORKDIR /app
1313

1414
COPY --from=builder /publish .
1515
# Start the application
16-
ENTRYPOINT ["dotnet", "netdaemonbot.dll"]
16+
ENTRYPOINT ["dotnet", "netdaemonbot.dll"]

src/BotParser.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System.Text.RegularExpressions;
2-
using System.Linq;
3-
using System.Threading.Tasks;
4-
using System.Collections.Generic;
5-
using netdaemonbot;
62

73
namespace netdaemonbot;
84

@@ -80,8 +76,8 @@ public class BotParser : IMessage
8076

8177
#region -- Parse expressions --
8278

83-
static Regex _exCommand = new Regex(@"(<@!\d+>)*\s*(?'command'\w+)\s*(?'argument'.*)");
84-
static Regex _exQuery = new Regex(@"(<@!\d+>)*\s*(?'query'.+)\?");
79+
static readonly Regex _exCommand = new(@"(<@!\d+>)*\s*(?'command'\w+)\s*(?'argument'.*)");
80+
static readonly Regex _exQuery = new(@"(<@!\d+>)*\s*(?'query'.+)\?");
8581

8682
#endregion
8783

@@ -97,9 +93,9 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,
9793
{
9894
// Parse queries
9995
Match? matchQuery = _exQuery.Matches(message).FirstOrDefault();
100-
if (matchQuery is object)
96+
if (matchQuery is not null)
10197
{
102-
foreach (Group? group in matchQuery.Groups)
98+
foreach (Group? group in matchQuery.Groups.Cast<Group?>())
10399
{
104100
if (group?.Name == "query")
105101
Query = string.IsNullOrEmpty(group.Value) ? null : group.Value;
@@ -110,9 +106,9 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,
110106
}
111107

112108
Match? match = _exCommand.Matches(message).FirstOrDefault();
113-
if (match is object)
109+
if (match is not null)
114110
{
115-
foreach (Group? group in match.Groups)
111+
foreach (Group? group in match.Groups.Cast<Group?>())
116112
{
117113
if (group?.Name == "command")
118114
Command = string.IsNullOrEmpty(group.Value) ? null : group.Value.ToLowerInvariant();
@@ -122,4 +118,4 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,
122118

123119
}
124120
}
125-
}
121+
}

src/BotResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public class BotResult
1919
/// <typeparam name="field">Field name</typeparam>
2020
/// <typeparam name="text">Text in field</typeparam>
2121
/// <returns></returns>
22-
public List<(string field, string text)> Fields { get; set; } = new List<(string, string)>();
22+
public List<(string field, string text)> Fields { get; set; } = [];
2323

24-
}
24+
}

src/BotService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System.Reflection;
22
using System.Text;
33
using DSharpPlus;
4-
using DSharpPlus.Entities;
5-
using Microsoft.Extensions.Hosting;
6-
using Microsoft.Extensions.Logging;
74

85
namespace netdaemonbot;
96

@@ -122,4 +119,4 @@ public Task StopAsync(CancellationToken cancellationToken)
122119
logger.LogInformation("Stopping BotService...");
123120
return Task.CompletedTask;
124121
}
125-
}
122+
}

src/Plugins/AlgoliaSearch.cs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-

2-
3-
using System.Text;
1+
using System.Text;
42
using Algolia.Search.Clients;
3+
using Algolia.Search.Models.Search;
54
using netdaemonbot;
65

76
/// <summary>
@@ -18,11 +17,12 @@ public class AlgoliaPlugin : IBotPlugin
1817
private int _orderOfProcessingMessages = 0;
1918
private ILogger _logger;
2019
SearchClient _algoliaSearchClient;
21-
SearchIndex _algoliaIndex;
20+
/*SearchIndex _algoliaIndex;*/
2221
private readonly string _searchCommand;
2322
private readonly bool _isDefaultSearch;
2423
private readonly string _searchCommandHelp;
2524
private readonly string _searchDescriptionHelp;
25+
private readonly string _indexName;
2626

2727
/// <summary>
2828
/// Constructor
@@ -44,7 +44,7 @@ public AlgoliaPlugin(
4444
{
4545
_ = appId ?? throw new ArgumentNullException(nameof(appId));
4646
_ = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
47-
_ = indexName ?? throw new ArgumentNullException(nameof(apiKey));
47+
_indexName = indexName ?? throw new ArgumentNullException(nameof(apiKey));
4848

4949
_searchCommand = searchCommand;
5050
_isDefaultSearch = searchCommand == "search" ? true : false;
@@ -54,7 +54,8 @@ public AlgoliaPlugin(
5454

5555
_logger = loggerFactory.CreateLogger<AlgoliaPlugin>();
5656
_algoliaSearchClient = new SearchClient(appId, apiKey);
57-
_algoliaIndex = _algoliaSearchClient.InitIndex(indexName);
57+
/*_algoliaIndex = _algoliaSearchClient.InitIndex(indexName);*/
58+
5859
_orderOfProcessingMessages = order;
5960
}
6061

@@ -126,43 +127,55 @@ private async Task<BotResult> QueryMessage(string query)
126127
var returnList = new List<(string, string)>();
127128
try
128129
{
129-
var query = new Algolia.Search.Models.Search.Query(userQuery);
130-
// query.
131-
var result = await _algoliaIndex.SearchAsync<Hit>(query);
130+
var query = new SearchQuery(
131+
new SearchForHits
132+
{
133+
IndexName = _indexName,
134+
Query = userQuery,
135+
HitsPerPage = 10,
136+
});
137+
var result = await _algoliaSearchClient.SearchAsync<Hit>(
138+
new SearchMethodParams
139+
{
140+
Requests = [query]
141+
});
132142

133-
foreach (var hit in result.Hits)
143+
foreach (var res in result.Results)
134144
{
135-
if (hit.hierarchy is object && hit.url is object)
145+
foreach (var hit in res.AsSearchResponse().Hits)
136146
{
137-
bool isIndexedWrong = false;
138-
139-
var caption = hit.anchor;
140-
for (int i = 5; i >= 0; i--)
147+
if (hit.hierarchy is not null && hit.url is not null)
141148
{
142-
var lvl = $"lvl{i}";
143-
if (hit.hierarchy.ContainsKey(lvl) && hit.hierarchy[lvl] is object)
149+
bool isIndexedWrong = false;
150+
151+
var caption = hit.anchor;
152+
for (int i = 5; i >= 0; i--)
144153
{
145-
caption = hit.hierarchy[lvl];
154+
var lvl = $"lvl{i}";
155+
if (hit.hierarchy.TryGetValue(lvl, out string? value) && value is not null)
156+
{
157+
caption = value;
146158

147-
if (caption.StartsWith("«") || caption.EndsWith("»"))
148-
isIndexedWrong = true; // It has indexed the arrow texts
159+
if (caption.StartsWith("«") || caption.EndsWith("»"))
160+
isIndexedWrong = true; // It has indexed the arrow texts
149161

150-
break;
162+
break;
163+
}
151164
}
152-
}
153165

154-
if (hit.url.EndsWith("#__docusaurus"))
155-
{
156-
hit.url = hit.url[..^14];
157-
}
166+
if (hit.url.EndsWith("#__docusaurus"))
167+
{
168+
hit.url = hit.url[..^14];
169+
}
158170

159-
// Compensate for fauly indexed pages
160-
if (!isIndexedWrong)
161-
returnList.Add((caption!, hit.url));
171+
// Compensate for fauly indexed pages
172+
if (!isIndexedWrong)
173+
returnList.Add((caption!, hit.url));
174+
}
162175
}
163176
}
164177
}
165-
catch (System.Exception e)
178+
catch (Exception e)
166179
{
167180
_logger.LogError(e, "Ops, something went wrong in Algolia search!");
168181
}
@@ -178,4 +191,4 @@ public class Hit
178191
public string? anchor { get; set; }
179192
public string? url { get; set; }
180193
public Dictionary<string, string>? hierarchy { get; set; }
181-
}
194+
}

src/Plugins/GitHub.cs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GithubPlugin : IBotPlugin
1616
private readonly GitHubClient _client;
1717
private readonly int _order;
1818

19-
static Regex _exIssueParsing = new Regex(@"\s*(?'type'\w+)\s*(?'topic'.*)");
19+
static readonly Regex _exIssueParsing = new(@"\s*(?'type'\w+)\s*(?'topic'.*)");
2020

2121
// private readonly bool _isAuthenticated;
2222
public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken = null)
@@ -32,20 +32,19 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
3232
if (string.IsNullOrEmpty(token) == false)
3333
{
3434
_client.Credentials = new Credentials(token);
35-
// _isAuthenticated = true;
3635
}
3736
}
3837

3938
public int Order => _order;
4039

4140
public IEnumerable<(string, string?)>? GetCommandsAndDecriptions()
4241
{
43-
return new List<(string, string?)>
44-
{
42+
return
43+
[
4544
("latest", "get latest release notes"),
4645
("issues", "get latest (max 10) reported issues"),
4746
("issue", "adds issues fast, enter command `issue` for additional options"),
48-
};
47+
];
4948
}
5049

5150
public async Task<BotResult?> HandleMessage(IMessage message)
@@ -67,7 +66,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
6766
return await AddIssueInRepo(message);
6867
}
6968
}
70-
catch (System.Exception e)
69+
catch (Exception e)
7170
{
7271
_logger.LogError(e, "Failed to handle message");
7372
}
@@ -92,11 +91,11 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
9291
}
9392

9493
Match? match = _exIssueParsing.Matches(message.CommandArgs).FirstOrDefault();
95-
if (match is object)
94+
if (match is not null)
9695
{
9796
string? command = null, title = null;
9897

99-
foreach (Group? group in match.Groups)
98+
foreach (Group? group in match.Groups.Cast<Group?>())
10099
{
101100
if (group?.Name == "type")
102101
command = string.IsNullOrEmpty(group.Value) ? null : group.Value.ToLowerInvariant();
@@ -109,18 +108,13 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
109108
return GetIssueMissingTitleHelpMessage();
110109
}
111110

112-
switch (command)
111+
return command switch
113112
{
114-
case "docs":
115-
return await AddDocsIssue(title, message.User);
116-
case "feature":
117-
return await AddDaemonIssue("feature", title, message.User);
118-
case "bug":
119-
return await AddDaemonIssue("bug", title, message.User);
120-
default:
121-
return UnKnownIssueCommand();
122-
}
123-
113+
"docs" => await AddDocsIssue(title, message.User),
114+
"feature" => await AddDaemonIssue("feature", title, message.User),
115+
"bug" => await AddDaemonIssue("bug", title, message.User),
116+
_ => UnKnownIssueCommand(),
117+
};
124118
;
125119

126120
}
@@ -145,7 +139,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
145139
_ => null
146140
};
147141

148-
if (label is object)
142+
if (label is not null)
149143
createIssue.Labels.Add(label);
150144

151145
var body = type switch
@@ -155,7 +149,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
155149
_ => null
156150
};
157151

158-
if (body is object)
152+
if (body is not null)
159153
createIssue.Body = body;
160154

161155
var issue = await _client.Issue.Create("net-daemon", "netdaemon", createIssue);
@@ -244,7 +238,7 @@ private BotResult GetIssueHelpMessage()
244238
if (releases.Count == 0)
245239
return null;
246240

247-
var release = releases.First();
241+
var release = releases[0];
248242

249243
var result = new BotResult() { Title = $"Latest release version {release.TagName}", Text = release.Body };
250244

@@ -292,7 +286,7 @@ private BotResult GetIssueHelpMessage()
292286
return result;
293287
}
294288

295-
private string featureTemplate = @"
289+
private readonly string featureTemplate = @"
296290
<!--
297291
Please describe the feature you want from a usage perspective.
298292
-->
@@ -312,7 +306,7 @@ Please describe the feature you want from a usage perspective.
312306
313307
";
314308

315-
private string docsTemplate = @"
309+
private readonly string docsTemplate = @"
316310
<!--
317311
Please describe what suggestions or issues you have for the docs.
318312
-->
@@ -323,7 +317,7 @@ Please describe the feature you want from a usage perspective.
323317
324318
";
325319

326-
private string issueTemplate = @"
320+
private readonly string issueTemplate = @"
327321
<!-- READ THIS FIRST:
328322
- If you need additional help with this template, please refer to https://netdaemon.xtz/help/reporting_issues/
329323
- Make sure you are running the latest version of NetDaemon before reporting an issue: https://github.com/net-daemon/netdaemon/releases

0 commit comments

Comments
 (0)