Commit 29b3ad6f authored by Peter van der meulen's avatar Peter van der meulen

Add instructions

parent 8bc85216
......@@ -19,10 +19,10 @@ config :snuggles_app,
alliance_name: System.get_env("ALLIANCE_NAME"),
discord_snuggles_id: String.to_integer(System.get_env("DISCORD_SNUGGLES_ID")),
discord_auth_channel_id: String.to_integer(System.get_env("DISCORD_AUTH_CHANNEL_ID")),
discord_admin_channel_id: String.to_integer(System.get_env("DISCORD_ADMIN_CHANNEL_ID")),
discord_guild_id: String.to_integer(System.get_env("DISCORD_GUILD_ID")),
discord_default_role_id: String.to_integer(System.get_env("DISCORD_DEFAULT_ROLE_ID")),
discord_all_role_ids: (System.get_env("DISCORD_ALL_ROLE_IDS") |> String.split(",") |> Enum.map(&String.to_integer/1)),
discord_admin_channel_id: String.to_integer(System.get_env("DISCORD_ADMIN_CHANNEL_ID"))
discord_all_role_ids: (System.get_env("DISCORD_ALL_ROLE_IDS") |> String.split(",") |> Enum.map(&String.to_integer/1))
# Configures the endpoint
config :snuggles_app, SnugglesAppWeb.Endpoint,
......
......@@ -11,6 +11,7 @@ defmodule SnugglesApp.Accounts do
alias SnugglesApp.Accounts.User
alias SnugglesApp.Accounts.EveUser
alias SnugglesApp.Accounts.Corporation
@doc """
Returns the list of users.
......@@ -66,7 +67,7 @@ defmodule SnugglesApp.Accounts do
|> Repo.insert()
end
def handle_user_auth_request(%{"author_id" => author_id, "channel_id" => _, "content" => content}) do
def handle_user_auth_request(%{"level" => _, "author_id" => author_id, "channel_id" => _, "content" => content}) do
case Regex.run(~r/!auth-(.+?)$/, content) do
nil ->
Logger.info("Account unmatched auth req: #{content}")
......@@ -131,6 +132,32 @@ defmodule SnugglesApp.Accounts do
{:ok}
end
@doc """
Deals with requests from the discord admin channel.
## Examples
iex> handle_admin_request(user, attrs)
{:ok, %User{}}
iex> updatee_user(user, attrs)
{:error, %Ecto.Changeset{}}
"""
def handle_admin_request(%{"author_id" => author_id, "content" => content} = payload) do
case String.downcase(content) do
"help" -> Discord.send_admin_help_message(author_id)
"add corp discord" -> send_admin_corp_creation_help_message(author_id)
_ -> Discord.send_admin_fallback_message(author_id)
end
IO.puts "#{author_id} send: #{content}"
end
defp send_admin_corp_creation_help_message(author_id) do
user = Repo.get_by(User, discord_id: Integer.to_string(author_id))
Discord.send_admin_corp_help_message(author_id, user)
end
@doc """
Updates a user.
......
defmodule SnugglesApp.Discord.Admin do
def fallback_message(author_id) do
"Sorry <@#{author_id}>, I don't understand you.
Try ***\"Help\"***, for a full list of interactions"
end
def help_message(author_id) do
"Hey <@#{author_id}>, I'm snuggles, You can do the following things in this channel:
**Add corp discord** - Gets you started with adding automated auth to your own corp-server.
**Help** - See this message."
end
def corp_add_help_message(author_id, nil) do
"Hey <@#{author_id}>, It looks like you have not yet authenticated yourself on this server,
You will need to get official access on ***this*** server to create corportions."
end
def corp_add_help_message(author_id, user) do
"Hey <@#{author_id}>, Thanks for showing interest to get your corp #{user.corp_ticker} linked to the alliance Auth
Here are some easy steps to get started:
1. Create an auth role on your server that sits above the normal line-member role, give that role the following rights:
```
- Manage roles
- Change Nickname
- Manage Nicknames
- Read Text channels and see voice channels.
- Send text messages
- The auth role needs access to read the public channel
```
The auth role can manage the role below it, not avove. Sort them like: https://i.imgur.com/tPG52MY.png
2. Invite snuggles-auth to your server using this link and give it the auth role:
https://discordapp.com/oauth2/authorize?client_id=#{Application.get_env(:snuggles_app, :discord_snuggles_id)}&scope=bot
3. Send a message in your public channel like this:
```
@snuggles add corp @MentionedMemberRole
```
4. When users join your corp discord they need to post a message in ***the alliance landing pad*** to get rights on your corp server:
```
!auth-corp
Talk to your local IT monkey if you have further questions or need help.
```
"
end
end
......@@ -7,6 +7,7 @@ defmodule SnugglesApp.Discord do
alias SnugglesApp.Discord.Roles
alias SnugglesApp.Discord.User
alias SnugglesApp.Discord.Message
alias SnugglesApp.Discord.Admin
@doc """
Starts the Discord Messages Post worker.
......@@ -75,7 +76,7 @@ defmodule SnugglesApp.Discord do
end
@doc """
Sends message to designated admin
Sends message to designated admin channel
## Examples
......@@ -85,4 +86,39 @@ defmodule SnugglesApp.Discord do
def send_message_to_admin(message) do
Message.send_admin(message)
end
@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
end
defmodule SnugglesApp.Discord.PostWorker do
use Nostrum.Consumer
alias SnugglesApp.RabbitService
alias Nostrum.Api
@auth_channel_id Application.get_env(:snuggles_app, :discord_auth_channel_id)
@admin_channel_id Application.get_env(:snuggles_app, :discord_admin_channel_id)
@snuggles_id Application.get_env(:snuggles_app, :discord_snuggles_id)
def start_link do
Consumer.start_link(__MODULE__)
end
def handle_event({:MESSAGE_CREATE, {msg}, _ws_state}) do
cond do
auth_message?(msg.channel_id) ->
{:ok, _} = RabbitService.publish_json("account.events", strip_message(msg))
msg.author.id == @snuggles_id ->
{:ok, "ignore"}
msg.channel_id == @auth_channel_id ->
{:ok, _} = RabbitService.publish_json("account.events", strip_message(msg, "member"))
Api.delete_message(msg.channel_id, msg.id)
msg.channel_id == @admin_channel_id ->
{:ok, _} = RabbitService.publish_json("account.events", strip_message(msg, "admin"))
to_snuggles?(msg.mentions) ->
{:ok, _} = RabbitService.publish_json(exchange(msg), strip_message(msg))
true ->
{:ok, "ignore"}
end
......@@ -28,19 +38,16 @@ defmodule SnugglesApp.Discord.PostWorker do
length(mentions) > 0 && Enum.at(mentions, 0).id == Application.get_env(:snuggles_app, :discord_snuggles_id)
end
defp auth_message?(channel_id) do
channel_id == Application.get_env(:snuggles_app, :discord_auth_channel_id)
end
defp exchange(msg) do
intent = msg.content |> String.split |> Enum.at(1) |> String.downcase
case intent do
_ -> "default.responses"
_ -> "default.responses"
end
end
defp strip_message(msg) do
defp strip_message(msg, level \\ "member") do
%{
level: level,
author_id: msg.author.id,
channel_id: msg.channel_id,
content: msg.content
......
......@@ -17,17 +17,17 @@ defmodule SnugglesApp.RabbitService.AccountConsumer do
end
# Confirmation sent by the broker after registering this process as a consumer
def handle_info({:basic_consume_ok, %{consumer_tag: consumer_tag}}, chan) do
def handle_info({:basic_consume_ok, %{consumer_tag: _}}, chan) do
{:noreply, chan}
end
# Sent by the broker when the consumer is unexpectedly cancelled (such as after a queue deletion)
def handle_info({:basic_cancel, %{consumer_tag: consumer_tag}}, chan) do
def handle_info({:basic_cancel, %{consumer_tag: _}}, chan) do
{:stop, :normal, chan}
end
# Confirmation sent by the broker to the consumer process after a Basic.cancel
def handle_info({:basic_cancel_ok, %{consumer_tag: consumer_tag}}, chan) do
def handle_info({:basic_cancel_ok, %{consumer_tag: _}}, chan) do
{:noreply, chan}
end
......@@ -76,9 +76,12 @@ defmodule SnugglesApp.RabbitService.AccountConsumer do
defp consume(channel, tag, redelivered, payload) do
case Poison.decode(payload) do
{:ok, object =%{"author_id" => _, "channel_id" => _, "content" => _}} ->
{:ok, object = %{"level" => "member", "author_id" => _, "channel_id" => _, "content" => _}} ->
SnugglesApp.Accounts.handle_user_auth_request(object)
:ok = Basic.ack channel, tag
{:ok, object = %{"level" => "admin", "author_id" => _, "channel_id" => _, "content" => _}} ->
SnugglesApp.Accounts.handle_admin_request(object)
:ok = Basic.ack channel, tag
{:ok, object = %{"user_id" => _}} ->
SnugglesApp.Accounts.handle_user_update_request(object)
:ok = Basic.ack channel, tag
......@@ -87,17 +90,17 @@ defmodule SnugglesApp.RabbitService.AccountConsumer do
Logger.info "AccountConsumer received invalid #{payload} from exchange"
{:error, reason} -> Logger.error "AccountConsumer: #{IO.inspect(reason)}, payload: #{payload}"
end
rescue
# Requeue unless it's a redelivered message.
# This means we will retry consuming a message once in case of exception
# before we give up and have it moved to the error queue
#
# You might also want to catch :exit signal in production code.
# Make sure you call ack, nack or reject otherwise comsumer will stop
# receiving messages.
exception ->
:ok = Basic.reject channel, tag, requeue: not redelivered
Logger.error "AccountConsumer error: payload: #{(payload)}"
# rescue
# # Requeue unless it's a redelivered message.
# # This means we will retry consuming a message once in case of exception
# # before we give up and have it moved to the error queue
# #
# # You might also want to catch :exit signal in production code.
# # Make sure you call ack, nack or reject otherwise comsumer will stop
# # receiving messages.
# exception ->
# :ok = Basic.reject channel, tag, requeue: not redelivered
# Logger.error "AccountConsumer error: payload: #{(payload)}"
end
end
......@@ -39,7 +39,7 @@ defmodule SnugglesAppWeb.UserController do
defp create_user(conn, code) do
case Accounts.create_user(code) do
{:ok, user} -> render(conn, "thanks.html", auth_token: user.auth_token)
{:error, reason} -> (conn |> put_flash(:info, "Something went wrong, lets try again") |> redirect(to: "/auth"))
{:error, _} -> (conn |> put_flash(:info, "Something went wrong, lets try again") |> redirect(to: "/auth"))
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment