Skip to content

Commit

Permalink
Refactor for new D#+, use temporary custom build
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Jul 11, 2024
1 parent 6b82530 commit 467617d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
11 changes: 7 additions & 4 deletions Cliptok.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
3 changes: 2 additions & 1 deletion Commands/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
13 changes: 8 additions & 5 deletions Events/HeartbeatEvent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions Events/ReadyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

}
Expand Down
4 changes: 2 additions & 2 deletions Helpers/SecurityActionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down
24 changes: 22 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using DSharpPlus.Clients;
using DSharpPlus.Extensions;
using DSharpPlus.Net.Gateway;
using System.Reflection;

namespace Cliptok
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 467617d

Please sign in to comment.