discord.ex 4.03 KB
Newer Older
1
defmodule SnugglesApp.Discord do
Peter van der meulen's avatar
Peter van der meulen committed
2 3 4 5
  @moduledoc """
  The AppMessages context responsible for sending messages from discord to apps
  """

6 7 8
  alias SnugglesApp.Discord.PostSupervisor
  alias SnugglesApp.Discord.Roles
  alias SnugglesApp.Discord.User
9
  alias SnugglesApp.Discord.Message
Peter van der meulen's avatar
Peter van der meulen committed
10
  alias SnugglesApp.Discord.Admin
11
  alias Nostrum.Api
Peter van der meulen's avatar
Peter van der meulen committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

  @doc """
  Starts the Discord Messages Post worker. 
  Messages coming from Discord will enter the application trough the worker,
  and then be pushed off in to queues to be handled by external apps and Discord 
  post delivery.
  This method is supposed to be ran once on application upstart.

  ## Examples

      iex> start_post_supervisor
      {:ok, #PID<0.346.0>}
  """
  def start_post_supervisor do
    PostSupervisor.start
  end
28

29

30
  @doc """
31
  Adds or removes the basic role to given user's discord ID
32 33 34

  ## Examples

35
      iex> manage_alliance_permissions(discord_id, previous_access, current_access)
36 37
      {:ok}
  """
38 39 40 41
  def manage_alliance_permissions(_, unchanged, unchanged), do: {:ok}

  def manage_alliance_permissions(discord_id, _, true) do
    add_default_role_to_user(discord_id)
42
    Discord.update_user_name(updated_user)
43 44 45 46
  end

  def manage_alliance_permissions(discord_id, _, false) do
    remove_default_role_from_user(String.to_integer(discord_id))
47
    Discord.update_user_name(updated_user)
48 49 50
  end

  defp add_default_role_to_user(discord_id) do
51 52
    {:ok} = Roles.add_default_to_user(discord_id)
    {:ok, _} = send_message_to_admin("<@#{discord_id}> reveived member rights")
53 54
  end

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  defp remove_default_role_from_user(discord_id) do
    Roles.remove_default_from_user(discord_id)
    {:ok, _} = send_message_to_admin("<@#{discord_id}> lost member rights")
  end

  @doc """
  Removes corp roles from user

  ## Examples

      iex> remove_corp_role_from_user(user_discord_id, corp_discord_id)
      {:ok}
  """
  def remove_corp_role_from_user(user_discord_id, corp_discord_id, discord_role_id) do
    Roles.remove_custom_from_user(user_discord_id, corp_discord_id, discord_role_id)
    {:ok, _} = send_message_to_admin("<@#{user_discord_id}> lost corp member rights")
  end

73
  @doc """
74 75
  Removes corp roles from user

76 77
  ## Examples

78
      iex> remove_corp_role_from_user(user_discord_id, corp_discord_id)
79 80
      {:ok}
  """
81 82 83
  def add_corp_role_to_user(user_discord_id, corp_discord_id, discord_role_id) do
    Roles.add_custom_to_user(user_discord_id, corp_discord_id, discord_role_id)
    {:ok, _} = send_message_to_admin("<@#{user_discord_id}> received corp member rights")
84 85 86 87
  end

  @doc """
  Updates a user name conform eve standards
88

89 90 91 92 93 94 95
  ## Examples

      iex> add_default_to_user(User%{})
      {:ok}
  """
  def update_user_name(user) do
    User.set_name(user)
96
  end
97

Peter van der meulen's avatar
Peter van der meulen committed
98 99 100 101
  def update_corp_user_name(user, guild_id) do
    User.set_name(user, guild_id)
  end

102 103 104 105 106 107 108 109 110 111 112 113 114
  @doc """
  Sends message to discord

  ## Examples

      iex> send_message("hello", user_id)
      {:ok}
  """
  def send_message(message, channel) do
    Message.send_message(message, channel)
  end

  @doc """
Peter van der meulen's avatar
Peter van der meulen committed
115
  Sends message to designated admin channel
116 117 118 119 120 121 122 123 124

  ## Examples

      iex> send_admin("hello")
      {:ok}
  """
  def send_message_to_admin(message) do
    Message.send_admin(message)
  end
Peter van der meulen's avatar
Peter van der meulen committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  @doc """
  Sends help message to designated admin channel

  ## Examples

      iex> send_admin_help_message("author_id")
      {:ok}
  """
  def send_admin_help_message(author_id) do
    Admin.help_message(author_id) |> Message.send_admin()
  end

  @doc """
  Sends corp help message to designated admin channel

  ## Examples
      iex> send_admin_corp_help_message(User%{user})
      {:ok}
  """
  def send_admin_corp_help_message(author_id, user) do
    Admin.corp_add_help_message(author_id, user) |> Message.send_admin()
  end

  @doc """
  Sends fallback message to designated admin channel

  ## Examples

      iex> send_admin_fallback_message("author_id")
      {:ok}
  """
  def send_admin_fallback_message(author_id) do
    Admin.fallback_message(author_id) |> Message.send_admin()
  end
160 161 162 163

  def remove_message(msg) do
    Api.delete_message(msg.channel_id, msg.id)
  end
Peter van der meulen's avatar
Peter van der meulen committed
164
end