Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for incoming webhook commands #345

Merged
merged 5 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// IApplicationAuthorizedWebhookEvent.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a webhook event for when an application is authorized.
/// </summary>
public interface IApplicationAuthorizedWebhookEvent
{
/// <summary>
/// Gets the user that authorized the application.
/// </summary>
IUser User { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the scopes that the application was authorized for.
/// </summary>
IReadOnlyList<string> Scopes { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the guild that the application was authorized for, if <see cref="IntegrationType"/> is <see cref="ApplicationIntegrationType.GuildInstallable"/>.
/// </summary>
Optional<IPartialGuild> Guild { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the integration type of the application.
/// </summary>
Optional<ApplicationIntegrationType> IntegrationType { get; }
Fixed Show fixed Hide fixed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// IWebhookEvent.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using OneOf;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a webhook event.
/// </summary>
public interface IWebhookEvent
{
/// <summary>
/// Gets the type of the event.
/// </summary>
WebhookEventType Type { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the timestamp of the event.
/// </summary>
DateTimeOffset Timestamp { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the data of the event.
/// </summary>
OneOf<IApplicationAuthorizedWebhookEvent, IEntitlement> Data { get; }
Fixed Show fixed Hide fixed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// IWebhookEventPayload.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a payload for a webhook event.
/// </summary>
public interface IWebhookEventPayload
{
/// <summary>
/// Gets the version of this payload. Currently, always 1.
/// </summary>
int Version { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the ID of the application.
/// </summary>
Snowflake ApplicationID { get; }
Fixed Show fixed Hide fixed

/// <summary>
/// Gets the type of the payload.
/// </summary>
WebhookEventPayloadType Type { get; }
Fixed Show fixed Hide fixed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// WebhookEventPayloadType.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents the type of webhook event payload.
/// </summary>
public enum WebhookEventPayloadType
{
/// <summary>
/// The payload is a ping.
/// </summary>
Ping,

Check notice

Code scanning / InspectCode

Type member is never used: Non-private accessibility Note

Enum member 'Ping' is never used

/// <summary>
/// The payload is an event with data.
/// </summary>
Event

Check notice

Code scanning / InspectCode

Type member is never used: Non-private accessibility Note

Enum member 'Event' is never used
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// WebhookEventType.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents the type of webhook event.
/// </summary>
public enum WebhookEventType
{
/// <summary>
/// The application was authorized to a user or guild.
/// </summary>
ApplicationAuthorized,

Check notice

Code scanning / InspectCode

Type member is never used: Non-private accessibility Note

Enum member 'ApplicationAuthorized' is never used

/// <summary>
/// An entitlement was created.
/// </summary>
EntitlementCreate,

Check notice

Code scanning / InspectCode

Type member is never used: Non-private accessibility Note

Enum member 'EntitlementCreate' is never used

/// <summary>
/// User was added to a Quest (currently unavailable).
/// </summary>
QuestUserEnrollment,

Check notice

Code scanning / InspectCode

Type member is never used: Non-private accessibility Note

Enum member 'QuestUserEnrollment' is never used
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Ctemplates/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cusers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cvoice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cwebhookevents/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cwebhooks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Crest/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Crest_005Cobjects/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ApplicationAuthorizedWebhookEvent.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;

namespace Remora.Discord.API.Objects;

/// <inheritdoc cref="IApplicationAuthorizedWebhookEvent"/>
[PublicAPI]
public record ApplicationAuthorizedWebhookEvent
(
IUser User,
IReadOnlyList<string> Scopes,
Optional<IPartialGuild> Guild = default,
Optional<ApplicationIntegrationType> IntegrationType = default
) : IApplicationAuthorizedWebhookEvent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// WebhookEvent.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using OneOf;
using Remora.Discord.API.Abstractions.Objects;

namespace Remora.Discord.API.Objects;

/// <inheritdoc cref="IWebhookEvent"/>
public record WebhookEvent

Check notice

Code scanning / InspectCode

Class is never instantiated: Non-private accessibility Note

Record 'WebhookEvent' is never instantiated
(
WebhookEventType Type,
DateTimeOffset Timestamp,
OneOf<IApplicationAuthorizedWebhookEvent, IEntitlement> Data
) : IWebhookEvent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// WebhookEventPayload.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;

namespace Remora.Discord.API.Objects;

/// <inheritdoc cref="IWebhookEventPayload"/>
[PublicAPI]
public record WebhookEventPayload
(
int Version,
Snowflake ApplicationID,
WebhookEventPayloadType Type
) : IWebhookEventPayload;
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public static IServiceCollection ConfigureDiscordJsonConverters
.AddStickerObjectConverters()
.AddApplicationRoleConnectionObjectConverters()
.AddMonetizationConverters()
.AddPollObjectConverters();
.AddPollObjectConverters()
.AddWebhookEventObjectConverters();

options.AddDataObjectConverter<IUnknownEvent, UnknownEvent>();
options.AddConverter<PropertyErrorDetailsConverter>();
Expand Down Expand Up @@ -1352,4 +1353,15 @@ this JsonSerializerOptions options

return options;
}

private static JsonSerializerOptions AddWebhookEventObjectConverters(this JsonSerializerOptions options)
{
options.AddDataObjectConverter<IWebhookEvent, WebhookEvent>()
.WithPropertyConverter(we => we.Type, new StringEnumConverter<WebhookEventType>(new SnakeCaseNamingPolicy()));

options.AddDataObjectConverter<IApplicationAuthorizedWebhookEvent, ApplicationAuthorizedWebhookEvent>();
options.AddDataObjectConverter<IWebhookEventPayload, WebhookEventPayload>();

return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Ctemplates/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cusers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cvoice/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cwebhookevents/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cobjects_005Cwebhooks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cwebhookevents/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=json_005Cconverters/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=json_005Cconverters_005Cinternal/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=json_005Cpolicies/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Loading
Loading