Skip to content

Commit 37599d5

Browse files
buildbuild
authored andcommitted
Async db
1 parent a40f3d0 commit 37599d5

24 files changed

Lines changed: 122 additions & 106 deletions

BaroBot/Commands/CategoryCommands/ArchiveCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ public partial class CategoryCommand
2222
[SlashCommand("archive", "Archives the specified category")]
2323
public async Task Archive(SocketCategoryChannel socketCategory)
2424
{
25-
Category? category = GetCategory(socketCategory, Context.Guild);
25+
Category? category = await GetCategory(socketCategory, Context.Guild);
2626
if (category == null)
2727
{
2828
await RespondAsync("That category is not registered!");
2929
return;
3030
}
3131

32-
GuildConfig? guildConfig = _database.FetchOne<GuildConfig>(config => config.GuildId == Context.Guild.Id);
32+
GuildConfig? guildConfig = await _database.FetchOneAsync<GuildConfig>(config => config.GuildId == Context.Guild.Id);
3333
if (guildConfig is null)
3434
{
3535
guildConfig = new GuildConfig(Context.Guild);
36-
_database.Save(guildConfig);
36+
await _database.SaveAsync(guildConfig);
3737
}
3838

3939
if (guildConfig.ArchiveCategoryId == 0 ||
40-
Context.Guild.GetCategoryChannel(guildConfig.ArchiveCategoryId) is not SocketCategoryChannel archiveCategory)
40+
Context.Guild.GetCategoryChannel(guildConfig.ArchiveCategoryId) is not { } archiveCategory)
4141
{
4242
await RespondAsync("The archive category is not set.");
4343
return;
@@ -54,7 +54,7 @@ public async Task Archive(SocketCategoryChannel socketCategory)
5454
if (role is not null)
5555
await role.DeleteAsync();
5656

57-
_database.Delete(category);
57+
await _database.DeleteAsync(category);
5858
await RespondAsync($"Category {category.Name} has been archived.");
5959
}
6060

BaroBot/Commands/CategoryCommands/CategoryCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ public CategoryCommand(DatabaseService database, DiscordSocketClient client, Env
4141
_eventDispatcher = eventEventDispatcher;
4242
}
4343

44-
private Category? GetCategory(SocketCategoryChannel categoryChannel, SocketGuild guild)
44+
private async Task<Category?> GetCategory(SocketCategoryChannel categoryChannel, SocketGuild guild)
4545
{
46-
return _database.FetchOne<Category>(category =>
46+
return await _database.FetchOneAsync<Category>(category =>
4747
category.CategoryId == categoryChannel.Id && category.GuildId == guild.Id);
4848
}
4949

50-
private Category? GetCategory(string categoryName, SocketGuild guild)
50+
private async Task<Category?> GetCategory(string categoryName, SocketGuild guild)
5151
{
52-
return _database.FetchOne<Category>(category =>
52+
return await _database.FetchOneAsync<Category>(category =>
5353
string.Equals(category.Name, categoryName, StringComparison.OrdinalIgnoreCase) && category.GuildId == guild.Id);
5454
}
5555

56-
private Category? GetCategory(IRole role, SocketGuild guild)
56+
private async Task<Category?> GetCategory(IRole role, SocketGuild guild)
5757
{
58-
return _database.FetchOne<Category>(category =>
58+
return await _database.FetchOneAsync<Category>(category =>
5959
category.RoleId == role.Id && category.GuildId == guild.Id);
6060
}
6161
}

BaroBot/Commands/CategoryCommands/CreateCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public async Task Create(
3434
[Summary("game-id", "The ID of the steam game this category is associated with")]
3535
int gameId = 0)
3636
{
37-
if (GetCategory(name, Context.Guild) != null)
37+
Category? existingCategory = await GetCategory(name, Context.Guild);
38+
if (existingCategory != null)
3839
{
3940
await RespondAsync("A category with that name already exists!");
4041
return;
@@ -63,7 +64,7 @@ await Context.Guild.CreateVoiceChannelAsync(voiceChannel, properties =>
6364
});
6465

6566
Category category = new Category(restCategory, restRole, gameId);
66-
_database.Save(category);
67+
await _database.SaveAsync(category);
6768
await _eventDispatcher.RaiseCategoryCreatedAsync(new CategoryCreatedEvent(category));
6869

6970
await RespondAsync($"Category '{name}' created!");

BaroBot/Commands/CategoryCommands/ImportCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public async Task Import(
3030
[Summary("game-id", "The ID of the steam game this category is associated with")]
3131
int gameId = 0)
3232
{
33-
Category? category = GetCategory(socketCategory, Context.Guild);
33+
Category? category = await GetCategory(socketCategory, Context.Guild);
3434
if (category != null)
3535
{
3636
await RespondAsync("That category is already registered!");
3737
return;
3838
}
3939

40-
category = GetCategory(role, Context.Guild);
40+
category = await GetCategory(role, Context.Guild);
4141
if (category != null)
4242
{
4343
await RespondAsync("That role is already registered!");
4444
return;
4545
}
4646

47-
_database.Save(new Category(socketCategory, role, gameId));
47+
await _database.SaveAsync(new Category(socketCategory, role, gameId));
4848
await RespondAsync($"Category '{socketCategory.Name}' imported!");
4949
}
5050
}

BaroBot/Commands/CategoryCommands/IpCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task Ip(
3333
[Summary("resolve-on-query", "Whether it should be resolved when it is queried.")]
3434
bool resolveOnQuery = false)
3535
{
36-
Category? category = GetCategory(socketCategory, Context.Guild);
36+
Category? category = await GetCategory(socketCategory, Context.Guild);
3737
if (category == null)
3838
{
3939
await RespondAsync("That category is not registered!");
@@ -54,7 +54,7 @@ public async Task Ip(
5454

5555
category.Ip = ip;
5656
category.ResolveIpOnQuery = resolveOnQuery;
57-
_database.Upsert(category);
57+
await _database.UpsertAsync(category);
5858
await RespondAsync($"Set {connectionString} as the connection to this categories server.", ephemeral: true);
5959
}
6060

BaroBot/Commands/CategoryCommands/RemoveCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public async Task Remove(
2323
[Summary("category", "The category to remove")]
2424
SocketCategoryChannel socketCategory)
2525
{
26-
Category? category = GetCategory(socketCategory, Context.Guild);
26+
Category? category = await GetCategory(socketCategory, Context.Guild);
2727
if (category == null)
2828
{
2929
await RespondAsync("That category is not registered!");
3030
return;
3131
}
3232

33-
if (!await category.Delete(_client))
33+
if (!await category.DeleteAsync(_client))
3434
{
3535
await RespondAsync("An error occurred while deleting the category.");
3636
return;
3737
}
3838

39-
_database.Delete(category);
39+
await _database.DeleteAsync(category);
4040
await RespondAsync($"Category '{category.Name}' removed!");
4141
}
4242

@@ -50,20 +50,20 @@ public async Task Remove(
5050
[Summary("category", "The category to remove")]
5151
string socketCategory)
5252
{
53-
Category? category = GetCategory(socketCategory, Context.Guild);
53+
Category? category = await GetCategory(socketCategory, Context.Guild);
5454
if (category == null)
5555
{
5656
await RespondAsync("That category is not registered!");
5757
return;
5858
}
5959

60-
if (!await category.Delete(_client))
60+
if (!await category.DeleteAsync(_client))
6161
{
6262
await RespondAsync("An error occurred while deleting the category.");
6363
return;
6464
}
6565

66-
_database.Delete(category);
66+
await _database.DeleteAsync(category);
6767
await RespondAsync($"Category '{category.Name}' removed!");
6868
}
6969
}

BaroBot/Commands/CategoryCommands/SteamIdCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ public async Task GameId(
2828
[Summary("game-id", "The new id")]
2929
int gameId)
3030
{
31-
Category? category = GetCategory(socketCategory, Context.Guild);
31+
Category? category = await GetCategory(socketCategory, Context.Guild);
3232
if (category is null)
3333
{
3434
await RespondAsync("That category is not registered!");
3535
return;
3636
}
3737

3838
category.GameId = gameId;
39-
_database.Update(category);
39+
await _database.UpdateAsync(category);
4040

4141
foreach (var user in Context.Guild.Users)
4242
{
4343
try
4444
{
45-
SteamLink? steamLink = _database.FetchOne<SteamLink>(x => x.UserId == user.Id);
45+
SteamLink? steamLink = await _database.FetchOneAsync<SteamLink>(x => x.UserId == user.Id);
4646
if (steamLink is null || !steamLink.UpdateAutomatically)
4747
continue;
4848

BaroBot/Commands/IpCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task Execute()
4949
return;
5050
}
5151

52-
Category? category = _database.FetchOne<Category>(category => category.CategoryId == categoryChannel.Id && category.GuildId == Context.Guild.Id);
52+
Category? category = await _database.FetchOneAsync<Category>(category => category.CategoryId == categoryChannel.Id && category.GuildId == Context.Guild.Id);
5353
if (category is null)
5454
{
5555
await RespondAsync("Unable to locate the target category.");

BaroBot/Commands/RentGeneratorCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public RentGeneratorCommand(DatabaseService database)
3838
public async Task Execute([Summary("name", "The name of the rent generator")] string name)
3939
{
4040
RestVoiceChannel restVoiceChannel = await Context.Guild.CreateVoiceChannelAsync(name);
41-
_database.Save(new RentGenerator(restVoiceChannel));
41+
await _database.SaveAsync(new RentGenerator(restVoiceChannel));
4242
await RespondAsync($"Created a rent generator with the name '{name}'");
4343
}
4444
}

BaroBot/Commands/RoleCommands/ListCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task List()
2525
return;
2626
}
2727

28-
Embed? availableRoles = AvailableRoles(Context.User);
28+
Embed? availableRoles = await AvailableRoles(Context.User);
2929
if (availableRoles == null)
3030
{
3131
await RespondAsync("There are no toggleable roles available in this server.");

0 commit comments

Comments
 (0)