From a1741fbf537b365ec5523b151ffa3bbefbee7bf3 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 10 Nov 2021 23:23:57 +0100 Subject: [PATCH] [core] Add `ban`, `clearchat` and `timeout` events Signed-off-by: Knut Ahlers --- events.go | 7 +++++++ irc.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ wiki/Events.md | 30 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/events.go b/events.go index 332445e..aef6d84 100644 --- a/events.go +++ b/events.go @@ -3,7 +3,9 @@ package main func ptrStr(s string) *string { return &s } var ( + eventTypeBan = ptrStr("ban") eventTypeBits = ptrStr("bits") + eventTypeClearChat = ptrStr("clearchat") eventTypeJoin = ptrStr("join") eventTypeHost = ptrStr("host") eventTypePart = ptrStr("part") @@ -13,6 +15,7 @@ var ( eventTypeSub = ptrStr("sub") eventTypeSubgift = ptrStr("subgift") eventTypeSubmysterygift = ptrStr("submysterygift") + eventTypeTimeout = ptrStr("timeout") eventTypeWhisper = ptrStr("whisper") eventTypeTwitchCategoryUpdate = ptrStr("category_update") @@ -21,6 +24,9 @@ var ( eventTypeTwitchTitleUpdate = ptrStr("title_update") knownEvents = []*string{ + eventTypeBan, + eventTypeBits, + eventTypeClearChat, eventTypeJoin, eventTypeHost, eventTypePart, @@ -30,6 +36,7 @@ var ( eventTypeSub, eventTypeSubgift, eventTypeSubmysterygift, + eventTypeTimeout, eventTypeWhisper, eventTypeTwitchCategoryUpdate, diff --git a/irc.go b/irc.go index 887d929..12caa7d 100644 --- a/irc.go +++ b/irc.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" "sync" + "time" "github.com/Luzifer/twitch-bot/plugins" "github.com/Luzifer/twitch-bot/twitch" @@ -116,6 +117,12 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) { }) go i.ExecuteJoins(config.Channels) + case "CLEARCHAT": + // CLEARCHAT (Twitch Commands) + // Purge a user’s messages, typically after a user is banned from + // chat or timed out. + i.handleClearChat(m) + case "JOIN": // JOIN (Default IRC Command) // User enters the channel, might be triggered multiple times @@ -133,6 +140,7 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) { i.handlePart(m) case "PING": + // PING (Default IRC Command) // Handled by the library, just here to prevent trace-logging every ping case "PRIVMSG": @@ -184,6 +192,43 @@ func (ircHandler) getChannel(m *irc.Message) string { return "" } +func (i ircHandler) handleClearChat(m *irc.Message) { + seconds, secondsErr := strconv.Atoi(string(m.Tags["ban-duration"])) + targetUserID, hasTargetUserID := m.Tags.GetTag("target-user-id") + + var ( + evt *string + fields = plugins.FieldCollection{ + "channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel + } + ) + + switch { + case secondsErr == nil && hasTargetUserID: + // User & Duration = Timeout + evt = eventTypeTimeout + fields["duration"] = time.Duration(seconds) * time.Second + fields["seconds"] = seconds + fields["target_id"] = targetUserID + fields["target_name"] = m.Trailing() + log.WithFields(log.Fields(fields)).Info("User was timeouted") + + case hasTargetUserID: + // User w/o Duration = Ban + evt = eventTypeBan + fields["target_id"] = targetUserID + fields["target_name"] = m.Trailing() + log.WithFields(log.Fields(fields)).Info("User was banned") + + default: + // No User = /clear + evt = eventTypeClearChat + log.WithFields(log.Fields(fields)).Info("Chat was cleared") + } + + go handleMessage(i.c, m, evt, fields) +} + func (i ircHandler) handleJoin(m *irc.Message) { go handleMessage(i.c, m, eventTypeJoin, nil) } diff --git a/wiki/Events.md b/wiki/Events.md index fafea32..7e83379 100644 --- a/wiki/Events.md +++ b/wiki/Events.md @@ -1,5 +1,15 @@ # Available Events +## `ban` + +Moderator action caused a user to be banned from chat. + +Fields: + +- `channel` - The channel the event occurred in +- `target_id` - The ID of the user being banned +- `target_name` - The login-name of the user being banned + ## `bits` User spent bits in the channel. The full message is available like in a normal chat message, additionally the `{{ .bits }}` field is added with the total amount of bits spent. @@ -19,6 +29,14 @@ Fields: - `category` - The name of the new game / category - `channel` - The channel the event occurred in +## `clearchat` + +Moderator action caused chat to be cleared. + +Fields: + +- `channel` - The channel the event occurred in + ## `join` User joined the channel-chat. This is **NOT** an indicator they are viewing, the event is **NOT** reliably sent when the user really joined the chat. The event will be sent with some delay after they join the chat and is sometimes repeated multiple times during their stay. So **DO NOT** use this to greet users! @@ -116,6 +134,18 @@ Fields: - `plan` - The sub-plan they are using (`1000` = T1, `2000` = T2, `3000` = T3, `Prime`) - `username` - The login-name of the user who gifted the subscription +## `timeout` + +Moderator action caused a user to be timeouted from chat. + +Fields: + +- `channel` - The channel the event occurred in +- `duration` - The timeout duration (`time.Duration`, nanoseconds) +- `seconds` - The timeout duration (`int`, seconds) +- `target_id` - The ID of the user being timeouted +- `target_name` - The login-name of the user being timeouted + ## `title_update` The current title for the channel was changed. (This event has some delay to the real category change!)