From 00022c1cb9b76315d4b8e53c0dacdbf9e4b0b01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique=20Ivanchechen?= Date: Wed, 15 Feb 2023 21:47:26 -0300 Subject: [PATCH] using shards to see if it fixes connection issues --- Kasbot.csproj | 2 +- Modules/PublicModule.cs | 2 +- Program.cs | 21 ++++++++------------- Services/CommandHandlingService.cs | 6 +++--- Services/PlayerService.cs | 3 ++- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/Kasbot.csproj b/Kasbot.csproj index f793392..4bfe39f 100644 --- a/Kasbot.csproj +++ b/Kasbot.csproj @@ -10,7 +10,7 @@ - + diff --git a/Modules/PublicModule.cs b/Modules/PublicModule.cs index 14e62b0..c6f148f 100644 --- a/Modules/PublicModule.cs +++ b/Modules/PublicModule.cs @@ -5,7 +5,7 @@ using TextCommandFramework.Services; namespace TextCommandFramework.Modules { - public class PublicModule : ModuleBase + public class PublicModule : ModuleBase { public PictureService PictureService { get; set; } public PlayerService PlayerService { get; set; } diff --git a/Program.cs b/Program.cs index 8222e0b..663fd38 100644 --- a/Program.cs +++ b/Program.cs @@ -26,15 +26,15 @@ namespace TextCommandFramework using (var services = ConfigureServices()) { - var client = services.GetRequiredService(); + var client = services.GetRequiredService(); client.Log += LogAsync; client.LoggedIn += () => Client_LoggedIn(client); - client.Ready += () => Client_Ready(client); + client.ShardReady += (shard) => Client_Ready(shard); services.GetRequiredService().Log += LogAsync; - await Connect(client); - //client.Disconnected += async (ex) => await Connect(client); + await client.LoginAsync(TokenType.Bot, TOKEN); + await client.StartAsync(); await services.GetRequiredService().InitializeAsync(); @@ -42,12 +42,6 @@ namespace TextCommandFramework } } - public async Task Connect(DiscordSocketClient client) - { - await client.LoginAsync(TokenType.Bot, TOKEN); - await client.StartAsync(); - } - private async Task Client_Ready(DiscordSocketClient client) { var announceLoginGuild = ulong.Parse(Environment.GetEnvironmentVariable("ANNOUNCE_LOGIN_GUILD") ?? "0"); @@ -69,7 +63,7 @@ namespace TextCommandFramework await channel.SendMessageAsync("@everyone LIVE!"); } - private Task Client_LoggedIn(DiscordSocketClient client) + private Task Client_LoggedIn(DiscordShardedClient client) { Console.WriteLine("Successfully logged in!"); @@ -88,9 +82,10 @@ namespace TextCommandFramework return new ServiceCollection() .AddSingleton(new DiscordSocketConfig { - GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent + GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent, + TotalShards = 3 }) - .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Services/CommandHandlingService.cs b/Services/CommandHandlingService.cs index 7dc9972..65c6ae8 100644 --- a/Services/CommandHandlingService.cs +++ b/Services/CommandHandlingService.cs @@ -12,7 +12,7 @@ namespace TextCommandFramework.Services public class CommandHandlingService { private readonly CommandService _commands; - private readonly DiscordSocketClient _discord; + private readonly DiscordShardedClient _discord; private readonly IServiceProvider _services; private readonly string CommandPrefix = "!"; @@ -20,7 +20,7 @@ namespace TextCommandFramework.Services public CommandHandlingService(IServiceProvider services) { _commands = services.GetRequiredService(); - _discord = services.GetRequiredService(); + _discord = services.GetRequiredService(); _services = services; _commands.CommandExecuted += CommandExecutedAsync; @@ -46,7 +46,7 @@ namespace TextCommandFramework.Services //Check if the message sent has the specified prefix if (!message.HasStringPrefix(CommandPrefix, ref argPos)) return; - var context = new SocketCommandContext(_discord, message); + var context = new ShardedCommandContext(_discord, message); await _commands.ExecuteAsync(context, argPos, _services); } diff --git a/Services/PlayerService.cs b/Services/PlayerService.cs index 93fbc39..0c7f5e8 100644 --- a/Services/PlayerService.cs +++ b/Services/PlayerService.cs @@ -25,7 +25,8 @@ namespace Kasbot.Services IAudioClient audioClient = await channel.ConnectAsync(); audioClient.Disconnected += (ex) => Stop(Context.Guild.Id); - + audioClient.StreamDestroyed += (ex) => Stop(Context.Guild.Id); + conn.AudioClient = audioClient; if (Clients.ContainsKey(Context.Guild.Id))