Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions general-helpers/feedback/leftPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,56 @@ namespace Bob.Feedback
{
public class Prompt : InteractionModuleBase<ShardedInteractionContext>
{
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<BobEntities>();
// 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<BobEntities>();

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:*")]
Expand Down
198 changes: 109 additions & 89 deletions main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BobEntities>();
server = await context.GetOrCreateServerAsync(user.Guild.Id);
}
Server server;
using (var scope = Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<BobEntities>();
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<BobEntities>();
welcomeImage = await context.GetWelcomeImage(user.Guild.Id);
WelcomeImage welcomeImage = null;
using (var scope = Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<BobEntities>();
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<BobEntities>();
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<BobEntities>();
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<BobEntities>();
await context.GetOrCreateServerAsync(guild.Id);
_ = Task.Run(async () =>
{
using var scope = Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<BobEntities>();
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<BobEntities>();
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<BobEntities>();
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<SocketEntitlement, ulong> before, SocketEntitlement after)
private static Task EntitlementUpdated(Cacheable<SocketEntitlement, ulong> before, SocketEntitlement after)
{
using var scope = Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<BobEntities>();
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<BobEntities>();
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<SocketEntitlement, ulong> ent)
Expand All @@ -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)
Expand Down
Loading