Skip to content

Commit

Permalink
fix: connection must be made after register
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Feb 7, 2024
1 parent 33dbf78 commit 3c40633
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
15 changes: 9 additions & 6 deletions ApplicationCommands/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace DisCatSharp.Examples.ApplicationCommands;

// TODO: rewrite, this is kinda weird
/// <summary>
/// The program.
/// </summary>
Expand Down Expand Up @@ -51,12 +52,6 @@ public static async Task MainAsync(string[] args)

DiscordShardedClient discordShardedClient = new(discordConfiguration);

Log.Logger.Information("Connecting to Discord...");
await discordShardedClient.StartAsync();

// Use the default logger provided for easy reading
discordShardedClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordShardedClient.CurrentUser.Username, discordShardedClient.CurrentUser.Discriminator, discordShardedClient.CurrentUser.Id);

// Register a Random class instance now for use later over in RollRandom.cs
ApplicationCommandsConfiguration appCommandsConfiguration = new(new ServiceCollection().AddSingleton<Random>().BuildServiceProvider())
{
Expand Down Expand Up @@ -99,6 +94,14 @@ public static async Task MainAsync(string[] args)
}

discordShardedClient.Logger.LogInformation("Application commands registered successfully");


Log.Logger.Information("Connecting to Discord...");
await discordShardedClient.StartAsync();

// Use the default logger provided for easy reading
discordShardedClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordShardedClient.CurrentUser.Username, discordShardedClient.CurrentUser.Discriminator, discordShardedClient.CurrentUser.Id);

// Listen for commands by putting this method to sleep and relying off of DiscordClient's event listeners
await Task.Delay(-1);
}
Expand Down
6 changes: 3 additions & 3 deletions Interactivity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ private static async Task Main(string[] args)
// Enabling VoiceNext for . You can also use lavalink.
discordClient.UseVoiceNext();

Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Enabling interactivity support
discordClient.UseInteractivity();

Expand Down Expand Up @@ -81,6 +78,9 @@ private static async Task Main(string[] args)

discordClient.Logger.LogInformation("Application commands registered successfully");

Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Listen for commands by putting this method to sleep and relying off of DiscordClient's event listeners
await Task.Delay(-1);
}
Expand Down
23 changes: 11 additions & 12 deletions Lavalink/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ private static async Task Main(string[] args)

var lavalink = discordClient.UseLavalink();

Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Use the default logger provided for easy reading
discordClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordClient.CurrentUser.Username, discordClient.CurrentUser.Discriminator, discordClient.CurrentUser.Id);

// Lavalink
discordClient.Logger.LogInformation($"Connecting to lavalink...");
await lavalink.ConnectAsync(lavalinkConfig); // Make sure this is after discordClient.ConnectAsync()
discordClient.Logger.LogInformation($"Successful connection with lavalink!");

// In order not to list all the commands when adding, you can create a list of all commands with this.
var appCommandModule = typeof(ApplicationCommandsModule);
var commands = Assembly.GetExecutingAssembly().GetTypes().Where(t => appCommandModule.IsAssignableFrom(t) && !t.IsNested).ToList();
Expand All @@ -88,7 +77,17 @@ private static async Task Main(string[] args)
foreach (var command in commands)
appCommandExt.RegisterGlobalCommands(command);

discordClient.Logger.LogInformation("Application commands registered successfully");
// Connect AFTER registering application commands
Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Use the default logger provided for easy reading
discordClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordClient.CurrentUser.Username, discordClient.CurrentUser.Discriminator, discordClient.CurrentUser.Id);

// Lavalink
discordClient.Logger.LogInformation($"Connecting to lavalink...");
await lavalink.ConnectAsync(lavalinkConfig); // Make sure this is after discordClient.ConnectAsync()
discordClient.Logger.LogInformation($"Successful connection with lavalink!");

// Listen for commands by putting this method to sleep and relying off of DiscordClient's event listeners
await Task.Delay(-1);
Expand Down
12 changes: 6 additions & 6 deletions VoiceNext/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ private static async Task Main(string[] args)
// This line is needed to enable support for voice channels in the bot.
discordClient.UseVoiceNext();

Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Use the default logger provided for easy reading
discordClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordClient.CurrentUser.Username, discordClient.CurrentUser.Discriminator, discordClient.CurrentUser.Id);

// Let the user know that we're registering the commands.
discordClient.Logger.LogInformation("Registering application commands...");

Expand All @@ -72,6 +66,12 @@ private static async Task Main(string[] args)
appCommandExt.RegisterGuildCommands(command, 885510395295584289);

discordClient.Logger.LogInformation("Application commands registered successfully");

Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync();

// Use the default logger provided for easy reading
discordClient.Logger.LogInformation("Connection success! Logged in as {CurrentUserUsername}#{CurrentUserDiscriminator} ({CurrentUserId})", discordClient.CurrentUser.Username, discordClient.CurrentUser.Discriminator, discordClient.CurrentUser.Id);

// Listen for commands by putting this method to sleep and relying off of DiscordClient's event listeners
await Task.Delay(-1);
Expand Down

0 comments on commit 3c40633

Please sign in to comment.