Skip to content

Commit

Permalink
Update DSP and readd the GuildCreatedEvent (#209)
Browse files Browse the repository at this point in the history
* Update DSP

* Reuse the event since it's fixed
  • Loading branch information
Sella-GH authored Nov 1, 2024
1 parent 992aae0 commit 86a3dd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CsvHelper" Version="33.0.1" />
<PackageVersion Include="DSharpPlus" Version="5.0.0-nightly-02396" />
<PackageVersion Include="DSharpPlus.Commands" Version="5.0.0-nightly-02396" />
<PackageVersion Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02396" />
<PackageVersion Include="DSharpPlus" Version="5.0.0-nightly-02401" />
<PackageVersion Include="DSharpPlus.Commands" Version="5.0.0-nightly-02401" />
<PackageVersion Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02401" />
<PackageVersion Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="8.1.3" />
<PackageVersion Include="Lavalink4NET.DSharpPlus.Nightly" Version="4.0.26-preview.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
Expand Down
31 changes: 20 additions & 11 deletions src/AzzyBot.Bot/Services/DiscordEvents/DiscordGuildsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ public sealed class DiscordGuildsHandler(ILogger<DiscordGuildsHandler> logger, A

private const string NewGuildText = "Thank you for adding me to your server **%GUILD%**! Before you can make good use of me, you have to set my settings first.\n\nPlease use the command `config modify-core` for this.\nOnly administrators are able to execute this command right now.";

public Task HandleEventAsync(DiscordClient sender, GuildCreatedEventArgs eventArgs)
public async Task HandleEventAsync(DiscordClient sender, GuildCreatedEventArgs eventArgs)
{
ArgumentNullException.ThrowIfNull(sender);
ArgumentNullException.ThrowIfNull(eventArgs);

_logger.GuildCreated(eventArgs.Guild.Name);

return Task.CompletedTask;
await _dbActions.AddGuildAsync(eventArgs.Guild.Id);
await GuildCreatedHelperAsync([eventArgs.Guild]);
}

public async Task HandleEventAsync(DiscordClient sender, GuildDeletedEventArgs eventArgs)
Expand Down Expand Up @@ -72,20 +73,13 @@ public async Task HandleEventAsync(DiscordClient sender, GuildDownloadCompletedE
return;
}

DiscordEmbed embed;
DiscordMember owner;
IEnumerable<DiscordGuild> addedGuilds = await _dbActions.AddGuildsAsync(eventArgs.Guilds);
if (addedGuilds.Any())
{
foreach (DiscordGuild guild in addedGuilds)
{
owner = await guild.GetGuildOwnerAsync();
await owner.SendMessageAsync(NewGuildText.Replace("%GUILD%", guild.Name, StringComparison.OrdinalIgnoreCase));
embed = await EmbedBuilder.BuildGuildAddedEmbedAsync(guild);
await _botService.SendMessageAsync(_settings.NotificationChannelId, embeds: [embed]);
}
await GuildCreatedHelperAsync(addedGuilds);
}

DiscordEmbed embed;
IEnumerable<ulong> removedGuilds = await _dbActions.DeleteGuildsAsync(eventArgs.Guilds);
if (removedGuilds.Any())
{
Expand All @@ -99,4 +93,19 @@ public async Task HandleEventAsync(DiscordClient sender, GuildDownloadCompletedE
IAsyncEnumerable<GuildEntity> guilds = _dbActions.GetGuildsAsync(loadEverything: true);
await _botService.CheckPermissionsAsync(guilds);
}

private async Task GuildCreatedHelperAsync(IEnumerable<DiscordGuild> guilds)
{
ArgumentNullException.ThrowIfNull(guilds);

DiscordEmbed embed;
DiscordMember owner;
foreach (DiscordGuild guild in guilds)
{
owner = await guild.GetGuildOwnerAsync();
await owner.SendMessageAsync(NewGuildText.Replace("%GUILD%", guild.Name, StringComparison.OrdinalIgnoreCase));
embed = await EmbedBuilder.BuildGuildAddedEmbedAsync(guild);
await _botService.SendMessageAsync(_settings.NotificationChannelId, embeds: [embed]);
}
}
}

0 comments on commit 86a3dd2

Please sign in to comment.