Skip to content

Commit bc50e04

Browse files
committed
feat: Add /alter random command
1 parent fbf026c commit bc50e04

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎lib/octocon/alters.ex‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,37 @@ defmodule Octocon.Alters do
258258
end)
259259
end
260260

261+
def get_random_alter(system_identity, fields \\ @all_fields) do
262+
system_id = Accounts.id_from_system_identity(system_identity, :system)
263+
264+
all_alter_ids =
265+
from(
266+
a in Alter,
267+
where: a.user_id == ^system_id,
268+
select: a.id
269+
)
270+
|> Repo.all_regional({:user, system_identity})
271+
272+
if all_alter_ids == [] do
273+
nil
274+
else
275+
random_alter_id = Enum.random(all_alter_ids)
276+
277+
where =
278+
unwrap_system_identity_where(system_identity, [id: random_alter_id])
279+
280+
query =
281+
Alter
282+
|> where(^where)
283+
|> select([a], struct(a, ^fields))
284+
285+
case Repo.one_regional(query, {:user, system_identity}) do
286+
nil -> nil
287+
alter -> {:ok, alter}
288+
end
289+
end
290+
end
291+
261292
@doc """
262293
Given the friendship level and security level of an entity, returns whether the entity can be viewed.
263294

‎lib/octocon_discord/commands/alter.ex‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule OctoconDiscord.Commands.Alter do
1919
"delete" => &__MODULE__.delete/2,
2020
"view" => &__MODULE__.view/2,
2121
"avatar" => &__MODULE__.Avatar.command/2,
22+
"random" => &__MODULE__.random/2,
2223
"remove-alias" => &__MODULE__.remove_alias/2,
2324
"remove-proxy-name" => &__MODULE__.remove_proxy_name/2,
2425
"extra-images" => &__MODULE__.extra_images/2,
@@ -104,6 +105,22 @@ defmodule OctoconDiscord.Commands.Alter do
104105
end)
105106
end
106107

108+
def random(%{system_identity: system_identity}, options) do
109+
case Alters.get_random_alter(system_identity) do
110+
nil ->
111+
Utils.error_embed("You don't have any alters to choose from.")
112+
113+
{:ok, alter} ->
114+
show = Utils.get_show_option(options)
115+
116+
[
117+
embeds: [Utils.alter_embed(alter)],
118+
ephemeral?: !show
119+
]
120+
end
121+
122+
end
123+
107124
def list(%{system_identity: system_identity}, options) do
108125
system_id = Accounts.id_from_system_identity(system_identity, :system)
109126

@@ -320,6 +337,12 @@ defmodule OctoconDiscord.Commands.Alter do
320337
]
321338
|> Utils.add_show_option()
322339
},
340+
%{
341+
name: "random",
342+
description: "Views a random alter.",
343+
type: :sub_command,
344+
options: Utils.add_show_option([])
345+
},
323346
%{
324347
name: "list",
325348
description: "Lists all of your alters.",

0 commit comments

Comments
 (0)