Skip to content

Commit

Permalink
add copypasta interaction commands
Browse files Browse the repository at this point in the history
  • Loading branch information
THE9rtyt committed Dec 17, 2023
1 parent 467549b commit 781251f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/miata_bot_discord/interactions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,27 @@ defmodule MiataBotDiscord.Interactions do
]
}
end

def pastabot() do
%{
name: "pasta",
description: "MiataBot copy pastas",
options: [
%{
name: "modify",
description: "modify the pasta",
options: [
%{description: "add a copypasta", name: "add", type: 3}
],
type: 1
},
%{
name: "random",
description: "get a random copypasta",
options: [],
type: 1
}
]
}
end
end
66 changes: 66 additions & 0 deletions lib/miata_bot_discord/listeners/memes_channel_listener.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule MiataBotDiscord.MemesChannelListener do
use Quarrel.Listener
use Bitwise
require Logger

alias MiataBot.{Repo, CopyPasta}
Expand All @@ -8,6 +9,8 @@ defmodule MiataBotDiscord.MemesChannelListener do
@timeout 60_000
@max 5

@admin_role 643_958_189_460_553_729

@impl GenServer
def init(state) do
{:ok,
Expand Down Expand Up @@ -40,6 +43,69 @@ defmodule MiataBotDiscord.MemesChannelListener do
end
end

def handle_interaction_create(
iaction = %Interaction{
data: %{
name: "pasta",
options: [
%{name: "random", type: 1, options: []}
]
}
},
state
) do
response = %{
type: 4,
data: %{
content: "#{Repo.one(from cp in CopyPasta, order_by: fragment("RANDOM()"), limit: 1, select: cp.content)}"
}
}

create_interaction_response(iaction, response)
end

def handle_interaction_create(
iaction = %Interaction{
member: %{
user_id: id,
roles: roles
},
data: %{
name: "pasta",
options: [
%{
name: "modify",
type: 1,
options: [%{name: "add", type: 3, value: copypasta}]
}
]
}
},
state
) do
if Enum.member?(roles, @admin_role) do
case MiataBot.Repo.insert(%MiataBot.CopyPasta{content: copypasta, created_by_discord_id: id}) do
{:ok, _} ->
response = %{type: 4, data: %{content: "successfully added pasta! \"#{copypasta}\""}}
create_interaction_response(iaction, response)

{:error, reason} ->
response = %{type: 4, data: %{content: "failed to add copy pasta: #{inspect(reason)}"}}
create_interaction_response(iaction, response)
end
else
response = %{type: 4, data: %{content: "<:sorrybuddy:1137121335731028049>"}}
create_interaction_response(iaction, response)
end

{:noreply, state}
end

def handle_interaction_create(interaction, state) do
# Logger.warn("unhandled interaction: #{inspect(interaction)}")
{:noreply, state}
end

def handle_message_create(_, state) do
{:noreply, state}
end
Expand Down

0 comments on commit 781251f

Please sign in to comment.