Skip to content

Commit

Permalink
Simplified the process flow. Now passing variables to the application…
Browse files Browse the repository at this point in the history
… start.
  • Loading branch information
rockerBOO committed Jul 13, 2020
1 parent a526041 commit 543997a
Show file tree
Hide file tree
Showing 26 changed files with 300 additions and 269 deletions.
75 changes: 16 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,36 @@
Blur
====

Chat Bot for Streamers. Built to scale. Fast, efficient processing.
Twitch Chat Bot

![Elixir CI](https://github.com/rockerBOO/blur/workflows/Elixir%20CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/rockerBOO/blur/badge.svg)](https://coveralls.io/github/rockerBOO/blur)

## Install

To setup, the configuration options are in `.env`. `.env.example` is setup to show how to configure, and just rename to `.env`.
First, add Blur to your mix.exs dependencies:

```elixir
# The key generated from twitchtmi.com/chat
export TWITCH_CHAT_KEY=oauth:

# Username on Twitch for the Bot.
# It needs to match to the user of the access token/chat key.
export TWITCH_USERNAME=

# OPTIONAL. This takes precidence over the chat key for more intergrated options (authenticated calls to twitch)
# export TWITCH_ACCESS_TOKEN=
def deps do
[{:blur, "~> 0.2"}]
end
```

## To Run

`source .env && iex -S mix`

## Configuration for Channels
Then, update your dependencies:

Configuration for channels are stored in `data/#channel/`
```sh-session
$ mix deps.get
```

* `data/rockerboo/commands.json`
* Commands list
* `data/rockerboo/config.json`
* Configuration options for the channel
* `data/rockerboo/aliases.json`
* Stores aliases for commmands
To setup, the configuration options are in `.env`. `.env.example` is setup to show how to configure, and just rename to `.env`.

### `data/#channel/config.json`
```js
{
"messages": "on"
}
```
```elixir
# The key generated from https://twitchapps.com/tmi/.
export TWITCH_CHAT_KEY=oauth:

### `data/#channel/commands.json`
```js
{
"hello": ["say", "Hello"],
"help": ["say", "You need help"],
"follower": ["cmd", "follower"],
"followed": ["cmd", "followed"],
"flip": ["say", "(╯°□°)╯︵┻━┻"],
"song": ["cmd", "song"],
"highlight": ["cmd", "highlight"]
}
# Username on Twitch for the Bot.
# It needs to match to the user of the access token/chat key.
export TWITCH_USERNAME=
```


### `data/#channel/aliases.json`
```js
{
"bealight": "bealright",
"bot": "elirc",
"glacier": "theme",
"xfile": "xfiles",
"x-file": "xfiles",
"x-files": "xfiles",
"h": "help",
"playlist": "lastfm",
"coming": "getsmeeverytime",
"63": "speedlimit",
"soundslist": "soundlist",
"sounds": "soundlist",
"table": "flip",
"tableflip": "flip"
}
```
12 changes: 0 additions & 12 deletions config/config.exs

This file was deleted.

3 changes: 0 additions & 3 deletions config/dev.exs

This file was deleted.

3 changes: 0 additions & 3 deletions config/test.exs

This file was deleted.

29 changes: 29 additions & 0 deletions lib/app.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Blur.App do
use Supervisor

def start! do
Supervisor.start_link(__MODULE__, [], name: :blur)
end

@spec start(atom, list) :: GenServer.on_start()
def start(_type, [user, channels]) do
Supervisor.start_link(__MODULE__, [user, channels], name: :blur)
end

@impl Supervisor
def init([user, channels]) do
{:ok, client} = ExIRC.start_link!()

children = [
{Blur.Channels, []},
{Blur.Users, []},
# {Blur.IRC.Client, [client]},
{Blur.IRC.Connection, [client, user]},
{Blur.IRC.Login, [client, channels]},
{Blur.IRC.Channel, [client]},
{Blur.IRC.Message, [client]}
]

Supervisor.init(children, strategy: :one_for_one)
end
end
21 changes: 21 additions & 0 deletions lib/blur.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
defmodule Blur do
@moduledoc """
Access your bot through Blur
Client
Handlers
- Login
on_logon -> join_channels
- Connection
%ConnState{}
on_connection -> login
on_disconnection -> logoff
- Message
pool
- Channels
on_join -> add_channel
[Channel, Channel]
- Users
on_names -> add_users
on_user_join -> add_user
on_part -> remove_user
[User, User]
"""

require Logger
Expand Down Expand Up @@ -36,6 +56,7 @@ defmodule Blur do
Say a message to the channel.
## Examples
Blur.say :twitch, "adattape"
"""
def say(client, channel, message),
Expand Down
17 changes: 3 additions & 14 deletions lib/handlers/IRC/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule Blur.IRC.Channel do
require Logger
alias ExIRC.Client

@spec start_link(client :: pid) :: GenServer.on_start()
def start_link(client) do
@spec start_link([client :: pid]) :: GenServer.on_start()
def start_link([client]) do
GenServer.start_link(__MODULE__, client)
end

Expand Down Expand Up @@ -39,24 +39,13 @@ defmodule Blur.IRC.Channel do
{:noreply, client}
end

def handle_info({:logon, _, _nick, _, _}, state) do
{:noreply, state}
end

def handle_info({:kicked, sender, channel}, state) do
by = sender.nick
Logger.debug("We were kicked from #{channel} by #{by}")
{:noreply, state}
end

# Drops unknown messages
def handle_info(_msg, state) do
{:noreply, state}
end

@impl true
def terminate(reason, _state) do
IO.inspect(reason)
def terminate(_reason, _state) do
:ok
end
end
30 changes: 17 additions & 13 deletions lib/handlers/IRC/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ defmodule Blur.IRC.Connection do
client: nil
end

@spec start_link(client :: GenServer.server()) :: GenServer.on_start()
def start_link(client) do
GenServer.start_link(__MODULE__, %State{client: client})
@spec start_link(list) :: GenServer.on_start()
def start_link([client, user]) do
GenServer.start_link(__MODULE__, %State{client: client, user: user, nick: user, name: user})
end

@impl GenServer
@impl true
@spec init(%State{}) :: {:ok, %State{}}
def init(state) do
ExIRC.Client.add_handler(state.client, self())
Expand All @@ -34,7 +34,18 @@ defmodule Blur.IRC.Connection do
{:ok, state}
end

@impl GenServer
def login(client, nick, token) do
# Login to IRC
case Blur.IRC.login(client, nick, token) do
{:error, :not_logged_in} ->
Logger.info("Logging in with #{nick} failed.")

:ok ->
:ok
end
end

@impl true
@spec handle_info(
{:connected, server :: binary, port :: non_neg_integer()}
| {:disconnected}
Expand All @@ -44,14 +55,7 @@ defmodule Blur.IRC.Connection do
def handle_info({:connected, server, port}, state) do
Logger.debug("Connected to #{server}:#{port}")

nick = Blur.Env.fetch!(:username)

# Login to IRC
:ok = Blur.IRC.login(state.client, nick, Blur.token())

# Request Twitch Capabilities (tags)
:ok = Blur.IRC.request_twitch_capabilities(state.client)

:ok = login(state.client, state.user, Blur.token())
{:noreply, state}
end

Expand Down
40 changes: 13 additions & 27 deletions lib/handlers/IRC/irc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Blur.IRC do
Request the CAP (capability) on the server
## Example
iex> Blur.IRC.cap_request client, ':twitch.tv/membership'
Blur.IRC.cap_request client, ':twitch.tv/membership'
:ok
"""
@spec cap_request(client :: pid | atom, cap :: binary) :: :ok | {:error, atom}
Expand All @@ -23,7 +23,7 @@ defmodule Blur.IRC do
Request twitch for capabilities
## Example
iex> Blur.IRC.request_twitch_capabilities client
Blur.IRC.request_twitch_capabilities client
:ok
"""
@spec request_twitch_capabilities(client :: pid | atom) :: :ok | {:error, atom}
Expand Down Expand Up @@ -51,32 +51,18 @@ defmodule Blur.IRC do
Connect to the IRC server.
## Example
iex> Blur.IRC.connect client, "irc.twitch.tv", 6667
Blur.IRC.connect client, "irc.twitch.tv", 6667
:ok
"""
@spec connect!(client :: pid | atom, host :: binary, port :: non_neg_integer) :: :ok
def connect!(client, host, port),
do: ExIRC.Client.connect!(client, host, port)

@doc """
Connect to the default IRC server.
## Example
iex> Blur.IRC.connect client
:ok
"""
@spec connect!(client :: pid | atom) :: :ok
def connect!(client) do
%{host: host, port: port} = %Blur.IRC.Connection.State{}

ExIRC.Client.connect!(client, host, port)
end

@doc """
Login to the server.
## Example
iex> Blur.IRC.login client, "rockerBOO", "oauth:oauthhashherewithlettersandnumbers"
Blur.IRC.login client, "rockerBOO", "oauth:oauthhashherewithlettersandnumbers"
:ok
"""
@spec login(client :: pid | atom, nick :: binary, pass :: binary) ::
Expand All @@ -87,7 +73,7 @@ defmodule Blur.IRC do
@doc """
Join many channels.
## Example
iex> Blur.IRC.join_many client, ["#rockerboo", "#adattape"]
Blur.IRC.join_many client, ["#rockerboo", "#adattape"]
:ok
"""
@spec join_many(client :: pid | atom | atom, list) :: :ok | {:error, atom}
Expand All @@ -99,12 +85,12 @@ defmodule Blur.IRC do
Join an IRC channel.
## Example
iex> Blur.IRC.join client, "#rockerboo"
Blur.IRC.join client, "#rockerboo"
:ok
"""
@spec join(client :: pid | atom, channel :: binary) :: :ok | {:error, atom}
def join(client, "#" <> _ = channel) do
Logger.debug("Join #{channel}...")
Logger.debug("Join #{channel}")
ExIRC.Client.join(client, channel)
end

Expand All @@ -115,12 +101,12 @@ defmodule Blur.IRC do
Part from IRC channel.
## Example
iex> Blur.IRC.part client, "#rockerboo"
Blur.IRC.part client, "#rockerboo"
:ok
"""
@spec part(client :: pid | atom, channel :: binary) :: :ok | {:error, atom}
def part(client, "#" <> _ = channel) do
Logger.debug("Part #{channel}...")
Logger.debug("Part #{channel}")
ExIRC.Client.part(client, channel)
end

Expand All @@ -131,7 +117,7 @@ defmodule Blur.IRC do
Send a message to the channel
## Example
iex> Blur.IRC.say client, "#rockerboo", "Hello"
Blur.IRC.say client, "#rockerboo", "Hello"
:ok
"""
@spec say(client :: pid | atom, channel :: binary, msg :: binary) :: :ok | {:error, atom}
Expand All @@ -143,7 +129,7 @@ defmodule Blur.IRC do
Quit the IRC server.
## Example
iex> Blur.IRC.quit client, "Goodbye!"
Blur.IRC.quit client, "Goodbye!"
:ok
"""
@spec quit(client :: pid | atom, msg :: nil | binary) :: :ok | {:error, atom}
Expand All @@ -155,7 +141,7 @@ defmodule Blur.IRC do
Quit the IRC server with no message.
## Example
iex> Blur.IRC.quit client
Blur.IRC.quit client
:ok
"""
@spec quit(client :: pid | atom) :: :ok | {:error, atom}
Expand All @@ -167,7 +153,7 @@ defmodule Blur.IRC do
Stop the IRC client process
## Example
iex> Blur.IRC.stop! client
Blur.IRC.stop! client
{:stop, :normal, :ok, %ExIRC.Client{}}
"""
@spec stop!(client :: pid | atom) :: {:stop, :normal, :ok, %ExIRC.Client.ClientState{}}
Expand Down
Loading

0 comments on commit 543997a

Please sign in to comment.