Compare commits

...

3 commits

Author SHA1 Message Date
77ebcf9d7b Put events in their own folder 2024-10-12 17:06:08 +02:00
779f99a5f3 Move commands to their own files 2024-10-12 17:05:40 +02:00
ed8b78385c Show example config 2024-10-12 17:05:23 +02:00
10 changed files with 96 additions and 30 deletions

2
.gitignore vendored
View file

@ -7,6 +7,6 @@
erl_crash.dump erl_crash.dump
*.ez *.ez
*.beam *.beam
/config/ /config/config.exs
.elixir_ls/ .elixir_ls/

11
config/config.example.exs Normal file
View file

@ -0,0 +1,11 @@
import Config
config :nostrum,
token: "bot token goes here",
gateway_intents: [
:guilds,
:guild_members,
:guild_messages,
:message_content,
:guild_message_reactions
]

View file

@ -1,24 +1,10 @@
defmodule HahaCommands do defmodule HahaYes.Commands.Download do
@moduledoc """ @moduledoc """
Contain all the function for each commands Contain all the function for each commands
""" """
alias Nostrum.Api alias Nostrum.Api
@doc """
Reply with a simple "Pong!"
## Example
User: h3h3 ping
Bot: Pong!
"""
def ping(msg) do
Api.create_message(msg.channel_id, "Pong!")
end
@doc """ @doc """
Download the video sent by the user at 480p max. Download the video sent by the user at 480p max.
@ -36,7 +22,7 @@ defmodule HahaCommands do
Bot: <video file> Bot: <video file>
""" """
def download(msg) do def execute(msg) do
arg = String.replace(msg.content, "h3h3 download ", "") arg = String.replace(msg.content, "h3h3 download ", "")
opt = ["-f", "bestvideo[height<=?480]+bestaudio/best", arg, "-o", "#{System.tmp_dir}/test.mp4", "--force-overwrites", "--playlist-reverse", "--no-playlist", "--remux-video=mp4/webm/mov", "--no-warnings"]; opt = ["-f", "bestvideo[height<=?480]+bestaudio/best", arg, "-o", "#{System.tmp_dir}/test.mp4", "--force-overwrites", "--playlist-reverse", "--no-playlist", "--remux-video=mp4/webm/mov", "--no-warnings"];

21
lib/HahaCommands/ping.ex Normal file
View file

@ -0,0 +1,21 @@
defmodule HahaYes.Commands.Ping do
@moduledoc """
Contain all the function for each commands
"""
alias Nostrum.Api
@doc """
Reply with a simple "Pong!"
## Example
User: h3h3 ping
Bot: Pong!
"""
def execute(msg) do
Api.create_message(msg.channel_id, "Pong!")
end
end

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 @impl true
def start(_type, _args) do def start(_type, _args) do
children = [ children = [
HahaConsumer HahaYes.Events.MessagesConsumer,
HahaYes.Events.ReadyConsumer,
HahaYes.Events.AddReactionsConsumer,
HahaYes.Events.RemoveReactionsConsumer
# Starts a worker by calling: HahaYes.Worker.start_link(arg) # Starts a worker by calling: HahaYes.Worker.start_link(arg)
# {HahaYes.Worker, arg} # {HahaYes.Worker, arg}
] ]