I'm making a bot that will be used for "points" tracking and such, like a reward system and I'm using Ecto with postgrex
, this is my approach so far, it works but I'm not sure if I'm doing it in an "Elixir", functional way or if I can improve this a bit ^-^
defmodule Cord.Commands do
...
def add_me(msg) do
case Repo.get_by(Rewards, user: msg.author.id) do
# Adds user if it doesn't exist
nil ->
case Repo.insert(%Rewards{user: msg.author.id}) do
{:ok, _struct} ->
Api.create_message(msg.channel_id, ":white_check_mark:")
{:error, _changeset} ->
Api.create_message(msg.channel_id, ":x:")
end
_ ->
Api.create_message(msg.channel_id, "You're already registered")
end
end
# Maps to each id in the list and updates each one
def thanks(msg) do
Enum.map msg.mentions, fn user ->
repo_user = Repo.get_by(Rewards, user: user.id)
points = repo_user.quantity
repo_user = Ecto.Changeset.change(repo_user, quantity: points + 1)
Repo.update!(repo_user)
Api.create_message(msg.channel_id, "#{msg.author.username} gave a :cookie: to #{user.username}")
end
end
def not_found(_), do: IO.inspect :ignore
end