Skip to content

Commit

Permalink
feat: clan join request events
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Jan 23, 2025
1 parent 6d3e6e5 commit 2c9de64
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 17 deletions.
107 changes: 96 additions & 11 deletions DisCatSharp/Clients/DiscordClient.Dispatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ internal async Task HandleDispatchAsync(GatewayPayload payload)
DiscordEntitlement ent = default;
DiscordSubscription sub = default;
JToken rawMbr = default;
DiscordGuildJoinRequest joinRequest = default;
JoinRequestStatusType joinRequestStatusType = default;
var rawRefMsg = dat["referenced_message"]; // TODO: Can we remove this?

#endregion
Expand Down Expand Up @@ -113,8 +115,8 @@ internal async Task HandleDispatchAsync(GatewayPayload payload)

case "voice_channel_status_update":
cid = (ulong)dat["id"]!;
var status = (string?)dat["status"]!;
await this.OnVoiceChannelStatusUpdateAsync((ulong)dat["guild_id"], cid, status).ConfigureAwait(false);
var voiceChannelStatus = (string?)dat["status"]!;
await this.OnVoiceChannelStatusUpdateAsync((ulong)dat["guild_id"], cid, voiceChannelStatus).ConfigureAwait(false);
break;

case "channel_topic_update":
Expand Down Expand Up @@ -640,8 +642,22 @@ internal async Task HandleDispatchAsync(GatewayPayload payload)
break;

case "guild_join_request_create":
gid = (ulong)dat["guild_id"]!;
joinRequestStatusType = Enum.TryParse<JoinRequestStatusType>((string)dat["status"]!, out var rcs) ? rcs : JoinRequestStatusType.Unknown;
joinRequest = DiscordJson.DeserializeObject<DiscordGuildJoinRequest>(dat["request"]!.ToString(), this);
await this.OnGuildJoinRequestCreateAsync(gid, joinRequestStatusType, joinRequest);
break;
case "guild_join_request_update":
case "guild_join_request_delete": // Deprecated
gid = (ulong)dat["guild_id"]!;
joinRequestStatusType = Enum.TryParse<JoinRequestStatusType>((string)dat["status"]!, out var rcu) ? rcu : JoinRequestStatusType.Unknown;
joinRequest = DiscordJson.DeserializeObject<DiscordGuildJoinRequest>(dat["request"]!.ToString(), this);
await this.OnGuildJoinRequestUpdateAsync(gid, joinRequestStatusType, joinRequest);
break;
case "guild_join_request_delete":
gid = (ulong)dat["guild_id"]!;
uid = (ulong)dat["user_id"]!;
var requestId = (ulong)dat["id"]!;
await this.OnGuildJoinRequestDeleteAsync(requestId, uid, gid);
break;

case "entitlement_create":
Expand Down Expand Up @@ -2423,11 +2439,13 @@ internal async Task OnGuildSoundboardSoundsUpdateEventAsync(List<DiscordSoundboa
/// <param name="guildId">The guild id.</param>
internal async Task OnSoundboardSoundsEventAsync(List<DiscordSoundboardSound> sounds, ulong guildId)
{
var guild = this.Guilds.TryGetValue(guildId, out var cachedGuild) ? cachedGuild : new()
{
Id = guildId,
Discord = this
};
var guild = this.Guilds.TryGetValue(guildId, out var cachedGuild)
? cachedGuild
: new()
{
Id = guildId,
Discord = this
};

if (this.Guilds.ContainsKey(guildId))
{
Expand Down Expand Up @@ -2487,13 +2505,13 @@ internal async Task OnInviteCreateEventAsync(ulong channelId, ulong guildId, Dis
/// <param name="dat">The raw invite.</param>
internal async Task OnInviteDeleteEventAsync(ulong channelId, ulong guildId, JToken dat)
{
var guild = this.InternalGetCachedGuild(guildId);
var guild = this.InternalGetCachedGuild(guildId)!;
var channel = this.InternalGetCachedChannel(channelId);

if (!guild.Invites.TryRemove(dat["code"].ToString(), out var invite))
if (!guild.Invites.TryRemove(dat["code"]!.ToString(), out var invite))
{
invite = dat.ToObject<DiscordInvite>();
invite.Discord = this;
invite!.Discord = this;
}

invite.IsRevoked = true;
Expand All @@ -2509,6 +2527,73 @@ internal async Task OnInviteDeleteEventAsync(ulong channelId, ulong guildId, JTo

#endregion

#region Guild Member Application

/// <summary>
/// Handles the guild join request create event.
/// </summary>
internal async Task OnGuildJoinRequestCreateAsync(ulong guildId, JoinRequestStatusType statusType, DiscordGuildJoinRequest request)
{
var guild = this.InternalGetCachedGuild(guildId) ?? new()
{
Id = guildId,
Discord = this
};
var ea = new GuildJoinRequestCreateEventArgs(this.ServiceProvider)
{
Request = request,
User = request.User,
Status = statusType,
Guild = guild
};
await this._guildJoinRequestCreated.InvokeAsync(this, ea).ConfigureAwait(false);
}

/// <summary>
/// Handles the guild join request update event.
/// </summary>
internal async Task OnGuildJoinRequestUpdateAsync(ulong guildId, JoinRequestStatusType statusTyp, DiscordGuildJoinRequest request)
{
var guild = this.InternalGetCachedGuild(guildId) ?? new()
{
Id = guildId,
Discord = this
};
var ea = new GuildJoinRequestUpdateEventArgs(this.ServiceProvider)
{
Request = request,
User = request.User,
Status = statusTyp,
Guild = guild
};
await this._guildJoinRequestUpdated.InvokeAsync(this, ea).ConfigureAwait(false);
}

/// <summary>
/// Handles the guild join request delete event.
/// </summary>
/// <param name="requestId">The request id.</param>
/// <param name="userId">The user id.</param>
/// <param name="guildId">The guild id.</param>
internal async Task OnGuildJoinRequestDeleteAsync(ulong requestId, ulong userId, ulong guildId)
{
var user = this.GetCachedOrEmptyUserInternal(userId);
var guild = this.InternalGetCachedGuild(guildId) ?? new()
{
Id = guildId,
Discord = this
};
var ea = new GuildJoinRequestDeleteEventArgs(this.ServiceProvider)
{
User = user,
Guild = guild,
RequestId = requestId
};
await this._guildJoinRequestDeleted.InvokeAsync(this, ea).ConfigureAwait(false);
}

#endregion

#region Message

/// <summary>
Expand Down
45 changes: 44 additions & 1 deletion DisCatSharp/Clients/DiscordClient.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public event AsyncEventHandler<DiscordClient, GuildSoundboardSoundsUpdateEventAr
private AsyncEvent<DiscordClient, GuildSoundboardSoundsUpdateEventArgs> _guildSoundboardSoundsUpdated;

/// <summary>
/// Fired in response to <see cref="DiscordClient.RequestSoundboardSoundsAsync"/>.
/// Fired in response to <see cref="DiscordClient.RequestSoundboardSoundsAsync" />.
/// </summary>
public event AsyncEventHandler<DiscordClient, SoundboardSoundsEventArgs> SoundboardSounds
{
Expand All @@ -695,6 +695,49 @@ public event AsyncEventHandler<DiscordClient, SoundboardSoundsEventArgs> Soundbo

#endregion

#region Guild Member Application

/// <summary>
/// Fired when a guild join request is created.
/// For this Event you need the <see cref="DiscordIntents.GuildExpressions" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestCreateEventArgs> GuildJoinRequestCreated
{
add => this._guildJoinRequestCreated.Register(value);
remove => this._guildJoinRequestCreated.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestCreateEventArgs> _guildJoinRequestCreated;

/// <summary>
/// Fired when a guild join request is updated.
/// For this Event you need the <see cref="DiscordIntents.Guilds" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestUpdateEventArgs> GuildJoinRequestUpdated
{
add => this._guildJoinRequestUpdated.Register(value);
remove => this._guildJoinRequestUpdated.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestUpdateEventArgs> _guildJoinRequestUpdated;

/// <summary>
/// Fired when a guild join request is deleted.
/// For this Event you need the <see cref="DiscordIntents.Guilds" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestDeleteEventArgs> GuildJoinRequestDeleted
{
add => this._guildJoinRequestDeleted.Register(value);
remove => this._guildJoinRequestDeleted.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestDeleteEventArgs> _guildJoinRequestDeleted;

#endregion

#region Invite

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions DisCatSharp/Clients/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ internal void InternalSetup()
this._guildSoundboardSoundDeleted = new("GUILD_SOUNDBOARD_SOUND_DELETED", EventExecutionLimit, this.EventErrorHandler);
this._guildSoundboardSoundsUpdated = new("GUILD_SOUNDBOARD_SOUNDS_UPDATED", EventExecutionLimit, this.EventErrorHandler);
this._soundboardSounds = new("SOUNDBOARD_SOUNDS", EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestCreated = new("GUILD_JOIN_REQUEST_CREATED", EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestUpdated = new("GUILD_JOIN_REQUEST_UPDATED", EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestDeleted = new("GUILD_JOIN_REQUEST_DELETED", EventExecutionLimit, this.EventErrorHandler);

this.GuildsInternal.Clear();
this.EmojisInternal.Clear();
Expand Down
67 changes: 67 additions & 0 deletions DisCatSharp/Clients/DiscordShardedClient.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,49 @@ public event AsyncEventHandler<DiscordClient, SoundboardSoundsEventArgs> Soundbo

#endregion

#region Guild Member Application

/// <summary>
/// Fired when a guild join request is created.
/// For this Event you need the <see cref="DiscordIntents.GuildExpressions" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestCreateEventArgs> GuildJoinRequestCreated
{
add => this._guildJoinRequestCreated.Register(value);
remove => this._guildJoinRequestCreated.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestCreateEventArgs> _guildJoinRequestCreated;

/// <summary>
/// Fired when a guild join request is updated.
/// For this Event you need the <see cref="DiscordIntents.Guilds" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestUpdateEventArgs> GuildJoinRequestUpdated
{
add => this._guildJoinRequestUpdated.Register(value);
remove => this._guildJoinRequestUpdated.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestUpdateEventArgs> _guildJoinRequestUpdated;

/// <summary>
/// Fired when a guild join request is deleted.
/// For this Event you need the <see cref="DiscordIntents.Guilds" /> intent specified in
/// <seealso cref="DiscordConfiguration.Intents" />
/// </summary>
public event AsyncEventHandler<DiscordClient, GuildJoinRequestDeleteEventArgs> GuildJoinRequestDeleted
{
add => this._guildJoinRequestDeleted.Register(value);
remove => this._guildJoinRequestDeleted.Unregister(value);
}

private AsyncEvent<DiscordClient, GuildJoinRequestDeleteEventArgs> _guildJoinRequestDeleted;

#endregion

#region Message Reaction

/// <summary>
Expand Down Expand Up @@ -2131,6 +2174,30 @@ private Task Client_SoundboardSoundsUpdated(DiscordClient client, GuildSoundboar
private Task Client_SoundboardSounds(DiscordClient client, SoundboardSoundsEventArgs e)
=> this._soundboardSounds.InvokeAsync(client, e);

/// <summary>
/// Handles the guild join request created event.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="e">The event args.</param>
private Task Client_GuildJoinRequestCreated(DiscordClient client, GuildJoinRequestCreateEventArgs e)
=> this._guildJoinRequestCreated.InvokeAsync(client, e);

/// <summary>
/// Handles the guild join request updated event.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="e">The event args.</param>
private Task Client_GuildJoinRequestUpdated(DiscordClient client, GuildJoinRequestUpdateEventArgs e)
=> this._guildJoinRequestUpdated.InvokeAsync(client, e);

/// <summary>
/// Handles the guild join request deleted event.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="e">The event args.</param>
private Task Client_GuildJoinRequestDeleted(DiscordClient client, GuildJoinRequestDeleteEventArgs e)
=> this._guildJoinRequestDeleted.InvokeAsync(client, e);


#endregion
}
9 changes: 9 additions & 0 deletions DisCatSharp/Clients/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ private void InternalSetup()
this._guildSoundboardSoundDeleted = new("GUILD_SOUNDBOARD_SOUND_DELETED", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
this._guildSoundboardSoundsUpdated = new("GUILD_SOUNDBOARD_SOUNDS_UPDATED", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
this._soundboardSounds = new("SOUNDBOARD_SOUNDS", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestCreated = new("GUILD_JOIN_REQUEST_CREATED", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestUpdated = new("GUILD_JOIN_REQUEST_UPDATED", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
this._guildJoinRequestDeleted = new("GUILD_JOIN_REQUEST_DELETED", DiscordClient.EventExecutionLimit, this.EventErrorHandler);
}

/// <summary>
Expand Down Expand Up @@ -783,6 +786,9 @@ private void HookEventHandlers(DiscordClient client)
client.GuildSoundboardSoundDeleted += this.Client_SoundboardSoundDeleted;
client.GuildSoundboardSoundsUpdated += this.Client_SoundboardSoundsUpdated;
client.SoundboardSounds += this.Client_SoundboardSounds;
client.GuildJoinRequestCreated += this.Client_GuildJoinRequestCreated;
client.GuildJoinRequestUpdated += this.Client_GuildJoinRequestUpdated;
client.GuildJoinRequestDeleted += this.Client_GuildJoinRequestDeleted;
}

/// <summary>
Expand Down Expand Up @@ -889,6 +895,9 @@ private void UnhookEventHandlers(DiscordClient client)
client.GuildSoundboardSoundDeleted -= this.Client_SoundboardSoundDeleted;
client.GuildSoundboardSoundsUpdated -= this.Client_SoundboardSoundsUpdated;
client.SoundboardSounds -= this.Client_SoundboardSounds;
client.GuildJoinRequestCreated -= this.Client_GuildJoinRequestCreated;
client.GuildJoinRequestUpdated -= this.Client_GuildJoinRequestUpdated;
client.GuildJoinRequestDeleted -= this.Client_GuildJoinRequestDeleted;
}

/// <summary>
Expand Down
Loading

0 comments on commit 2c9de64

Please sign in to comment.