Skip to content

Commit

Permalink
Improve default logging. Add resubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwinchester committed Jan 17, 2024
1 parent 462e82f commit 7e48d86
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 34 deletions.
29 changes: 24 additions & 5 deletions lib/tmi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ defmodule TMI do

@impl TMI.Handler
def handle_kick(channel, user, kicker) do
Logger.debug("[#{TMI.bot_string(__MODULE__)}] [#{channel}] #{user} was kicked by #{kicker}")
Logger.debug(
"[#{TMI.bot_string(__MODULE__)}] [#{channel}] #{user} was kicked by #{kicker}"
)
end

@impl TMI.Handler
Expand All @@ -152,7 +154,9 @@ defmodule TMI do

@impl TMI.Handler
def handle_mention(message, sender, channel) do
Logger.debug("[#{TMI.bot_string(__MODULE__)}] [#{channel}] MENTION - <#{sender}> #{message}")
Logger.debug(
"[#{TMI.bot_string(__MODULE__)}] [#{channel}] MENTION - <#{sender}> #{message}"
)
end

@impl TMI.Handler
Expand All @@ -172,7 +176,9 @@ defmodule TMI do

@impl TMI.Handler
def handle_unrecognized(msg) do
Logger.debug("[#{TMI.bot_string(__MODULE__)}] UNRECOGNIZED: #{inspect(msg, pretty: true)}")
Logger.debug(
"[#{TMI.bot_string(__MODULE__)}] UNRECOGNIZED: #{inspect(msg, pretty: true)}"
)
end

defoverridable(
Expand Down Expand Up @@ -205,17 +211,25 @@ defmodule TMI do

@doc false
def default_handle_event(%TMI.Events.Message{} = event, module) do
Logger.debug("[#{bot_string(module)}] [#{event.channel}] <#{event.user_login}> #{event.message}")
Logger.debug(
"[#{bot_string(module)}] [#{event.channel}] <#{event.user_login}> #{event.message}"
)
end

def default_handle_event(%TMI.Events.ChatAction{} = event, module) do
Logger.debug("[#{bot_string(module)}] [#{event.channel}] * <#{event.user_id}> #{event.message}")
Logger.debug(
"[#{bot_string(module)}] [#{event.channel}] * <#{event.user_id}> #{event.message}"
)
end

def default_handle_event(%TMI.Events.Whisper{} = event, module) do
Logger.debug("[#{bot_string(module)}] WHISPER - <#{event.user_login}> #{event.message}")
end

def default_handle_event(%TMI.Events.Unrecognized{msg: %ExIRC.Message{} = msg}, module) do
Logger.debug("[#{bot_string(module)}] [#{msg.cmd}] #{inspect(msg.args)}")
end

def default_handle_event(event, module) do
Logger.debug("[#{bot_string(module)}] EVENT\n#{inspect(event, pretty: true)}")
end
Expand Down Expand Up @@ -353,6 +367,11 @@ defmodule TMI do
params ->
tag_string
|> TMI.IRC.Tags.parse!()
|> tap(fn tags ->
if Map.get(tags, "custom-reward-id") do
IO.inspect(msg, pretty: true)
end
end)
|> TMI.Event.from_map_with_name(:message, params)
end

Expand Down
16 changes: 10 additions & 6 deletions lib/tmi/events/channel_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ defmodule TMI.Events.ChannelUpdate do
@moduledoc """
When we get a ROOMSTATE notice in IRC or a chat settings update from EventSub.
"""
use TMI.Event, fields: [
# TODO: Follower-only, sub-only, slow-mode, unique (is that r9k?)?
:emote_only?,
:channel_id,
:channel
]
use TMI.Event,
fields: [
:emote_only?,
:followers_only?,
:unique_only?,
:subs_only?,
:slow_delay,
:channel_id,
:channel
]
end
9 changes: 5 additions & 4 deletions lib/tmi/events/emote_mode.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule TMI.Events.EmoteMode do
@moduledoc false
use TMI.Event, fields: [
:channel,
:emote_only?
]
use TMI.Event,
fields: [
:channel,
:emote_only?
]
end
3 changes: 2 additions & 1 deletion lib/tmi/events/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule TMI.Events.Message do
:timestamp,
:user_id,
:user_login,
:user_type
:user_type,
:reward_id
]
end
35 changes: 35 additions & 0 deletions lib/tmi/events/resub.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule TMI.Events.Resub do
@moduledoc """
Sub event.
IRC `msg-id` is `sub` and EventSub name is `sub`.
"""
use TMI.Event,
fields: [
:id,
:channel_id,
:channel,
:plan,
:plan_name,
:system_message,
:timestamp,
:gifted?,
:goal_type,
:goal_current,
:goal_target,
:goal_contributions,
:share_streak?,
:months,
:badge_info,
:badges,
:color,
:cumulative_months,
:login,
:display_name,
:is_mod?,
:is_turbo?,
:is_sub?,
:is_vip?,
:user_id,
:user_type
]
end
7 changes: 1 addition & 6 deletions lib/tmi/events/sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule TMI.Events.Sub do
:goal_contributions,
:share_streak?,
:months,
# --- user fields ---
:badge_info,
:badges,
:color,
Expand All @@ -31,10 +30,6 @@ defmodule TMI.Events.Sub do
:is_sub?,
:is_vip?,
:user_id,
:user_type,
# --- recipient fields ---
:recipient_display_name,
:recipient_id,
:recipient_login
:user_type
]
end
8 changes: 7 additions & 1 deletion lib/tmi/fields.ex
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ defmodule TMI.Fields do
Applies only to messages with more than 9 characters. Is `true` if users must
post unique messages; otherwise, `false`.
"""
@type r9k? :: boolean()
@type unique_only? :: boolean()

@typedoc """
Twitch IRC tag `returning-chatter`.
Expand Down Expand Up @@ -609,6 +609,12 @@ defmodule TMI.Fields do
"""
@type is_vip? :: boolean()

@typedoc """
[UNDOCUMENTED]
Twitch IRC tag `custom-reward-id`. Got this in a message that was a custom reward.
"""
@type reward_id :: String.t()

## Unrecognized stuff...

@typedoc """
Expand Down
2 changes: 1 addition & 1 deletion lib/tmi/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule TMI.Handler do

## The common bits...

@callback handle_event(TMI.event()) :: any
@callback handle_event(event :: TMI.event()) :: any

@callback handle_mention(message :: String.t(), sender :: String.t(), channel :: String.t()) ::
any
Expand Down
2 changes: 1 addition & 1 deletion lib/tmi/irc/parsing/event_mapping.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule TMI.IRC.Parsing.EventMapping do
# "" => :sub,
# "" => :sub_message,
# "" => :sub_end,
# "" => :resub,
"resub" => :resub,
# "" => :sub_gift,
"submysterygift" => :community_sub_gift,
"subgift" => :sub_gift,
Expand Down
3 changes: 2 additions & 1 deletion lib/tmi/irc/parsing/tag_mapping.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule TMI.IRC.Parsing.TagMapping do
"bits" => :bits,
"client-nonce" => :client_nonce,
"color" => :color,
"custom-reward-id" => :reward_id,
"display-name" => :display_name,
"emotes" => :emotes,
"emote-only" => :emote_only?,
Expand Down Expand Up @@ -69,7 +70,7 @@ defmodule TMI.IRC.Parsing.TagMapping do
"pinned-chat-paid-exponent" => :exponent,
"pinned-chat-paid-is-system-message" => :system_message?,
"pinned-chat-paid-level" => :level,
"r9k" => :r9k?,
"r9k" => :unique_only?,
"reply-parent-display-name" => :parent_user_display_name,
"reply-parent-msg-body" => :parent_message,
"reply-parent-msg-id" => :parent_id,
Expand Down
8 changes: 8 additions & 0 deletions lib/tmi/irc/tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ defmodule TMI.IRC.Tags do
{tag_name(key), String.to_integer(val)}
end

defp tag_map({"slow" = key, val}) do
{tag_name(key), String.to_integer(val)}
end

defp tag_map({"user-type" = key, val}) do
type =
case val do
Expand Down Expand Up @@ -276,6 +280,10 @@ defmodule TMI.IRC.Tags do
{tag_name(key), val == "1"}
end

defp tag_map({"r9k" = key, val}) do
{tag_name(key), val == "1"}
end

defp tag_map({"msg-param-prior-gifter-anonymous" = key, val}) do
{tag_name(key), val == "true"}
end
Expand Down
11 changes: 10 additions & 1 deletion test/support/data/irc/messages/emote_only_off.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[
{:unrecognized, "@msg-id=emote_only_off", %ExIRC.Message{server: [], nick: [], user: [], host: [], ctcp: false, cmd: "@msg-id=emote_only_off", args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is no longer in emote-only mode."]}}
{:unrecognized, "@msg-id=emote_only_off",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd: "@msg-id=emote_only_off",
args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is no longer in emote-only mode."]
}}
]
11 changes: 10 additions & 1 deletion test/support/data/irc/messages/emote_only_on.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[
{:unrecognized, "@msg-id=emote_only_on", %ExIRC.Message{server: [], nick: [], user: [], host: [], ctcp: false, cmd: "@msg-id=emote_only_on", args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is now in emote-only mode."]}}
{:unrecognized, "@msg-id=emote_only_on",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd: "@msg-id=emote_only_on",
args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is now in emote-only mode."]
}}
]
13 changes: 12 additions & 1 deletion test/support/data/irc/messages/msgemoteonly.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
[
{:unrecognized, "@msg-id=msg_emoteonly", %ExIRC.Message{server: [], nick: [], user: [], host: [], ctcp: false, cmd: "@msg-id=msg_emoteonly", args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is in emote only mode. You can find your currently available emoticons using the smiley in the chat text area."]}}
{:unrecognized, "@msg-id=msg_emoteonly",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd: "@msg-id=msg_emoteonly",
args: [
"tmi.twitch.tv NOTICE #spirodonfl :This room is in emote only mode. You can find your currently available emoticons using the smiley in the chat text area."
]
}}
]
14 changes: 14 additions & 0 deletions test/support/data/irc/messages/resub.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{:unrecognized,
"@badge-info=subscriber/2;badges=subscriber/0,sub-gifter/1;color=#00FF7F;display-name=NegZaxis;emotes=;flags=;id=64fe3ad1-083a-4fcc-a35b-6c612575f4e5;login=negzaxis;mod=0;msg-id=resub;msg-param-cumulative-months=2;msg-param-months=0;msg-param-multimonth-duration=0;msg-param-multimonth-tenure=0;msg-param-should-share-streak=0;msg-param-sub-plan-name=Download\\sA\\sPirate;msg-param-sub-plan=1000;msg-param-was-gifted=false;room-id=151368796;subscriber=1;system-msg=NegZaxis\\ssubscribed\\sat\\sTier\\s1.\\sThey've\\ssubscribed\\sfor\\s2\\smonths!;tmi-sent-ts=1705495897497;user-id=95193003;user-type=;vip=0",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd:
"@badge-info=subscriber/2;badges=subscriber/0,sub-gifter/1;color=#00FF7F;display-name=NegZaxis;emotes=;flags=;id=64fe3ad1-083a-4fcc-a35b-6c612575f4e5;login=negzaxis;mod=0;msg-id=resub;msg-param-cumulative-months=2;msg-param-months=0;msg-param-multimonth-duration=0;msg-param-multimonth-tenure=0;msg-param-should-share-streak=0;msg-param-sub-plan-name=Download\\sA\\sPirate;msg-param-sub-plan=1000;msg-param-was-gifted=false;room-id=151368796;subscriber=1;system-msg=NegZaxis\\ssubscribed\\sat\\sTier\\s1.\\sThey've\\ssubscribed\\sfor\\s2\\smonths!;tmi-sent-ts=1705495897497;user-id=95193003;user-type=;vip=0",
args: ["tmi.twitch.tv USERNOTICE #piratesoftware"]
}}
]
27 changes: 22 additions & 5 deletions test/tmi_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@ defmodule TMIExampleTest do

describe "emote_mode" do
test "emote_only_on" do
message = {:unrecognized, "@msg-id=emote_only_on", %ExIRC.Message{server: [], nick: [], user: [], host: [], ctcp: false, cmd: "@msg-id=emote_only_on", args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is now in emote-only mode."]}}
message =
{:unrecognized, "@msg-id=emote_only_on",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd: "@msg-id=emote_only_on",
args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is now in emote-only mode."]
}}

expected = %TMI.Events.EmoteMode{channel: "#spirodonfl", emote_only?: true}

assert TMI.parse_message(message) == expected
end

test "emote_only_off" do
message = {:unrecognized, "@msg-id=emote_only_off", %ExIRC.Message{server: [], nick: [], user: [], host: [], ctcp: false, cmd: "@msg-id=emote_only_off", args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is no longer in emote-only mode."]}}
message =
{:unrecognized, "@msg-id=emote_only_off",
%ExIRC.Message{
server: [],
nick: [],
user: [],
host: [],
ctcp: false,
cmd: "@msg-id=emote_only_off",
args: ["tmi.twitch.tv NOTICE #spirodonfl :This room is no longer in emote-only mode."]
}}

expected = %TMI.Events.EmoteMode{channel: "#spirodonfl", emote_only?: false}

Expand Down Expand Up @@ -203,9 +223,6 @@ defmodule TMIExampleTest do
months: 0,
plan: :t3,
plan_name: "Subscription (spirodonfl): Tier 3 Sub",
recipient_display_name: nil,
recipient_id: nil,
recipient_login: nil,
share_streak?: false,
system_message: "hmida74 subscribed at Tier 3.",
timestamp: ~U[2024-01-16 21:11:54.873Z],
Expand Down

0 comments on commit 7e48d86

Please sign in to comment.