diff --git a/Cliptok.csproj b/Cliptok.csproj index e1f2c425..c97e36cd 100644 --- a/Cliptok.csproj +++ b/Cliptok.csproj @@ -5,14 +5,17 @@ <TargetFramework>net8.0</TargetFramework> <LangVersion>12.0</LangVersion> <UserSecretsId>d9345310-5908-4697-8613-28a24d06d183</UserSecretsId> - </PropertyGroup> + <RestoreAdditionalProjectSources> + https://nuget.pkg.github.com/Erisa/index.json + </RestoreAdditionalProjectSources> + </PropertyGroup> <ItemGroup> <PackageReference Include="Abyssal.HumanDateParser" Version="2.0.0-20191113.1" /> - <PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02309" /> - <PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-02309" /> - <PackageReference Include="DSharpPlus.SlashCommands" Version="5.0.0-nightly-02309" /> + <PackageReference Include="DSharpPlus" Version="5.0.0-Nightly-Erisa-3-294f8244db8ec86723c58c2f16f64020ce790f4f" /> + <PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-Nightly-Erisa-3-294f8244db8ec86723c58c2f16f64020ce790f4f" /> + <PackageReference Include="DSharpPlus.SlashCommands" Version="5.0.0-Nightly-Erisa-3-294f8244db8ec86723c58c2f16f64020ce790f4f" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Serilog" Version="4.0.0" /> <PackageReference Include="Serilog.Expressions" Version="5.0.0" /> diff --git a/Commands/Utility.cs b/Commands/Utility.cs index 33de9553..fff1edc6 100644 --- a/Commands/Utility.cs +++ b/Commands/Utility.cs @@ -6,13 +6,14 @@ internal class Utility : BaseCommandModule [Description("Pong? This command lets you know whether I'm working well.")] public async Task Ping(CommandContext ctx) { + ctx.Client.Logger.LogDebug(ctx.Client.GetConnectionLatency(Program.cfgjson.ServerID).ToString()); DiscordMessage return_message = await ctx.Message.RespondAsync("Pinging..."); ulong ping = (return_message.Id - ctx.Message.Id) >> 22; char[] choices = new char[] { 'a', 'e', 'o', 'u', 'i', 'y' }; char letter = choices[Program.rand.Next(0, choices.Length)]; await return_message.ModifyAsync($"P{letter}ng! ð\n" + $"⢠It took me `{ping}ms` to reply to your message!\n" + - $"⢠Last Websocket Heartbeat took `{ctx.Client.Ping}ms`!"); + $"⢠Last Websocket Heartbeat took `{Math.Round(ctx.Client.GetConnectionLatency(0).TotalMilliseconds, 0)}ms`!"); } [Command("edit")] diff --git a/Events/HeartbeatEvent.cs b/Events/HeartbeatEvent.cs index 73c2b587..7613dcdd 100644 --- a/Events/HeartbeatEvent.cs +++ b/Events/HeartbeatEvent.cs @@ -1,19 +1,22 @@ -namespace Cliptok.Events +using DSharpPlus.Net.Gateway; + +namespace Cliptok.Events { public class HeartbeatEvent { - public static async Task OnHeartbeat(DiscordClient client, HeartbeatedEventArgs e) + public static async Task OnHeartbeat(IGatewayClient client) { + Program.discord.Logger.LogDebug("Heartbeat ping: {ping}", client.Ping.TotalMicroseconds); if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL")) && client.IsConnected) { - var response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL") + client.Ping); + var response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL") + client.Ping.TotalMicroseconds); if (response.StatusCode == HttpStatusCode.OK) { - client.Logger.LogDebug("Heartbeat ping succeeded."); + Program.discord.Logger.LogDebug("Heartbeat ping succeeded."); } else { - client.Logger.LogError("Heartbeat ping sent: {status} {content}", (int)response.StatusCode, await response.Content.ReadAsStringAsync()); + Program.discord.Logger.LogError("Heartbeat ping sent: {status} {content}", (int)response.StatusCode, await response.Content.ReadAsStringAsync()); } return; } diff --git a/Events/ReadyEvent.cs b/Events/ReadyEvent.cs index 64c87acf..742c28b2 100644 --- a/Events/ReadyEvent.cs +++ b/Events/ReadyEvent.cs @@ -134,6 +134,21 @@ public static async Task OnStartup(DiscordClient client) $"```\n" + $"{commitMessage}\n" + $"```"); + + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL"))) + { + var response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL")); + if (response.StatusCode == HttpStatusCode.OK) + { + discord.Logger.LogDebug("Heartbeat ping succeeded."); + } + else + { + discord.Logger.LogError("Heartbeat ping sent: {status} {content}", (int)response.StatusCode, await response.Content.ReadAsStringAsync()); + } + return; + } + } } diff --git a/Helpers/SecurityActionHelpers.cs b/Helpers/SecurityActionHelpers.cs index f69f4969..eedfb895 100644 --- a/Helpers/SecurityActionHelpers.cs +++ b/Helpers/SecurityActionHelpers.cs @@ -4,7 +4,7 @@ public class SecurityActionHelpers { public static async Task<JToken> GetCurrentSecurityActions(ulong guildId) { - using HttpRequestMessage getActionsRequest = new(HttpMethod.Get, $"https://discord.com/api/v{Program.discord.GatewayVersion}/guilds/{guildId}"); + using HttpRequestMessage getActionsRequest = new(HttpMethod.Get, $"https://discord.com/api/v10/guilds/{guildId}"); getActionsRequest.Headers.Authorization = new AuthenticationHeaderValue("Bot", Environment.GetEnvironmentVariable("CLIPTOK_TOKEN") ?? Program.cfgjson.Core.Token); var getActionsResponse = await Program.httpClient.SendAsync(getActionsRequest); @@ -15,7 +15,7 @@ public static async Task<HttpResponseMessage> SetCurrentSecurityActions(ulong gu { // create & send request - using HttpRequestMessage setActionsRequest = new(HttpMethod.Put, $"https://discord.com/api/v{Program.discord.GatewayVersion}/guilds/{guildId}/incident-actions"); + using HttpRequestMessage setActionsRequest = new(HttpMethod.Put, $"https://discord.com/api/v10/guilds/{guildId}/incident-actions"); setActionsRequest.Headers.Authorization = new AuthenticationHeaderValue("Bot", Environment.GetEnvironmentVariable("CLIPTOK_TOKEN") ?? Program.cfgjson.Core.Token); setActionsRequest.Content = new StringContent(newSecurityActions, Encoding.UTF8, "application/json"); diff --git a/Program.cs b/Program.cs index d1e10e03..02812681 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,6 @@ +using DSharpPlus.Clients; +using DSharpPlus.Extensions; +using DSharpPlus.Net.Gateway; using System.Reflection; namespace Cliptok @@ -11,6 +14,19 @@ public class AvatarResponseBody public string Key { get; set; } } + class GatewayController : IGatewayController + { + public async ValueTask HeartbeatedAsync(IGatewayClient client) + { + await HeartbeatEvent.OnHeartbeat(client); + } + + public async ValueTask ZombiedAsync(IGatewayClient client) + { + await Task.CompletedTask; + } + } + class Program : BaseCommandModule { public static DiscordClient discord; @@ -136,7 +152,12 @@ static async Task Main(string[] _) logging.AddSerilog(); }); - discordBuilder.ConfigureGatewayClient(clientConfig => + discordBuilder.ConfigureServices(services => + { + services.Replace<IGatewayController, GatewayController>(); + }); + + discordBuilder.ConfigureExtraFeatures(clientConfig => { clientConfig.LogUnknownEvents = false; clientConfig.LogUnknownAuditlogs = false; @@ -159,7 +180,6 @@ static async Task Main(string[] _) .HandleThreadListSynced(ThreadEvents.Discord_ThreadListSynced) .HandleThreadMemberUpdated(ThreadEvents.Discord_ThreadMemberUpdated) .HandleThreadMembersUpdated(ThreadEvents.Discord_ThreadMembersUpdated) - .HandleHeartbeated(HeartbeatEvent.OnHeartbeat) .HandleGuildBanRemoved(UnbanEvent.OnUnban) .HandleVoiceStateUpdated(VoiceEvents.VoiceStateUpdate) .HandleChannelUpdated(ChannelEvents.ChannelUpdated)