diff --git a/pkg/connector/client.go b/pkg/connector/client.go index ece6371..cc0f357 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -31,6 +31,7 @@ import ( "github.com/rs/zerolog" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/bridgev2/simplevent" "maunium.net/go/mautrix/bridgev2/status" "go.mau.fi/mautrix-discord/pkg/discordid" @@ -314,6 +315,22 @@ func (d *DiscordClient) syncGuilds(ctx context.Context) { } } +// deleteGuildPortalSpace queues a remote event that deletes a guild space +// (including children). +func (d *DiscordClient) deleteGuildPortalSpace(ctx context.Context, guildID string) { + log := zerolog.Ctx(ctx) + log.Info().Msg("Unbridging guild by deleting the entire space") + + d.connector.Bridge.QueueRemoteEvent(d.UserLogin, &simplevent.ChatDelete{ + EventMeta: simplevent.EventMeta{ + Type: bridgev2.RemoteEventChatDelete, + PortalKey: d.guildPortalKeyFromID(guildID), + }, + OnlyForMe: true, + Children: true, + }) +} + func (d *DiscordClient) bridgeGuild(ctx context.Context, guildID string) error { log := zerolog.Ctx(ctx) diff --git a/pkg/connector/handlediscord.go b/pkg/connector/handlediscord.go index 6c5f8d8..87022fe 100644 --- a/pkg/connector/handlediscord.go +++ b/pkg/connector/handlediscord.go @@ -242,6 +242,7 @@ func (d *DiscordClient) handleDiscordEvent(rawEvt any) { log := d.UserLogin.Log.With().Str("action", "handle discord event"). Type("event_type", rawEvt). Logger() + ctx := log.WithContext(d.UserLogin.Bridge.BackgroundCtx) switch evt := rawEvt.(type) { case *discordgo.Ready: @@ -286,6 +287,13 @@ func (d *DiscordClient) handleDiscordEvent(rawEvt any) { // TODO case *discordgo.MessageReactionRemoveEmoji: (needs impl. in discordgo) case *discordgo.PresenceUpdate: return + case *discordgo.GuildDelete: + if evt.Unavailable { + log.Warn().Str("guild_id", evt.ID).Msg("Guild became unavailable") + // For now, leave the portals alone if the guild only went away due to an outage. + return + } + d.deleteGuildPortalSpace(ctx, evt.ID) case *discordgo.Event: // For presently unknown reasons sometimes discordgo won't unmarshal // events into their proper corresponding structs.