Skip to content

Commit 803563e

Browse files
committed
Add a method to directly get the locale without creating the full object and little cleanup
1 parent 7e1d933 commit 803563e

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

Cyberia.Cytrusaurus/CytrusWatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ await _onlineMonitoredFileRepository.UpsertAsync(new OnlineMonitoredFile
161161
{
162162
await OnCytrusErroredAsync(new CytrusErroredEventArgs(e, "An error occurred while sending a GET request to the Cytrus URL, see the logs for more details."));
163163
Log.Error(e, "An error occurred while sending a GET request to {CytrusUrl}", $"{BaseUrl}/{CytrusFileName}");
164+
164165
return;
165166
}
166167

@@ -176,7 +177,6 @@ await _onlineMonitoredFileRepository.UpsertAsync(new OnlineMonitoredFile
176177
if (cytrus is null)
177178
{
178179
await OnCytrusErroredAsync(new CytrusErroredEventArgs("Failed to deserialize the Cytrus data from the response content, see the logs for more details."));
179-
Log.Error("Failed to deserialize the Cytrus data from the response content:\n{CytrusJson}", json);
180180

181181
return;
182182
}
@@ -247,6 +247,7 @@ await _onlineMonitoredFileRepository.UpsertAsync(new OnlineMonitoredFile
247247

248248
return null;
249249
}
250+
250251
#region Events
251252

252253
public event ICytrusWatcher.NewCytrusFileDetectedEventHandler? NewCytrusFileDetected;

Cyberia.Database/Repositories/DiscordCachedUserRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@ public async Task<bool> DeleteAsync(ulong id)
5858
using var connection = await _connectionFactory.CreateConnectionAsync();
5959
return await connection.ExecuteAsync(query, new { Id = id }) > 0;
6060
}
61+
62+
/// <summary>
63+
/// Gets the locale of a user by their ID.
64+
/// </summary>
65+
/// <param name="id">The ID of the user.</param>
66+
/// <returns>The locale of the user if found; otherwise, <see langword="null"/>.</returns>
67+
public async Task<string?> GetLocaleById(ulong id)
68+
{
69+
const string query =
70+
$"""
71+
SELECT {nameof(DiscordCachedUser.Locale)}
72+
FROM {nameof(DiscordCachedUser)}
73+
WHERE {nameof(DiscordCachedUser.Id)} = @Id
74+
""";
75+
76+
using var connection = await _connectionFactory.CreateConnectionAsync();
77+
return await connection.QueryFirstOrDefaultAsync<string>(query, new { Id = id });
78+
}
6179
}

Cyberia.Salamandra/Extensions/ServiceProviderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public static class ServiceProviderExtensions
2121
/// <returns>The service provider.</returns>
2222
public static IServiceProvider RegisterSalamandraEvents(this IServiceProvider provider)
2323
{
24-
var cytrusWatcher = provider.GetRequiredService<ICytrusWatcher>();
2524
var cytrusService = provider.GetRequiredService<ICytrusService>();
25+
var cytrusWatcher = provider.GetRequiredService<ICytrusWatcher>();
2626

2727
cytrusWatcher.NewCytrusFileDetected += cytrusService.OnNewCytrusFileDetected;
2828
cytrusWatcher.CytrusErrored += cytrusService.OnCytrusErrored;
2929

30-
var langsWatcher = provider.GetRequiredService<ILangsWatcher>();
3130
var langsService = provider.GetRequiredService<ILangsService>();
31+
var langsWatcher = provider.GetRequiredService<ILangsWatcher>();
3232

3333
langsWatcher.CheckLangsFinished += langsService.OnCheckLangsFinished;
3434

Cyberia.Salamandra/Services/CultureService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ public CultureService(DiscordCachedUserRepository discordCachedUserRepository, D
4545

4646
public async Task<string?> GetLocaleAsync(DiscordInteraction interaction)
4747
{
48-
var user = await _discordCachedUserRepository.GetAsync(interaction.User.Id);
48+
var locale = await _discordCachedUserRepository.GetLocaleById(interaction.User.Id);
4949

50-
return user?.Locale ?? interaction.Locale ?? interaction.GuildLocale;
50+
return locale ?? interaction.Locale ?? interaction.GuildLocale;
5151
}
5252

5353
public async Task<CultureInfo> GetCultureAsync(DiscordInteraction interaction)
5454
{
55-
var user = await _discordCachedUserRepository.GetAsync(interaction.User.Id);
55+
var locale = await _discordCachedUserRepository.GetLocaleById(interaction.User.Id);
5656

57-
return string.IsNullOrEmpty(user?.Locale)
57+
return locale is null
5858
? GetInteractionCulture(interaction)
59-
: CultureInfo.GetCultureInfo(user.Locale);
59+
: CultureInfo.GetCultureInfo(locale);
6060
}
6161

6262
/// <summary>

0 commit comments

Comments
 (0)