diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7fd10c7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "mix_task", + "name": "mix (Default task)", + "request": "launch", + "projectDir": "${workspaceRoot}" + }, + { + "type": "mix_task", + "name": "mix test", + "request": "launch", + "task": "test", + "taskArgs": [ + "--trace" + ], + "startApps": true, + "projectDir": "${workspaceRoot}", + "requireFiles": [ + "test/**/test_helper.exs", + "test/**/*_test.exs" + ] + } + ] +} \ No newline at end of file diff --git a/lib/ircked_elixir/chat_group.ex b/lib/ircked_elixir/chat_group.ex index 9947a0f..57bacb9 100644 --- a/lib/ircked_elixir/chat_group.ex +++ b/lib/ircked_elixir/chat_group.ex @@ -42,6 +42,7 @@ defmodule IrckedElixir.ChatGroup do IO.puts(inspect pm) case pm.body do ".cunny" -> send_all(chatters, pm.to, "cunny!") + ".asskey" -> play(chatters, pm.to, File.read!("/home/rishi/awesome.txt") |> String.split("\n")) _ -> "" end @@ -55,12 +56,25 @@ defmodule IrckedElixir.ChatGroup do GenServer.start_link(__MODULE__, chatters, name: :chatgroup) end + def play(chatters, to, asskey) do + asskey + |> Stream.with_index |> Enum.to_list + |> Enum.each( + fn line -> + GenServer.call( + (@base_nick<>to_string(rem(elem(line, 1), @chatters)+1)) |> String.to_atom, + {:sendprivmsg, to, to_string(elem(line, 1))<>elem(line, 0)} + ) + end + ) + end + def send_all(chatters, to, body) do chatters |> Map.keys |> Enum.each( fn chatter -> - send(String.to_atom(chatter), {:sendprivmsg, to, body}) + GenServer.call(String.to_atom(chatter), {:sendprivmsg, to, body}) IO.puts("LOL, "<>inspect(chatter)) end ) diff --git a/lib/ircked_elixir/chatter.ex b/lib/ircked_elixir/chatter.ex index cf46937..779d59e 100644 --- a/lib/ircked_elixir/chatter.ex +++ b/lib/ircked_elixir/chatter.ex @@ -34,6 +34,13 @@ defmodule IrckedElixir.Chatter do {:noreply, state} end + @impl true + def handle_call({:sendprivmsg, to, body}, _, state) do + Privmsg.construct(state.nick, to, body).message |> Message.send(state.sock) + Process.sleep(10) + {:reply, :ok, state} + end + def start_link(starting_state) do GenServer.start_link(__MODULE__, starting_state, name: starting_state.nick |> String.to_atom) end