Skip to content

Commit

Permalink
FEAT: mix format & CI setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarathustra2 committed Jan 19, 2021
1 parent 6cd46d1 commit e56bbe9
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 143 deletions.
5 changes: 5 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
import_deps: [],
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: []
]
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI
on: [pull_request, push]
jobs:
mix_test:
name: mix test (Elixir ${{ matrix.elixir }} OTP ${{ matrix.otp }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- elixir: 1.10.4
otp: 21.3.8.16
- elixir: 1.10.4
otp: 23.0.3
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}
- name: Install Dependencies
run: |
mix local.hex --force
mix local.rebar --force
mix deps.get --only test
- name: Check formatting
run: mix format --check-formatted
- name: Run tests
run: mix test
6 changes: 4 additions & 2 deletions lib/Discord/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Alchemy.Discord.Events do
with {:ok, guild_id} <- Channels.lookup(channel_id) do
Guilds.update_channel(guild_id, channel)
end

{:channel_update, [Channel.from_map(channel)]}
end

Expand All @@ -40,6 +41,7 @@ defmodule Alchemy.Discord.Events do
with {:ok, guild_id} <- Channels.lookup(channel_id) do
Guilds.remove_channel(guild_id, channel_id)
end

Channels.remove_channel(channel_id)
{:channel_delete, [Channel.from_map(channel)]}
end
Expand Down Expand Up @@ -108,10 +110,10 @@ defmodule Alchemy.Discord.Events do

def handle("GUILD_ROLE_UPDATE", %{"guild_id" => id, "role" => new_role = %{"id" => role_id}}) do
guild_result = Guilds.safe_call(id, {:section, "roles"})

old_role =
with {:ok, guild} <- guild_result,
role when not is_nil(role) <- guild[role_id]
do
role when not is_nil(role) <- guild[role_id] do
to_struct(role, Role)
else
_ -> nil
Expand Down
2 changes: 1 addition & 1 deletion lib/Structs/Channels/store_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Alchemy.Channel.StoreChannel do

# Note: should never encounter a store channel, as they're not something
# bots can send/read to. It's "the store."

defstruct [
:id,
:guild_id,
Expand Down
4 changes: 1 addition & 3 deletions lib/Structs/Messages/Reactions/emoji.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ defmodule Alchemy.Reaction.Emoji do
@moduledoc false

@derive Poison.Encoder
defstruct [:id,
:name]

defstruct [:id, :name]

@doc false
def resolve(emoji) do
Expand Down
4 changes: 1 addition & 3 deletions lib/Structs/Messages/Reactions/reaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ defmodule Alchemy.Reaction do
@moduledoc false

@derive Poison.Encoder
defstruct [:count,
:me,
:emoji]
defstruct [:count, :me, :emoji]
end
84 changes: 47 additions & 37 deletions lib/Structs/Voice/voice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Alchemy.Voice do
alias Alchemy.Voice.Supervisor, as: VoiceSuper
alias Alchemy.Discord.Gateway.RateLimiter

@type snowflake :: String.t
@type snowflake :: String.t()

@typedoc """
Represents a voice region.
Expand All @@ -40,15 +40,15 @@ defmodule Alchemy.Voice do
Whether this is a custom voice region.
"""
@type region :: %VoiceRegion{
id: snowflake,
name: String.t,
sample_hostname: String.t,
sample_port: Integer,
vip: Boolean,
optimal: Boolean,
deprecated: Boolean,
custom: Boolean
}
id: snowflake,
name: String.t(),
sample_hostname: String.t(),
sample_port: Integer,
vip: Boolean,
optimal: Boolean,
deprecated: Boolean,
custom: Boolean
}
@typedoc """
Represents the state of a user's voice connection.
Expand All @@ -72,16 +72,16 @@ defmodule Alchemy.Voice do
Whether this user is muted by the current user.
"""
@type state :: %VoiceState{
guild_id: snowflake | nil,
channel_id: snowflake,
user_id: snowflake,
session_id: String.t,
deaf: Boolean,
mute: Boolean,
self_deaf: Boolean,
self_mute: Boolean,
suppress: Boolean
}
guild_id: snowflake | nil,
channel_id: snowflake,
user_id: snowflake,
session_id: String.t(),
deaf: Boolean,
mute: Boolean,
self_deaf: Boolean,
self_mute: Boolean,
suppress: Boolean
}
@typedoc """
Represents the audio options that can be passed to different play methods.
Expand All @@ -103,28 +103,31 @@ defmodule Alchemy.Voice do
The timeout will be spread across 2 different message receptions,
i.e. a timeout of `6000` will only wait 3s at every reception.
"""
@spec join(snowflake, snowflake, integer) :: :ok | {:error, String.t}
@spec join(snowflake, snowflake, integer) :: :ok | {:error, String.t()}
def join(guild, channel, timeout \\ 6000) do
case Registry.lookup(Registry.Voice, {guild, :gateway}) do
[{_, ^channel}|_] -> :ok
[{_, ^channel} | _] -> :ok
_ -> VoiceSuper.start_client(guild, channel, timeout)
end
end

@doc """
Disconnects from voice in a guild.
Returns an error if the connection hadn’t been established.
"""
@spec leave(snowflake) :: :ok | {:error, String.t}
@spec leave(snowflake) :: :ok | {:error, String.t()}
def leave(guild) do
case Registry.lookup(Registry.Voice, {guild, :gateway}) do
[] ->
{:error, "You're not joined to voice in this guild"}
[{pid, _}|_] ->

[{pid, _} | _] ->
Supervisor.terminate_child(VoiceSuper.Gateway, pid)
RateLimiter.change_voice_state(guild, nil)
end
end

@doc """
Starts playing a music file on a guild's voice connection.
Expand All @@ -137,11 +140,10 @@ defmodule Alchemy.Voice do
Voice.play_file("666", "cool_song.mp3")
```
"""
@spec play_file(snowflake, Path.t, audio_options) :: :ok | {:error, String.t}
@spec play_file(snowflake, Path.t(), audio_options) :: :ok | {:error, String.t()}
def play_file(guild, file_path, options \\ []) do
with [{pid, _}|_] <- Registry.lookup(Registry.Voice, {guild, :controller}),
true <- File.exists?(file_path)
do
with [{pid, _} | _] <- Registry.lookup(Registry.Voice, {guild, :controller}),
true <- File.exists?(file_path) do
GenServer.call(pid, {:play, file_path, :file, options})
else
[] -> {:error, "You're not joined to voice in this guild"}
Expand All @@ -152,9 +154,10 @@ defmodule Alchemy.Voice do
defp play_type(guild, type, data, options) do
case Registry.lookup(Registry.Voice, {guild, :controller}) do
[] -> {:error, "You're not joined to voice in this guild"}
[{pid, _}|_] -> GenServer.call(pid, {:play, data, type, options})
[{pid, _} | _] -> GenServer.call(pid, {:play, data, type, options})
end
end

@doc """
Starts playing audio from a url.
Expand All @@ -163,32 +166,36 @@ defmodule Alchemy.Voice do
This function does not check the validity of this url, so if it's invalid,
an error will get logged, and no audio will be played.
"""
@spec play_url(snowflake, String.t, audio_options) :: :ok | {:error, String.t}
@spec play_url(snowflake, String.t(), audio_options) :: :ok | {:error, String.t()}
def play_url(guild, url, options \\ []) do
play_type(guild, :url, url, options)
end

@doc """
Starts playing audio from an `iodata`, or a stream of `iodata`.
Similar to `play_url/2` except it doesn't create a stream from
`youtube-dl` for you.
"""
@spec play_iodata(snowflake, iodata | Enumerable.t, audio_options) :: :ok | {:error, String.t}
@spec play_iodata(snowflake, iodata | Enumerable.t(), audio_options) ::
:ok | {:error, String.t()}
def play_iodata(guild, data, options \\ []) do
play_type(guild, :iodata, data, options)
end

@doc """
Stops playing audio on a guild's voice connection.
Returns an error if the connection hadn't been established.
"""
@spec stop_audio(snowflake) :: :ok | {:error, String.t}
@spec stop_audio(snowflake) :: :ok | {:error, String.t()}
def stop_audio(guild) do
case Registry.lookup(Registry.Voice, {guild, :controller}) do
[] -> {:error, "You're not joined to voice in this guild"}
[{pid, _}|_] -> GenServer.call(pid, :stop_playing)
[{pid, _} | _] -> GenServer.call(pid, :stop_playing)
end
end

@doc """
Lets this process listen for the end of an audio track in a guild.
Expand All @@ -214,13 +221,14 @@ defmodule Alchemy.Voice do
end
```
"""
@spec listen_for_end(snowflake) :: :ok | {:error, String.t}
@spec listen_for_end(snowflake) :: :ok | {:error, String.t()}
def listen_for_end(guild) do
case Registry.lookup(Registry.Voice, {guild, :controller}) do
[] -> {:error, "You're not joined to voice in this guild"}
[{pid, _}|_] -> GenServer.call(pid, :add_listener)
[{pid, _} | _] -> GenServer.call(pid, :add_listener)
end
end

@doc """
Blocks the current process until audio has stopped playing in a guild.
Expand All @@ -236,15 +244,17 @@ defmodule Alchemy.Voice do
end
```
"""
@spec wait_for_end(snowflake, integer | :infinity) :: :ok | {:error, String.t}
@spec wait_for_end(snowflake, integer | :infinity) :: :ok | {:error, String.t()}
def wait_for_end(guild, timeout \\ :infinity) do
listen_for_end(guild)

receive do
{:audio_stopped, ^guild} -> :ok
after
timeout -> {:error, "Timed out waiting for audio"}
end
end

@doc """
Returns which channel the client is connected to in a guild.
Expand All @@ -253,7 +263,7 @@ defmodule Alchemy.Voice do
@spec which_channel(snowflake) :: snowflake | nil
def which_channel(guild) do
case Registry.lookup(Registry.Voice, {guild, :gateway}) do
[{_, channel}|_] -> channel
[{_, channel} | _] -> channel
_ -> nil
end
end
Expand Down
9 changes: 1 addition & 8 deletions lib/Structs/Voice/voice_region.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@ defmodule Alchemy.VoiceRegion do
@moduledoc false

@derive Poison.Encoder
defstruct [:id,
:name,
:sample_hostname,
:sample_port,
:vip,
:optimal,
:deprecated,
:custom]
defstruct [:id, :name, :sample_hostname, :sample_port, :vip, :optimal, :deprecated, :custom]
end
20 changes: 11 additions & 9 deletions lib/Structs/Voice/voice_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ defmodule Alchemy.VoiceState do
@moduledoc false

@derive Poison.Encoder
defstruct [:guild_id,
:channel_id,
:user_id,
:session_id,
:deaf,
:mute,
:self_deaf,
:self_mute,
:suppress]
defstruct [
:guild_id,
:channel_id,
:user_id,
:session_id,
:deaf,
:mute,
:self_deaf,
:self_mute,
:suppress
]
end
6 changes: 4 additions & 2 deletions lib/Structs/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ defmodule Alchemy.Permissions do
@spec to_bitset([permission]) :: Integer
def to_bitset(list) do
@perm_map
|> Enum.reduce(0,
|> Enum.reduce(
0,
fn {k, v}, acc ->
if Enum.member?(list, k), do: acc ||| v, else: acc
end)
end
)
end

@doc """
Expand Down
Loading

0 comments on commit e56bbe9

Please sign in to comment.