diff --git a/general-helpers/feedback/leftPrompt.cs b/general-helpers/feedback/leftPrompt.cs index 670b7d9e..86a53646 100644 --- a/general-helpers/feedback/leftPrompt.cs +++ b/general-helpers/feedback/leftPrompt.cs @@ -10,51 +10,56 @@ namespace Bob.Feedback { public class Prompt : InteractionModuleBase { - public static async Task LeftGuild(SocketGuild guild) + public static Task LeftGuild(SocketGuild guild) { - try + _ = Task.Run(async () => { - // Check if the guild still exists - IUser owner = guild.Owner ?? await Bot.Client.GetShardFor(guild.Id).GetUserAsync(guild.OwnerId); - var components = new ComponentBuilder(); - - var selectMenu = new SelectMenuBuilder + try { - MinValues = 1, - CustomId = $"leftGuild:{guild.Name}", - Placeholder = "Select why...", - }; + // Check if the guild still exists + IUser owner = guild.Owner ?? await Bot.Client.GetShardFor(guild.Id).GetUserAsync(guild.OwnerId); + var components = new ComponentBuilder(); - selectMenu.AddOption("Too complicated", "1 Too complicated"); - selectMenu.AddOption("Found a better bot", "2 Found a better bot"); - selectMenu.AddOption("Missing feature(s)", "3 Missing features"); - selectMenu.AddOption("Invited on accident / wrong server", "4 Invited on accident / wrong server"); - selectMenu.AddOption("Bot was not responding", "5 Bot was not responding"); - selectMenu.AddOption("Other", "6 Other"); + var selectMenu = new SelectMenuBuilder + { + MinValues = 1, + CustomId = $"leftGuild:{guild.Name}", + Placeholder = "Select why...", + }; - selectMenu.MaxValues = selectMenu.Options.Count; + selectMenu.AddOption("Too complicated", "1 Too complicated"); + selectMenu.AddOption("Found a better bot", "2 Found a better bot"); + selectMenu.AddOption("Missing feature(s)", "3 Missing features"); + selectMenu.AddOption("Invited on accident / wrong server", "4 Invited on accident / wrong server"); + selectMenu.AddOption("Bot was not responding", "5 Bot was not responding"); + selectMenu.AddOption("Other", "6 Other"); - components.WithSelectMenu(selectMenu); + selectMenu.MaxValues = selectMenu.Options.Count; - // Send the message to the guild owner - await owner.SendMessageAsync(text: "### 😢 Sorry to see you go...\nMind telling us why?", components: components.Build()); - } - catch - { - // User's direct messages are closed, no action needed - } + components.WithSelectMenu(selectMenu); - using var scope = Bot.Services.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); + // Send the message to the guild owner + await owner.SendMessageAsync(text: "### 😢 Sorry to see you go...\nMind telling us why?", components: components.Build()); + } + catch + { + // User's direct messages are closed, no action needed + } - var server = await context.GetServer(guild.Id); + using var scope = Bot.Services.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); - if (server == null) - { - return; - } + var server = await context.GetServer(guild.Id); + + if (server == null) + { + return; + } + + await context.RemoveServer(server); + }); - await context.RemoveServer(server); + return Task.CompletedTask; } [ComponentInteraction("leftGuild:*")] diff --git a/main.cs b/main.cs index e5d35a0b..e196721c 100644 --- a/main.cs +++ b/main.cs @@ -104,7 +104,7 @@ public static async Task Main() await Client.LoginAsync(TokenType.Bot, Token); await Client.StartAsync(); - Uptime.StartHttpListener(); + // Uptime.StartHttpListener(); // Wait for all shards to be ready before proceeding await _allShardsReady.Task; @@ -224,123 +224,143 @@ private static Task ShardReady(DiscordSocketClient shard) return Task.CompletedTask; } - private static async Task UserJoined(SocketGuildUser user) + private static Task UserJoined(SocketGuildUser user) { - try + _ = Task.Run(async () => { - if (user.IsBot) + try { - return; - } + if (user.IsBot) + { + return; + } - Server server; - using (var scope = Services.CreateScope()) - { - var context = scope.ServiceProvider.GetRequiredService(); - server = await context.GetOrCreateServerAsync(user.Guild.Id); - } + Server server; + using (var scope = Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + server = await context.GetOrCreateServerAsync(user.Guild.Id); + } - if (server.Welcome == false) - { - return; - } + if (server.Welcome == false) + { + return; + } - var systemChannel = user.Guild.SystemChannel; + var systemChannel = user.Guild.SystemChannel; - if (systemChannel is not SocketTextChannel textChannel) - { - return; - } + if (systemChannel is not SocketTextChannel textChannel) + { + return; + } - var botUser = user.Guild.GetUser(Client.CurrentUser.Id); - if (botUser == null) - { - return; - } + var botUser = user.Guild.GetUser(Client.CurrentUser.Id); + if (botUser == null) + { + return; + } - var permissions = botUser.GetPermissions(textChannel); - if (!permissions.SendMessages || !permissions.ViewChannel) - { - return; - } + var permissions = botUser.GetPermissions(textChannel); + if (!permissions.SendMessages || !permissions.ViewChannel) + { + return; + } - if (server.HasWelcomeImage && permissions.AttachFiles) - { - WelcomeImage welcomeImage = null; - using (var scope = Services.CreateScope()) + if (server.HasWelcomeImage && permissions.AttachFiles) { - var context = scope.ServiceProvider.GetRequiredService(); - welcomeImage = await context.GetWelcomeImage(user.Guild.Id); + WelcomeImage welcomeImage = null; + using (var scope = Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + welcomeImage = await context.GetWelcomeImage(user.Guild.Id); + } + + if (welcomeImage != null) + { + await textChannel.SendFileAsync( + new MemoryStream(welcomeImage.Image), + "welcome.webp", + text: Welcome.PrepareWelcomeMessage(server.CustomWelcomeMessage, user.Mention) + ); + } + } + else + { + await textChannel.SendMessageAsync(text: Welcome.PrepareWelcomeMessage(server.CustomWelcomeMessage, user.Mention)); } - if (welcomeImage != null) + // If support server, then give the user the Friend badge + if (user.Guild.Id == supportServerId) { - await textChannel.SendFileAsync( - new MemoryStream(welcomeImage.Image), - "welcome.webp", - text: Welcome.PrepareWelcomeMessage(server.CustomWelcomeMessage, user.Mention) - ); + User dbUser; + using (var scope = Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + dbUser = await context.GetOrCreateUserAsync(user.Id); + } + + await Badge.GiveUserBadge(dbUser, Badges.Badges.Friend); } } - else + catch (Exception e) { - await textChannel.SendMessageAsync(text: Welcome.PrepareWelcomeMessage(server.CustomWelcomeMessage, user.Mention)); + Console.WriteLine(e); } + }); - // If support server, then give the user the Friend badge - if (user.Guild.Id == supportServerId) - { - User dbUser; - using (var scope = Services.CreateScope()) - { - var context = scope.ServiceProvider.GetRequiredService(); - dbUser = await context.GetOrCreateUserAsync(user.Id); - } - - await Badge.GiveUserBadge(dbUser, Badges.Badges.Friend); - } - } - catch (Exception e) - { - Console.WriteLine(e); - } + return Task.CompletedTask; } - private static async Task JoinedGuild(SocketGuild guild) + private static Task JoinedGuild(SocketGuild guild) { - using var scope = Services.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - await context.GetOrCreateServerAsync(guild.Id); + _ = Task.Run(async () => + { + using var scope = Services.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + await context.GetOrCreateServerAsync(guild.Id); + }); + + return Task.CompletedTask; } - private static async Task EntitlementCreated(SocketEntitlement ent) + private static Task EntitlementCreated(SocketEntitlement ent) { - using var scope = Services.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - IUser entUser = await ent.User.Value.GetOrDownloadAsync(); - User user = await context.GetOrCreateUserAsync(entUser.Id); - - if (ent.EndsAt == null) - { - user.PremiumExpiration = DateTimeOffset.MaxValue; - } - else + _ = Task.Run(async () => { - user.PremiumExpiration = (DateTimeOffset)ent.EndsAt; - } + using var scope = Services.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + IUser entUser = await ent.User.Value.GetOrDownloadAsync(); + User user = await context.GetOrCreateUserAsync(entUser.Id); - await context.SaveChangesAsync(); + if (ent.EndsAt == null) + { + user.PremiumExpiration = DateTimeOffset.MaxValue; + } + else + { + user.PremiumExpiration = (DateTimeOffset)ent.EndsAt; + } + + await context.SaveChangesAsync(); + }); + + return Task.CompletedTask; } - private static async Task EntitlementUpdated(Cacheable before, SocketEntitlement after) + private static Task EntitlementUpdated(Cacheable before, SocketEntitlement after) { - using var scope = Services.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - IUser entUser = await before.Value.User.Value.GetOrDownloadAsync(); - User user = await context.GetOrCreateUserAsync(entUser.Id); + _ = Task.Run(async () => + { + using var scope = Services.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + IUser entUser = await before.Value.User.Value.GetOrDownloadAsync(); + User user = await context.GetOrCreateUserAsync(entUser.Id); + + user.PremiumExpiration = (DateTimeOffset)after.EndsAt; + await context.SaveChangesAsync(); + }); - user.PremiumExpiration = (DateTimeOffset)after.EndsAt; - await context.SaveChangesAsync(); + return Task.CompletedTask; } private static Task EntitlementDeleted(Cacheable ent) @@ -357,8 +377,8 @@ private static Task MessageReceived(SocketMessage message) // Ensure channel is not null (guild messages only) if (message.Channel is not SocketGuildChannel channel) return; - - if (channel.Guild == null) + + if (channel.Guild == null) return; SocketGuildUser fetchedBot = Client.GetGuild(channel.Guild.Id)