Skip to content

Commit da22e3d

Browse files
committed
remember last user format
1 parent e769044 commit da22e3d

14 files changed

+129
-9
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ cardian-*.tar
2727

2828
.elixir_ls
2929

30-
.env
30+
.env
31+
32+
# Database files
33+
*.db
34+
*.db-*

PRIVACY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Privacy Policy
22

3-
Cardian doesn't store any data. Publicly available data is cached for operational purposes.\
3+
Cardian stores the user id to remember your last format preference.\
4+
Publicly available data is cached for operational purposes.\
45
If an error occurs your User ID might be logged for debugging purposes.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The bot is available in the new Discord app directory!
1313
- Autocompletion
1414
- Card suggestions with fuzzy searching
1515
- Card info
16+
- different formats using the `format:` option
17+
- remembers the last used option as default
1618
- High quality art
1719
- Add card info to messages with `<card name>`s via right click
1820

cardian.nomad

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ job "cardian" {
66
group "cardian" {
77
count = 1
88

9+
volume "cardian" {
10+
type = "host"
11+
source = "cardian"
12+
read_only = false
13+
}
14+
915
task "cardian" {
1016
driver = "docker"
1117

@@ -19,6 +25,11 @@ job "cardian" {
1925
SENTRY_URL = "url"
2026
}
2127

28+
volume_mount {
29+
volume = "cardian"
30+
destination = "/db"
31+
}
32+
2233
resources {
2334
cpu = 1000
2435
memory = 1000

config/config.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Config
2+
3+
config :cardian,
4+
ecto_repos: [Cardian.Repo]

config/runtime.exs

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ config :cardian,
1010
update_interval: String.to_integer(System.get_env("CARDIAN_UPDATE_INTERVAL", "120")),
1111
bonk_url: System.get_env("BONK_URL", "http://localhost:3000/order/list?auth=test-token")
1212

13+
config :cardian, Cardian.Repo,
14+
database: "database.db",
15+
migration_primary_key: [name: :id, type: :binary_id]
16+
1317
config :sentry,
1418
dsn: System.fetch_env!("SENTRY_URL"),
1519
enable_source_code_context: true,
@@ -20,4 +24,6 @@ config :sentry,
2024
if config_env() == :prod do
2125
config :logger,
2226
level: :info
27+
28+
config :cardian, Cardian.Repo, database: "/db/database.db"
2329
end

lib/cardian/application.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ defmodule Cardian.Application do
88
@impl true
99
def start(_type, _args) do
1010
children = [
11+
{Ecto.Migrator, repos: Application.fetch_env!(:cardian, :ecto_repos)},
1112
Cardian.EventConsumer,
12-
Cardian.CardRegistry
13+
Cardian.CardRegistry,
14+
Cardian.Repo
1315
]
1416

1517
# See https://hexdocs.pm/elixir/Supervisor.html

lib/cardian/configs/user_config.ex

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Cardian.Configs.UserConfig do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:id, Ecto.ULID, autogenerate: true}
6+
schema "user_configs" do
7+
field(:discord_id, :integer)
8+
field(:format, Ecto.Enum, values: [:paper, :md, :dl])
9+
10+
timestamps()
11+
end
12+
13+
def changeset(user_config, params \\ %{}) do
14+
user_config
15+
|> cast(params, [:discord_id, :format])
16+
|> validate_required([:discord_id, :format])
17+
|> unique_constraint(:discord_id)
18+
end
19+
end

lib/cardian/interactions.ex

+25-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Cardian.Interactions do
33
alias Nostrum.Api
44
alias Nostrum.Struct.Interaction
55
alias Cardian.{Builder, CardRegistry}
6+
alias Cardian.Configs.UserConfig
7+
alias Cardian.UserConfigs
68

79
def deploy_commands() do
810
{:ok, _} = Api.bulk_overwrite_global_application_commands(get_commands())
@@ -32,7 +34,7 @@ defmodule Cardian.Interactions do
3234
%{
3335
type: 3,
3436
name: "format",
35-
description: "Which format of the card game (default: Paper)",
37+
description: "Which format of the card game (remembers your last choice)",
3638
choices: [
3739
%{
3840
name: "Paper",
@@ -101,7 +103,8 @@ defmodule Cardian.Interactions do
101103
data: %{
102104
name: "card",
103105
options: [%{name: "name", value: card} | _] = options
104-
}
106+
},
107+
user: %{id: user_id}
105108
} = interaction
106109
) do
107110
Api.create_interaction_response!(interaction, %{type: 5})
@@ -110,9 +113,26 @@ defmodule Cardian.Interactions do
110113
[c | _] ->
111114
format =
112115
case Enum.find(options, &(&1.name == "format")) do
113-
%{name: "format", value: "md"} -> :md
114-
%{name: "format", value: "dl"} -> :dl
115-
_ -> :paper
116+
%{name: "format", value: "paper"} ->
117+
UserConfigs.create_or_update_config(%{discord_id: user_id, format: :paper})
118+
119+
:paper
120+
121+
%{name: "format", value: "md"} ->
122+
UserConfigs.create_or_update_config(%{discord_id: user_id, format: :md})
123+
124+
:md
125+
126+
%{name: "format", value: "dl"} ->
127+
UserConfigs.create_or_update_config(%{discord_id: user_id, format: :dl})
128+
129+
:dl
130+
131+
_ ->
132+
case UserConfigs.get_config_by_discord_id(user_id) do
133+
%UserConfig{format: f} -> f
134+
_ -> :paper
135+
end
116136
end
117137

118138
msg = %{

lib/cardian/repo.ex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Cardian.Repo do
2+
use Ecto.Repo,
3+
otp_app: :cardian,
4+
adapter: Ecto.Adapters.SQLite3
5+
end

lib/cardian/user_configs.ex

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule Cardian.UserConfigs do
2+
import Ecto.Query, warn: false
3+
alias Cardian.Repo
4+
alias Cardian.Configs.UserConfig
5+
6+
def list_configs do
7+
Repo.all(UserConfig)
8+
end
9+
10+
def get_config_by_discord_id(discord_id) do
11+
Repo.get_by(UserConfig, discord_id: discord_id)
12+
end
13+
14+
def create_or_update_config(attrs \\ %{}) do
15+
case get_config_by_discord_id(attrs.discord_id) do
16+
nil -> %UserConfig{discord_id: attrs.discord_id}
17+
config -> config
18+
end
19+
|> UserConfig.changeset(attrs)
20+
|> Repo.insert_or_update()
21+
end
22+
end

mix.exs

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ defmodule Cardian.MixProject do
2626
{:req, "0.3.0"},
2727
{:nimble_parsec, "~> 1.2"},
2828
{:sentry, "~> 8.0"},
29-
{:hackney, "~> 1.18"}
29+
{:hackney, "~> 1.18"},
30+
{:ecto_sql, "~> 3.10"},
31+
{:ecto_sqlite3, ">= 0.0.0"},
32+
{:ecto_ulid, "~> 0.3.0"}
3033
]
3134
end
3235
end

mix.lock

+9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
%{
22
"castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"},
3+
"cc_precompiler": {:hex, :cc_precompiler, "0.1.8", "933a5f4da3b19ee56539a076076ce4d7716d64efc8db46fd066996a7e46e2bfd", [:mix], [{:elixir_make, "~> 0.7.3", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "176bdf4366956e456bf761b54ad70bc4103d0269ca9558fd7cee93d1b3f116db"},
34
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
45
"chacha20": {:hex, :chacha20, "1.0.4", "0359d8f9a32269271044c1b471d5cf69660c362a7c61a98f73a05ef0b5d9eb9e", [:mix], [], "hexpm", "2027f5d321ae9903f1f0da7f51b0635ad6b8819bc7fe397837930a2011bc2349"},
56
"cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"},
67
"curve25519": {:hex, :curve25519, "1.0.5", "f801179424e4012049fcfcfcda74ac04f65d0ffceeb80e7ef1d3352deb09f5bb", [:mix], [], "hexpm", "0fba3ad55bf1154d4d5fc3ae5fb91b912b77b13f0def6ccb3a5d58168ff4192d"},
8+
"db_connection": {:hex, :db_connection, "2.5.0", "bb6d4f30d35ded97b29fe80d8bd6f928a1912ca1ff110831edcd238a1973652c", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c92d5ba26cd69ead1ff7582dbb860adeedfff39774105a4f1c92cbb654b55aa2"},
9+
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
10+
"ecto": {:hex, :ecto, "3.10.3", "eb2ae2eecd210b4eb8bece1217b297ad4ff824b4384c0e3fdd28aaf96edd6135", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "44bec74e2364d491d70f7e42cd0d690922659d329f6465e89feb8a34e8cd3433"},
11+
"ecto_sql": {:hex, :ecto_sql, "3.10.2", "6b98b46534b5c2f8b8b5f03f126e75e2a73c64f3c071149d32987a5378b0fdbd", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "68c018debca57cb9235e3889affdaec7a10616a4e3a80c99fa1d01fdafaa9007"},
12+
"ecto_sqlite3": {:hex, :ecto_sqlite3, "0.11.0", "1e094ade9ff1bc7c33c5c6b114f8a5156d0b7c5ddf9038d61cb8fdd61e7c4c55", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exqlite, "~> 0.9", [hex: :exqlite, repo: "hexpm", optional: false]}], "hexpm", "3d5b9a69b9a9547329413b278b4b072b9bbadf4fd599a746b3d6b0e174a418bb"},
13+
"ecto_ulid": {:hex, :ecto_ulid, "0.3.0", "fd6426ff30da547d6f5c31e43170ad307cbda2e680c7793c891e9ef86bd68dbe", [:mix], [{:ecto, "~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "82cb3be73635587700f1a4aec08e4ad894e7b3d2f6ed63236b9f3afd3859c74d"},
714
"ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"},
15+
"elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"},
816
"equivalex": {:hex, :equivalex, "1.0.3", "170d9a82ae066e0020dfe1cf7811381669565922eb3359f6c91d7e9a1124ff74", [:mix], [], "hexpm", "46fa311adb855117d36e461b9c0ad2598f72110ad17ad73d7533c78020e045fc"},
17+
"exqlite": {:hex, :exqlite, "0.13.15", "a32c0763915e2b0d7ced9dd8638802d38e9569053f3b28b815bd0faef1cbe6d9", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "4afcc870a33b57781a1e57cd4294eef68815059d26b774c7cd075536b21434b7"},
918
"finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"},
1019
"gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"},
1120
"gun": {:hex, :gun, "2.0.1", "160a9a5394800fcba41bc7e6d421295cf9a7894c2252c0678244948e3336ad73", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "a10bc8d6096b9502205022334f719cc9a08d9adcfbfc0dbee9ef31b56274a20b"},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Cardian.Repo.Migrations.CreateUserconfig do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:user_configs) do
6+
add :discord_id, :integer
7+
add :format, :string
8+
9+
timestamps()
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)