Skip to content
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
3 changes: 3 additions & 0 deletions config/config.template.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ config :kintama_store_bot, KintamaStoreBot.Repo,
database: "./database.db" # Change anything you want

config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

config :bandit,
port: String.to_integer(System.get_env("PORT")) || 8888
3 changes: 2 additions & 1 deletion lib/kintama_store/api/riot_auth_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ defmodule KintamaStoreBot.Api.RiotAuthApi do
def auth_cookies(client) do
req_body = %{
client_id: "play-valorant-web-prod",
nonce: 1,
nonce: "1",
redirect_uri: "https://playvalorant.com/opt_in",
response_type: "token id_token",
scope: "account openid",
}

{:ok, response} = Tesla.post(client, "/api/v1/authorization", req_body)
Expand Down
4 changes: 4 additions & 0 deletions lib/kintama_store/api/valorant_official_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ defmodule KintamaStoreBot.Api.ValorantOfficialApi do

end

# refs: for ClientPlatform: https://valapidocs.techchrism.me/endpoint/prices#client-platform
# refs: for ClientVersion: https://dash.valorant-api.com/endpoints/version
@spec client(String.t(), String.t()) :: Tesla.Client.t()
def client(riot_token, riot_entitlement) do
middleware = [
{Tesla.Middleware.BaseUrl, "https://pd.ap.a.pvp.net"},
Tesla.Middleware.JSON,
{Tesla.Middleware.Headers, [
{"X-Riot-Entitlements-JWT", riot_entitlement},
{"X-Riot-ClientPlatform", "ew0KCSJwbGF0Zm9ybVR5cGUiOiAiUEMiLA0KCSJwbGF0Zm9ybU9TIjogIldpbmRvd3MiLA0KCSJwbGF0Zm9ybU9TVmVyc2lvbiI6ICIxMC4wLjE5MDQyLjEuMjU2LjY0Yml0IiwNCgkicGxhdGZvcm1DaGlwc2V0IjogIlVua25vd24iDQp9"},
{"X-Riot-ClientVersion", "release-09.10-shipping-16-2953141"},
{"Content-Type", "application/json"},
]},
{Tesla.Middleware.BearerAuth, token: riot_token},
Expand Down
3 changes: 2 additions & 1 deletion lib/kintama_store/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ defmodule KintamaStoreBot.Application do
children = [
# Starts a worker by calling: KintamaStore.Worker.start_link(arg)
# {KintamaStore.Worker, arg}
Nosedrum.Storage.ETS,
# Nosedrum.Storage.ETS,
{KintamaStoreBot.Consumer, restart: :permanent, max_restarts: 10},
{KintamaStoreBot.Repo, []},
{Bandit, plug: KintamaStoreBot.Plug.OAuthPlug, scheme: :http, port: Application.get_env(:bandit, :port)},
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
30 changes: 14 additions & 16 deletions lib/kintama_store/consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ defmodule KintamaStoreBot.Consumer do
alias KintamaStoreBot.Handler.{InteractionHandler,MessageHandler}
alias KintamaStoreBot.Utils.DiscordUtils

alias Nosedrum.Invoker.Split, as: CommandInvoker
alias Nosedrum.Storage.ETS, as: CommandStorage

use Nostrum.Consumer

@commands %{
"applycmd" => KintamaStoreBot.Cogs.ApplyCommand,
"removecmd" => KintamaStoreBot.Cogs.RemoveCommand,
"listcmd" => KintamaStoreBot.Cogs.ListGuildCommands,
"help" => KintamaStoreBot.Cogs.Help,
}
# @commands %{
# "applycmd" => KintamaStoreBot.Cogs.ApplyCommand,
# "removecmd" => KintamaStoreBot.Cogs.RemoveCommand,
# "listcmd" => KintamaStoreBot.Cogs.ListGuildCommands,
# "help" => KintamaStoreBot.Cogs.Help,
# }

def start_link do
Memento.Schema.create([node()])
Expand All @@ -30,18 +27,18 @@ defmodule KintamaStoreBot.Consumer do
end

def handle_event({:READY, _data, _ws_state}) do
Api.update_status(:online, ".help")
Api.update_status(:online, "/storeでストアを確認しよう")

Enum.each(@commands, fn {name, cog} -> CommandStorage.add_command([name], cog) end)
# Enum.each(@commands, fn {name, cog} -> CommandStorage.add_command([name], cog) end)
end

def handle_event({:MESSAGE_CREATE, msg, _ws_state}) do
def handle_event({:MESSAGE_CREATE, {_old_msg, new_msg}, _ws_state}) do
try do
CommandInvoker.handle_message(msg, CommandStorage)
MessageHandler.handle_before(msg)
# CommandInvoker.handle_message(msg, CommandStorage)
MessageHandler.handle_before(new_msg)
rescue
e ->
Api.create_message(msg.channel_id, ":exclamation: コマンドの実行中にエラーが発生しました。もう一度お試しいただくか、管理者に連絡してください。")
Api.create_message(new_msg.channel_id, ":exclamation: コマンドの実行中にエラーが発生しました。もう一度お試しいただくか、管理者に連絡してください。")
IO.inspect(e, label: "An error occued while handling message")
end
end
Expand All @@ -60,7 +57,8 @@ defmodule KintamaStoreBot.Consumer do
flags: 64
}
})
IO.inspect(e, label: "An error occued while handling message")
IO.inspect(e, label: "An error occued while handling interaction")
IO.puts(Exception.format_stacktrace(__STACKTRACE__))
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/kintama_store/handler/interaction_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ defmodule KintamaStoreBot.Handler.InteractionHandler do
}
})
{username, password} ->
Memento.transaction! fn ->
Memento.transaction fn ->
Memento.Query.select(AuthState, {:==, :discord_user_id, discord_user_id})
|> Enum.at(0)
|> case do
%AuthState{} ->
MementoUtils.update_state(discord_user_id, %{
MementoUtils.update_state!(discord_user_id, %{
state_name: "auth",
executed_command: cmd_name,
username: username,
password: password
})

_ ->
Memento.Query.write(%AuthState{
Memento.Query.write!(%AuthState{
discord_user_id: discord_user_id,
state_name: "auth",
executed_command: cmd_name,
Expand All @@ -121,13 +121,16 @@ defmodule KintamaStoreBot.Handler.InteractionHandler do

auth_client = RiotAuthApi.client("")
initial_cookie = auth_client |> RiotAuthApi.auth_cookies()

IO.puts("Proceeding login to riot account...")

# Thus auth_request returns token with path, or 2fa response
# auth_requestは成功時にパスに入ったトークンか、もしくは2段階認証を返す
%{body: res_body, cookie: mfa_cookie} = auth_client |> RiotAuthApi.auth_request(initial_cookie, username, password)

case res_body do
%{"error" => "auth_failure"} ->
IO.inspect(res_body)
Api.create_interaction_response(interaction, %{
type: 4,
data: %{
Expand Down
28 changes: 28 additions & 0 deletions lib/kintama_store/plug/oauth_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule KintamaStoreBot.Plug.OAuthPlug do
use Plug.Router

def init(options) do
# initialize options
options
end

plug :match
plug :dispatch

get "/" do
conn
|> put_resp_content_type("text/html")
|> send_resp(200, "hello world")
end

get "/callback" do
IO.inspect(conn.query_params)
conn
|> put_resp_content_type("text/html")
|> send_resp(200, "hello world")
end

match _ do
send_resp(conn, 404, "404 Not Found")
end
end
2 changes: 1 addition & 1 deletion lib/kintama_store/utils/discord_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule KintamaStoreBot.Utils.DiscordUtils do
alias Nostrum.Struct.{Interaction}

def get_user_id(%Interaction{} = interaction) do
Integer.to_string(interaction.member.user.id)
Integer.to_string(interaction.member.user_id)
end

def get_guild_id(%Interaction{} = interaction) do
Expand Down
4 changes: 2 additions & 2 deletions lib/kintama_store/utils/memento_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ defmodule KintamaStoreBot.Utils.MementoUtils do
end
end

def update_state(discord_user_id, changes = %{}) do
def update_state!(discord_user_id, changes = %{}) do
Memento.Query.select(AuthState, {:==, :discord_user_id, discord_user_id})
|> Enum.at(0)
|> case do
%AuthState{} = state ->
state
|> struct(changes)
|> Memento.Query.write()
|> Memento.Query.write!()
|> then(&{:ok, &1})

_ ->
Expand Down
29 changes: 16 additions & 13 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule KintamaStoreBot.MixProject do
[
app: :kintama_store_bot,
version: "0.1.0",
elixir: "~> 1.13",
elixir: "~> 1.17",
start_permanent: Mix.env() == :prod,
deps: deps(),
aliases: aliases(),
Expand All @@ -30,31 +30,34 @@ defmodule KintamaStoreBot.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:nostrum, "~> 0.6"},
{:nostrum, "~> 0.10"},
# {:nostrum, github: "Kraigie/nostrum", override: true},
# {:nostrum, path: "../nostrum", override: true},

{:nosedrum, "~> 0.4"},
{:tesla, "~> 1.4"},
#{:nosedrum, "~> 0.6"},
{:tesla, "~> 1.13"},

# optional, but recommended adapter
{:hackney, "~> 1.18"},
#{:hackney, "~> 1.20"},

{:httpoison, "~> 1.8"},
{:httpoison, "~> 2.2"},

# optional, required by JSON middleware
{:jason, ">= 1.0.0"},
{:poison, "~> 5.0"},
{:jason, ">= 1.4.4"},
{:poison, "~> 6.0"},

{:ecto, "~> 3.7"},
{:ecto_sql, "~> 3.0"},
{:ecto_sqlite3, "~> 0.7.3"},
{:ecto, "~> 3.12"},
{:ecto_sql, "~> 3.12"},
{:ecto_sqlite3, "~> 0.17.4"},

{:tzdata, "~> 1.1"},
{:tzdata, "~> 1.1.2"},

{:memento, "~> 0.3.2"},
{:memento, "~> 0.4.1"},
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}

# For http server to get riot oauth
{:bandit, "~> 1.0"}
]
end
end
Loading