Skip to content

Commit 462e82f

Browse files
ROOMSTATE and NOTICE for emoteonly
1 parent 7afb717 commit 462e82f

5 files changed

Lines changed: 76 additions & 17 deletions

File tree

lib/tmi.ex

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ defmodule TMI do
217217
end
218218

219219
def default_handle_event(event, module) do
220-
Logger.debug("[#{bot_string(module)}] [#{event.channel}] EVENT\n#{inspect(event, pretty: true)}")
220+
Logger.debug("[#{bot_string(module)}] EVENT\n#{inspect(event, pretty: true)}")
221221
end
222222

223223
@doc """
@@ -284,6 +284,36 @@ defmodule TMI do
284284
%{channel: channel}
285285
end
286286

287+
@doc """
288+
Parse a NOTICE message.
289+
290+
## Example:
291+
292+
iex> args = "tmi.twitch.tv NOTICE #ryanwinchester_ :This room is no longer in emote-only mode."
293+
iex> notice_args_to_map(args)
294+
%{channel: "#ryanwinchester_"}
295+
296+
"""
297+
def notice_args_to_map(message) do
298+
[_server, notice] = :binary.split(message, " NOTICE ")
299+
[channel, _rest] = :binary.split(notice, " :")
300+
%{channel: channel}
301+
end
302+
303+
@doc """
304+
Parse a ROOMSTATE message.
305+
306+
## Example:
307+
308+
iex> roomstate_args_to_map("tmi.twitch.tv ROOMSTATE #ryanwinchester_")
309+
%{channel: "#ryanwinchester_"}
310+
311+
"""
312+
def roomstate_args_to_map(message) do
313+
[_server, channel] = :binary.split(message, " ROOMSTATE ")
314+
%{channel: channel}
315+
end
316+
287317
@doc """
288318
Parse Twitch tags, if they are enabled.
289319
@@ -304,17 +334,11 @@ defmodule TMI do
304334
# Convert the ExIRC message to bot message.
305335
# ----------------------------------------------------------------------------
306336

307-
# def parse_message(message) do
308-
# ed
309-
310-
## WITH tags
311-
312337
# TODO: I think I will want to recursively parse the message args until I get
313338
# one of the `PRIVMSG`, `USERNOTICE`, etc. messages.
314339
# As it is now, if we get a message or system_msg or anything that has this
315340
# text, we could get unexpected results since `String.contains?/2` could match.
316-
@doc false
317-
def apply_incoming_to_bot({:unrecognized, tag_string, %ExIRC.Message{args: [arg]}}, bot) do
341+
def parse_message({:unrecognized, tag_string, %ExIRC.Message{args: [arg]} = msg}) do
318342
cond do
319343
String.contains?(arg, "PRIVMSG") ->
320344
case message_args_to_map(arg) do
@@ -325,34 +349,51 @@ defmodule TMI do
325349
params
326350
| message: String.trim_trailing(message, <<0x01>>)
327351
})
328-
|> bot.handle_event()
329352

330353
params ->
331354
tag_string
332355
|> TMI.IRC.Tags.parse!()
333356
|> TMI.Event.from_map_with_name(:message, params)
334-
|> bot.handle_event()
335357
end
336358

337359
String.contains?(arg, "USERNOTICE") ->
338360
tag_string
339361
|> TMI.IRC.Tags.parse!()
340362
|> TMI.Event.from_map(usernotice_args_to_map(arg))
341-
|> bot.handle_event()
363+
364+
String.contains?(arg, "NOTICE") ->
365+
tag_string
366+
|> TMI.IRC.Tags.parse!()
367+
|> TMI.Event.from_map(notice_args_to_map(arg))
368+
369+
String.contains?(arg, "ROOMSTATE") ->
370+
tag_string
371+
|> TMI.IRC.Tags.parse!()
372+
|> TMI.Event.from_map_with_name(:channel_update, roomstate_args_to_map(arg))
342373

343374
String.contains?(arg, "WHISPER") ->
344375
tag_string
345376
|> TMI.IRC.Tags.parse!()
346377
|> TMI.Event.from_map_with_name(:whisper, whisper_args_to_map(arg))
347-
|> bot.handle_event()
348378

349379
true ->
350380
tag_string
351381
|> TMI.IRC.Tags.parse!()
352-
|> bot.handle_unrecognized(arg)
382+
|> TMI.Event.from_map_with_name(:unrecognized, %{msg: msg})
353383
end
354384
end
355385

386+
def parse_message({:unrecognized, _cmd, %ExIRC.Message{} = msg}) do
387+
TMI.Event.from_map_with_name(%{}, :unrecognized, %{msg: msg})
388+
end
389+
390+
## WITH tags
391+
392+
@doc false
393+
def apply_incoming_to_bot({:unrecognized, _tag_string, %ExIRC.Message{}} = msg, bot) do
394+
parse_message(msg) |> bot.handle_event()
395+
end
396+
356397
## Without tags
357398

358399
def apply_incoming_to_bot({:connected, server, port}, bot) do

lib/tmi/events/channel_update.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
defmodule TMI.Events.ChannelUpdate do
2-
@moduledoc false
3-
use TMI.Event, fields: []
2+
@moduledoc """
3+
When we get a ROOMSTATE notice in IRC or a chat settings update from EventSub.
4+
"""
5+
use TMI.Event, fields: [
6+
# TODO: Follower-only, sub-only, slow-mode, unique (is that r9k?)?
7+
:emote_only?,
8+
:channel_id,
9+
:channel
10+
]
411
end

lib/tmi/events/emote_mode.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule TMI.Events.EmoteMode do
22
@moduledoc false
33
use TMI.Event, fields: [
4+
:channel,
45
:emote_only?
56
]
67
end

lib/tmi/irc/parsing/event_mapping.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ defmodule TMI.IRC.Parsing.EventMapping do
7171
# "" => :user_auth_grant,
7272
# "" => :user_auth_revoke,
7373
# "" => :user_update,
74-
"viewermilestone" => :viewer_milestone
74+
"viewermilestone" => :viewer_milestone,
75+
# WTF ಠ_ಠ
76+
"msg_emoteonly" => :unrecognized
7577
}
7678

7779
@doc """

test/tmi_example_test.exs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ defmodule TMIExampleTest do
55
test "emote_only_on" do
66
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."]}}
77

8-
expected = %TMI.Events.EmoteMode{}
8+
expected = %TMI.Events.EmoteMode{channel: "#spirodonfl", emote_only?: true}
9+
10+
assert TMI.parse_message(message) == expected
11+
end
12+
13+
test "emote_only_off" do
14+
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."]}}
15+
16+
expected = %TMI.Events.EmoteMode{channel: "#spirodonfl", emote_only?: false}
917

1018
assert TMI.parse_message(message) == expected
1119
end

0 commit comments

Comments
 (0)