Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Fix Alchemy.Reactions.Emoji.resolve/1 crashing all reaciton management related functions #112

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/Structs/Channels/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ defmodule Alchemy.Channel do
ChannelCategory,
VoiceChannel,
DMChannel,
GroupDMChannel
GroupDMChannel,
NewsChannel,
StoreChannel
}

alias Alchemy.User
import Alchemy.Structs

@moduledoc """
This module contains useful functions for operating on `Channels`.
Expand Down Expand Up @@ -159,7 +160,7 @@ defmodule Alchemy.Channel do

Whether or not the channel is considered nsfw
- `last_message_id`

The id of the last message sent in the channel, if any
- `parent_id`

Expand Down Expand Up @@ -222,7 +223,7 @@ defmodule Alchemy.Channel do

The id of the guild this channel belongs to
- `position`

The sorting position of this channel in the guild
- `permission_overwrites`

Expand Down Expand Up @@ -324,6 +325,8 @@ defmodule Alchemy.Channel do
2 -> VoiceChannel.from_map(map)
3 -> GroupDMChannel.from_map(map)
4 -> ChannelCategory.from_map(map)
5 -> NewsChannel.from_map(map)
6 -> StoreChannel.from_map(map)
end
end
end
23 changes: 23 additions & 0 deletions lib/Structs/Channels/news_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Alchemy.Channel.NewsChannel do
@moduledoc false
alias Alchemy.OverWrite
import Alchemy.Structs

defstruct [
:id,
:guild_id,
:position,
:permission_overwrites,
:name,
:topic,
:nsfw,
:last_message_id,
:parent_id
]

def from_map(map) do
map
|> field_map("permission_overwrites", &map_struct(&1, OverWrite))
|> to_struct(__MODULE__)
end
end
24 changes: 24 additions & 0 deletions lib/Structs/Channels/store_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Alchemy.Channel.StoreChannel do
@moduledoc false
alias Alchemy.OverWrite
import Alchemy.Structs

# 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,
:position,
:permission_overwrites,
:name,
:last_message_id,
:parent_id
]

def from_map(map) do
map
|> field_map("permission_overwrites", &map_struct(&1, OverWrite))
|> to_struct(__MODULE__)
end
end
25 changes: 24 additions & 1 deletion lib/Structs/Messages/Reactions/emoji.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,34 @@ defmodule Alchemy.Reaction.Emoji do
defstruct [:id,
:name]

@type t :: %__MODULE__{id: String.t(), name: String.t()}

@doc false

@doc """
Returns the %Emoji{} struct,
resolving its values according with the data type of the parameter `emoji`.

## Example
```shell
iex(1)> Alchemy.Reaction.Emoji.resolve(%Emoji{id: nil, name: "✅"})
%Emoji{id: nil, name: "✅"}

iex(2)> Alchemy.Reaction.Emoji.resolve(%{id: nil, name: "✅"})
%Emoji{id: nil, name: "✅"}

iex(3)> Alchemy.Reaction.Emoji.resolve(%{"id" => nil, "name" => "✅"})
%Emoji{id: nil, name: "✅"}

iex(4)> Alchemy.Reaction.Emoji.resolve("✅")
%Emoji{id: nil, name: "✅"}
```
"""
@spec resolve(emoji :: any()) :: Emoji.t()
def resolve(emoji) do
case emoji do
%__MODULE__{} = em -> em
%{"id" => id, "name" => name} -> %__MODULE__{id: id, name: name}
%{id: id, name: name} -> %__MODULE__{id: id, name: name}
unicode -> %__MODULE__{name: unicode}
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/cogs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Alchemy.Cogs do
alias Alchemy.Cogs.CommandHandler
alias Alchemy.Cogs.EventRegistry
alias Alchemy.Events
alias Alchemy.Guild
require Logger

@moduledoc """
Expand Down Expand Up @@ -604,7 +603,7 @@ defmodule Alchemy.Cogs do
@doc """
Returns a map from command name (string) to the command information.

Each command is either `{module, arity, function_name}`, or
Each command is either `{module, arity, function_name}`, or
`{module, arity, function_name, parser}`.

This can be useful for providing some kind of help command, or telling
Expand Down Expand Up @@ -643,7 +642,7 @@ defmodule Alchemy.Cogs do
Returns the permission bitset of the current member in the channel the command
was called from.

If you just want the base permissions of the member in the guild,
If you just want the base permissions of the member in the guild,
see `guild_permissions`.
Returns `{:ok, perms}`, or `{:error, why}`. Fails if not called from
a guild, or the guild or the member couldn't be fetched from the cache.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Alchemy.Mixfile do
def project do
[
app: :alchemy,
version: "0.6.6",
version: "0.6.8",
Copy link
Contributor Author

@aramsm aramsm Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cronokirby I'm assuming the last PR (#110) was version 0.6.7 so I jumped to 0.6.8 =P

elixir: "~> 1.8",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down