Skip to content

Commit

Permalink
Fix shocker setup command
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Jan 10, 2025
1 parent 19d881d commit 72d4cbb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ obj/
riderModule.iml
/_ReSharper.Caches/
.idea
.vs
.vs
.user
17 changes: 9 additions & 8 deletions DiscordBot/Commands/Setup/_SetupCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ public async Task ShockerConfig()
return;
}

var ownShockers = shockersAction.AsT0.Value;
var ownDevices = shockersAction.AsT0.Value;
var activeShockers = await _db.UsersShockers.Where(x => x.User == Context.User.Id).ToListAsync();

var ownShockers = ownDevices.SelectMany(x => x.Shockers);

// cleanup old shockers
var oldShockers = activeShockers.Where(x => ownShockers.All(y => y.Id != x.ShockerId)).Select(x => x.ShockerId);
await _db.UsersShockers.Where(x => oldShockers.Contains(x.ShockerId)).ExecuteDeleteAsync();
var oldShockers = activeShockers.Where(x => ownShockers.All(y => y.Id != x.ShockerId)).Select(x => x.ShockerId).ToArray();
if(oldShockers.Length > 0) await _db.UsersShockers.Where(x => oldShockers.Contains(x.ShockerId)).ExecuteDeleteAsync();

var message = await Page(0, ownShockers, activeShockers);
var message = Page(0, ownDevices, activeShockers);
await FollowupAsync(message.Item1, components: message.Item2);
}

private async Task<(string, MessageComponent)> Page(int page, IReadOnlyCollection<ResponseDeviceWithShockers> ownShockers,
private (string, MessageComponent) Page(int page, IReadOnlyCollection<ResponseDeviceWithShockers> ownShockers,
List<UsersShocker> activeShockers)
{
var device = ownShockers.ElementAtOrDefault(page);
Expand Down Expand Up @@ -147,7 +148,7 @@ public async Task ShockerButtonInteraction()

var ownShockers = shockersAction.AsT0.Value;

var message = await Page(page, ownShockers, activeShockers);
var message = Page(page, ownShockers, activeShockers);

await ModifyOriginalResponseAsync(properties =>
{
Expand Down Expand Up @@ -184,7 +185,7 @@ public async Task ShockerPageButtonInteraction()

var page = int.Parse(((string)CustomId.GetValue(Context.Interaction.Data)!).Split('@')[1]);

var message = await Page(page, ownShockers, activeShockers);
var message = Page(page, ownShockers, activeShockers);

await ModifyOriginalResponseAsync(properties =>
{
Expand Down
5 changes: 3 additions & 2 deletions DiscordBot/DiscordBot.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -14,7 +14,8 @@
<RootNamespace>OpenShock.DiscordBot</RootNamespace>
<InvariantGlobalization>false</InvariantGlobalization>
<LangVersion>13</LangVersion>
</PropertyGroup>
<UserSecretsId>05918ba7-2547-4661-9c92-1172d1441505</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
Expand Down
14 changes: 9 additions & 5 deletions DiscordBot/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Discord;
using System.Diagnostics;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
Expand All @@ -13,6 +14,8 @@
using Serilog;
using System.Reflection;

try
{
HostBuilder builder = new();

builder.UseContentRoot(Directory.GetCurrentDirectory())
Expand All @@ -28,6 +31,7 @@
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true,
reloadOnChange: false);

config.AddUserSecrets(Assembly.GetExecutingAssembly(), optional: true);
config.AddEnvironmentVariables();
if (args is { Length: > 0 }) config.AddCommandLine(args);
})
Expand All @@ -43,7 +47,8 @@

builder.ConfigureServices((context, services) =>
{
var discordBotConfig = context.Configuration.GetSection("bot").Get<DiscordBotConfig>() ??
var botConfig = context.Configuration.GetSection("bot");
var discordBotConfig = botConfig.Get<DiscordBotConfig>() ??
throw new Exception("Could not load bot config");

services.AddSingleton(discordBotConfig);
Expand Down Expand Up @@ -76,8 +81,7 @@
services.AddHostedService<StatusTask>();
});

try
{

var host = builder.Build();

Log.Information("Starting OpenShock Discord Bot version {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString());
Expand Down Expand Up @@ -135,7 +139,7 @@
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
Console.WriteLine(ex);
}
finally
{
Expand Down
11 changes: 11 additions & 0 deletions DiscordBot/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"OpenShockDiscordBot": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

0 comments on commit 72d4cbb

Please sign in to comment.