Put events in their own folder

This commit is contained in:
Loïc Bersier 2024-10-12 17:06:08 +02:00
parent 779f99a5f3
commit 77ebcf9d7b
6 changed files with 61 additions and 13 deletions

14
lib/events/AddReaction.ex Normal file
View file

@ -0,0 +1,14 @@
defmodule HahaYes.Events.AddReactionsConsumer do
@moduledoc """
Triggered when reactions are added to a message.
Used to handle starboard.
TODO: Implement starboard
"""
use Nostrum.Consumer
def handle_event({:MESSAGE_REACTION_ADD, _reacts, _ws_state}) do
IO.puts("Someone added reaction.")
end
end

View file

@ -0,0 +1,14 @@
defmodule HahaYes.Events.RemoveReactionsConsumer do
@moduledoc """
Triggered when reactions are added to a message.
Used to handle starboard.
TODO: Implement starboard
"""
use Nostrum.Consumer
def handle_event({:MESSAGE_REACTION_REMOVE, _reacts, _ws_state}) do
IO.puts("Someone removed reaction.")
end
end

19
lib/events/messages.ex Normal file
View file

@ -0,0 +1,19 @@
defmodule HahaYes.Events.MessagesConsumer do
@moduledoc """
Parse messages to execute commands
"""
use Nostrum.Consumer
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) when msg.author.bot != true do
if String.starts_with?(msg.content, "h3h3 ") do
msg.content
|> String.replace("h3h3 ", "")
|> String.split(" ")
|> Enum.at(0)
|> String.downcase()
|> String.capitalize()
|> then(& apply(String.to_atom("#{HahaYes.Commands}.#{&1}"), :execute, [msg]))
end
end
end

10
lib/events/ready.ex Normal file
View file

@ -0,0 +1,10 @@
defmodule HahaYes.Events.ReadyConsumer do
use Nostrum.Consumer
def handle_event({:READY, event, _ws_state}) do
IO.puts("""
#{event.user.username} (#{event.user.id}) is ready!
I am in #{length(event.guilds)} servers!
""")
end
end

View file

@ -1,12 +0,0 @@
defmodule HahaConsumer do
use Nostrum.Consumer
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) when msg.author.bot != true do
if String.starts_with?(msg.content, "h3h3 ") do
msg.content
|> String.replace("h3h3 ", "")
|> String.split(" ")
|> then(& apply(HahaCommands, String.to_atom(Enum.at(&1, 0)), [msg]))
end
end
end

View file

@ -8,7 +8,10 @@ defmodule HahaYes.Application do
@impl true
def start(_type, _args) do
children = [
HahaConsumer
HahaYes.Events.MessagesConsumer,
HahaYes.Events.ReadyConsumer,
HahaYes.Events.AddReactionsConsumer,
HahaYes.Events.RemoveReactionsConsumer
# Starts a worker by calling: HahaYes.Worker.start_link(arg)
# {HahaYes.Worker, arg}
]