Skip to content

Commit

Permalink
Merge branch 'master' into elixir-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
rockerBOO committed Jun 29, 2020
2 parents 4ebac14 + e57776b commit ee18d1e
Show file tree
Hide file tree
Showing 31 changed files with 621 additions and 733 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
78 changes: 54 additions & 24 deletions lib/blur.ex
Original file line number Diff line number Diff line change
@@ -1,42 +1,72 @@
defmodule Blur do
@moduledoc """
Wrapper for client functions
Short to sent to a random channel, Ideally just 1.
say(client, "Hello!")
Access your bot through Blur
"""

require Logger

@doc """
Join a channel.
## Examples
iex> Blur.join "#channel"
:ok
"""
@spec join(channel :: binary) :: :ok | {:error, atom}
def join(channel),
do: join(:irc_client, channel)

require Logger
@doc """
Join a channel on the client IRC connection.
## Examples
iex> Blur.join client, "#channel"
:ok
"""
@spec join(pid | atom, binary) :: :ok | {:error, atom}
def join(client, "#" <> _ = channel),
do: Blur.IRC.join(client, channel)

# To say to the client irc connection
def say(client, msg),
do: client |> Blur.IRC.say(msg)
def join(client, channel),
do: join(client, "#" <> channel)

@doc """
Leave a channel on the client IRC connection.
# To say to a RANDOM irc connection
def say(msg),
do: Blur.IRC.say(msg)
## Examples
iex> Blur.leave client, "#channel"
:ok
"""
@spec leave(client :: pid, channel :: binary) :: :ok | {:error, atom}
def leave(client, channel),
do: Blur.IRC.part(client, channel)

# To say to a channel '#rockerboo'
def join("#" <> _ = channel),
do: Blur.IRC.join(channel)
@doc """
Leave a channel.
# To join a channel on a RANDOM irc connection
def join(channel),
do: join("#" <> channel)
## Examples
iex> Blur.leave "#channel"
:ok
"""
@spec leave(channel :: binary) :: :ok | {:error, atom}
def leave(channel),
do: leave(:irc_client, channel)

# To join a channel on the client irc connection
def join(client, "#" <> _ = channel),
do: client |> Blur.IRC.join(channel)
@doc """
Say a message to the channel.
# Join a channel on the client irc connection
def join(client, channel),
do: join(client, "#" <> channel)
## Examples
iex> Blur.say "#channel", "a message to the channel"
:ok
"""
@spec say(channel :: charlist, msg :: charlist) :: :ok | {:error, atom}
def say(channel, msg),
do: Blur.IRC.say(:irc_client, channel, msg)

# Get token from the Environmental Variables
@doc """
Get token from the environmental variables.
"""
@spec token() :: nil | binary
def token do
case Blur.Env.fetch!(:access_token) do
nil -> Blur.Env.fetch!(:chat_key)
Expand Down
129 changes: 95 additions & 34 deletions lib/channel.ex
Original file line number Diff line number Diff line change
@@ -1,90 +1,151 @@
defmodule Blur.Channel do
@moduledoc """
Channel module
users(channel)
add_user(channel, user)
channel_to_atom("#channel")
has_config?(channel)
commands(channel)
aliases(channel)
"""

use GenServer
require Logger
alias Blur.Channel.Config

defmodule State do
defstruct channel: "",
client: nil,
config?: nil,
users: []
end

defmodule Error do
@moduledoc """
Channel errors
"""
defexception [:message]
end

def start_link("#" <> _ = channel) do
GenServer.start_link(__MODULE__,
[channel],
[name: channel_to_atom(channel)]
@spec to_atom(channel :: binary) :: atom
def to_atom("#" <> channel), do: String.to_atom(channel)
def to_atom(channel), do: to_atom("#" <> channel)

@doc """
Start channel store
"""
@spec start_link(client :: pid, channel :: binary) :: GenServer.on_start()
def start_link(client, "#" <> _ = channel) do
GenServer.start_link(
__MODULE__,
[client, channel]
)
end

def start_link(channel), do: start_link("#" <> channel)
def start_link(client, channel), do: start_link(client, "#" <> channel)

def init([channel]) do
config? = channel |> Config.load_channel_config
@impl true
@spec init(list) :: {:ok, %State{}}
def init([client, channel]) do
config? = channel |> Config.load_channel_config()

{:ok, users} = Blur.Channel.Users.start_link(channel)

{:ok, %{channel: channel, config?: config?, users: users}}
{:ok, %State{channel: channel, client: client, config?: config?, users: users}}
end

@doc """
Add user to channel
## Examples
iex> Blur.Channel.add_user "#rockerboo", "rockerBOO"
:ok
"""
@spec add_user(channel :: binary, user :: binary) :: :ok
def add_user(channel, user) do
atom = channel_to_atom(channel)
atom
|> GenServer.cast({:add_user, user})
to_atom(channel) |> GenServer.cast({:add_user, user})
end

@doc """
Remove user from the channel
## Examples
iex> Blur.Channel.remove_user "#rockerboo", "rockerBOO"
:ok
"""
@spec remove_user(channel :: binary, user :: binary) :: :ok
def remove_user(channel, user) do
atom = channel_to_atom(channel)
atom |> GenServer.cast({:remove_user, user})
to_atom(channel) |> GenServer.cast({:remove_user, user})
end

@doc """
Get users in a channel
## Examples
iex> Blur.Channel.users "#rockerboo"
["rockerBOO"]
"""
@spec users(channel :: binary) :: :ok
def users(channel) do
atom = channel_to_atom(channel)
atom |> GenServer.call(:users)
to_atom(channel) |> GenServer.call(:users)
end

def channel_to_atom("#" <> channel), do: String.to_atom(channel)
def channel_to_atom(channel), do: channel_to_atom("#" <> channel)
@doc """
Get config for a channel
## Examples
iex> Blur.Channel.config? "#rockerboo"
%{}
"""
@spec config?(channel :: binary) :: nil | map
def config?(channel) do
atom = channel_to_atom(channel)
atom |> GenServer.call(:config?)
to_atom(channel) |> GenServer.call(:config?)
end

@doc """
Get commands for a channel
## Examples
iex> Blur.Channel.commands "#rockerboo"
%{}
"""
@spec commands(channel :: binary) :: list
def commands(channel) do
atom = channel_to_atom(channel)
atom |> GenServer.call(:commands)
to_atom(channel) |> GenServer.call(:commands)
end

@doc """
Get aliases for a channel
## Examples
iex> Blur.Channel.aliases "#rockerboo"
%{}
"""
@spec aliases(channel :: binary) :: list
def aliases(channel) do
atom = channel_to_atom(channel)
atom |> GenServer.call(:aliases)
to_atom(channel) |> GenServer.call(:aliases)
end

@doc """
Handle channel info
"""
@impl true
@spec handle_call(:aliases, pid, %State{}) :: {:reply, map, %State{}}
def handle_call(:aliases, _from, state),
do: {:reply, Config.get("#{state.channel}-aliases"), state}
do: {:reply, Config.get("#{state.channel}-aliases"), state}

@spec handle_call(:commands, pid, %State{}) :: {:reply, map, %State{}}
def handle_call(:commands, _from, state),
do: {:reply, Config.get("#{state.channel}-commands"), state}
do: {:reply, Config.get("#{state.channel}-commands"), state}

@spec handle_call(:config?, pid, %State{}) :: {:reply, nil | map, %State{}}
def handle_call(:config?, _from, state),
do: {:reply, state.config?, state}
do: {:reply, state.config?, state}

@spec handle_call(:users, pid, %State{}) :: {:reply, list, %State{}}
def handle_call(:users, _from, state) do
{:reply, Blur.Channel.Users.list(state.channel), state}
end

@doc """
Handle channel users
"""
@impl true
@spec handle_cast({:add_user, user :: binary}, %State{}) :: {:reply, list, %State{}}
def handle_cast({:add_user, user}, state) do
Blur.Channel.Users.add(state.channel, user)

Expand Down
10 changes: 5 additions & 5 deletions lib/channel_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ defmodule Blur.Channel.Config do

# check if commands were loaded for channel
if config_loaded?(channel) do
Logger.info "Loaded config for #{channel}"
Logger.info("Loaded config for #{channel}")
true
else
false
end
end

def load(_channel, []), do: :ok

def load(channel, [type | remaining]) do
channel |> load_from_json(type) |> save(channel, type)

load(channel, remaining)
end

def load_from_json("#" <> channel, type) do
case File.read "data/#{channel}/#{type}.json" do
{:ok, body} -> body |> Poison.decode!()
case File.read("data/#{channel}/#{type}.json") do
{:ok, body} -> body |> Poison.decode!()
{:error, :enoent} -> nil
end
end
Expand All @@ -51,7 +52,6 @@ defmodule Blur.Channel.Config do
end

def config_loaded?(channel) do
cache = ConCache.ets(:channels_config)
cache |> :ets.member("#{channel}-config")
ConCache.ets(:channels_config) |> :ets.member("#{channel}-config")
end
end
13 changes: 2 additions & 11 deletions lib/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ defmodule Blur.Command do

require Logger

def run(nil, _, _), do: nil
def run(command, user, channel) do
case command do
["say", message] -> Blur.say(channel, message)
["cmd", "song"] ->
{artist, _, title} =
Blur.Lastfm.get_last_played()
Blur.say(channel, "#{artist} - #{title}")
["cmd", cmd] -> IO.inspect cmd
_ -> nil
end
def run(_msg, _user, _channel) do
:ok
end
end
28 changes: 0 additions & 28 deletions lib/extensions/lastfm/lastfm.ex

This file was deleted.

11 changes: 0 additions & 11 deletions lib/extensions/points/database.ex

This file was deleted.

Loading

0 comments on commit ee18d1e

Please sign in to comment.