Skip to content

Commit

Permalink
fixed that bot cannot register slash commands because it running in r…
Browse files Browse the repository at this point in the history
…unner

Signed-off-by: TheBottleCyber <[email protected]>
  • Loading branch information
TheBottleCyber committed Feb 16, 2022
1 parent 31da462 commit 7604e20
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
output/

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down
22 changes: 11 additions & 11 deletions Leviathan.Bot/Leviathan.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Interactions" Version="3.3.0"/>
<PackageReference Include="Discord.Net.WebSocket" Version="3.3.0"/>
<PackageReference Include="Humanizer" Version="2.14.1"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0"/>
<PackageReference Include="Quartz" Version="3.3.3"/>
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.3.3"/>
<PackageReference Include="Serilog" Version="2.10.0"/>
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1"/>
<PackageReference Include="Discord.Net.Interactions" Version="3.3.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="3.3.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Quartz" Version="3.3.3" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.3.3" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Leviathan.Core\Leviathan.Core.csproj"/>
<ProjectReference Include="..\Leviathan.Core\Leviathan.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Leviathan.Bot/Modules/SlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task About()
var devLocalizedString = LocalizationHelper.GetLocalizedString("Developer");
var inGameNickLocalizedString = LocalizationHelper.GetLocalizedString("DiscordAboutCommandInGameNick");

await RespondAsync("Leviathan v1.0.5 - EVE Online Discord Bot\n" +
await RespondAsync("Leviathan v1.0.8 - EVE Online Discord Bot\n" +
$"{devLocalizedString}: TheBottle ({inGameNickLocalizedString} The Bottle)\n\n" +
$"{runTimeLocalizedString} {stringDate}");
}
Expand Down
7 changes: 7 additions & 0 deletions Leviathan.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task Start(string[] args)
try
{
Log.Information("Starting bot host");

var builder = CreateHostBuilder(args).Build();
_discordSocketClient = builder.Services.GetRequiredService<DiscordSocketClient>();
await builder.Services.GetRequiredService<CommandHandler>().InitializeAsync();
Expand All @@ -65,6 +66,12 @@ await builder.Services.GetRequiredService<InteractionService>()
await _discordSocketClient.LoginAsync(TokenType.Bot, _settings.DiscordConfig.BotToken);
await _discordSocketClient.StartAsync();

while (true)
{
if (_discordSocketClient.ConnectionState == ConnectionState.Connected) break;
else Thread.Sleep(250);
}

await builder.RunAsync();
}
catch (Exception ex)
Expand Down
14 changes: 13 additions & 1 deletion Leviathan.Bot/Services/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ public CommandHandler(DiscordSocketClient client, InteractionService commands, I

public async Task InitializeAsync()
{
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
var currentAssembly = Assembly.GetEntryAssembly();

if (currentAssembly!.GetName().Name == "Leviathan.Bot")
{
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
else
{
var assembly = AppDomain.CurrentDomain.GetAssemblies()
.SingleOrDefault(assembly => assembly.GetName().Name == "Leviathan.Bot");

await _commands.AddModulesAsync(assembly, _services);
}

_client.InteractionCreated += HandleInteraction;
}
Expand Down

0 comments on commit 7604e20

Please sign in to comment.