From 0896eb9ffb21480f46cebbe1eeb5b5065cee72c9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Bersier?= <loic.bersier1@gmail.com>
Date: Sat, 12 Oct 2024 14:38:08 +0200
Subject: [PATCH] Initial test

---
 README.md                   | 22 +++++++++++++++--
 lib/commands.ex             | 47 +++++++++++++++++++++++++++++++++++++
 lib/haha_yes.ex             | 12 ++++++++++
 lib/haha_yes/application.ex | 21 +++++++++++++++++
 mix.exs                     | 31 ++++++++++++++++++++++++
 mix.lock                    | 16 +++++++++++++
 test/haha_yes_test.exs      |  8 +++++++
 test/test_helper.exs        |  1 +
 8 files changed, 156 insertions(+), 2 deletions(-)
 create mode 100644 lib/commands.ex
 create mode 100644 lib/haha_yes.ex
 create mode 100644 lib/haha_yes/application.ex
 create mode 100644 mix.exs
 create mode 100644 mix.lock
 create mode 100644 test/haha_yes_test.exs
 create mode 100644 test/test_helper.exs

diff --git a/README.md b/README.md
index bbbe9e9..3ac58c7 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,21 @@
-# Discord_Elixir_test
+# HahaYes
+
+**TODO: Add description**
+
+## Installation
+
+If [available in Hex](https://hex.pm/docs/publish), the package can be installed
+by adding `haha_yes` to your list of dependencies in `mix.exs`:
+
+```elixir
+def deps do
+  [
+    {:haha_yes, "~> 0.1.0"}
+  ]
+end
+```
+
+Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
+and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
+be found at <https://hexdocs.pm/haha_yes>.
 
-Toying around with Elixir and Nostrum
\ No newline at end of file
diff --git a/lib/commands.ex b/lib/commands.ex
new file mode 100644
index 0000000..3d0c317
--- /dev/null
+++ b/lib/commands.ex
@@ -0,0 +1,47 @@
+defmodule HahaCommands 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 ping(msg) do
+    Api.create_message(msg.channel_id, "Pong!")
+  end
+
+  @doc """
+  Download the video sent by the user at 480p max.
+
+  ## Parameters
+
+    - url: String that represents the URL to download a video from.
+
+  ## Examples
+
+    User: h3h3 download https://x.com/i/status/1844807680373768595
+
+    Bot: <video file>
+
+    User: h3h3 download https://www.youtube.com/watch?v=ryS7TS_J7KA
+
+    Bot: <video file>
+  """
+  def download(msg) do
+    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"];
+
+    System.cmd("yt-dlp", opt)
+    Api.delete_message(msg.channel_id, msg.id)
+    Api.create_message(msg.channel_id, files: ["#{System.tmp_dir}/test.mp4"])
+  end
+end
diff --git a/lib/haha_yes.ex b/lib/haha_yes.ex
new file mode 100644
index 0000000..6c1a606
--- /dev/null
+++ b/lib/haha_yes.ex
@@ -0,0 +1,12 @@
+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
diff --git a/lib/haha_yes/application.ex b/lib/haha_yes/application.ex
new file mode 100644
index 0000000..fce7547
--- /dev/null
+++ b/lib/haha_yes/application.ex
@@ -0,0 +1,21 @@
+defmodule HahaYes.Application do
+  # See https://hexdocs.pm/elixir/Application.html
+  # for more information on OTP Applications
+  @moduledoc false
+
+  use Application
+
+  @impl true
+  def start(_type, _args) do
+    children = [
+      HahaConsumer
+      # Starts a worker by calling: HahaYes.Worker.start_link(arg)
+      # {HahaYes.Worker, arg}
+    ]
+
+    # See https://hexdocs.pm/elixir/Supervisor.html
+    # for other strategies and supported options
+    opts = [strategy: :one_for_one, name: HahaYes.Supervisor]
+    Supervisor.start_link(children, opts)
+  end
+end
diff --git a/mix.exs b/mix.exs
new file mode 100644
index 0000000..757ec8d
--- /dev/null
+++ b/mix.exs
@@ -0,0 +1,31 @@
+defmodule HahaYes.MixProject do
+  use Mix.Project
+
+  def project do
+    [
+      app: :haha_yes,
+      version: "0.1.0",
+      elixir: "~> 1.17",
+      start_permanent: Mix.env() == :prod,
+      deps: deps()
+    ]
+  end
+
+  # Run "mix help compile.app" to learn about applications.
+  def application do
+    [
+      extra_applications: [:logger],
+      mod: {HahaYes.Application, []}
+    ]
+  end
+
+  # Run "mix help deps" to learn about dependencies.
+  defp deps do
+    [
+      {:nostrum, github: "Kraigie/nostrum"},
+      {:ex_doc, "~> 0.21", only: :dev, runtime: false}
+      # {:dep_from_hexpm, "~> 0.3.0"},
+      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
+    ]
+  end
+end
diff --git a/mix.lock b/mix.lock
new file mode 100644
index 0000000..798ec9e
--- /dev/null
+++ b/mix.lock
@@ -0,0 +1,16 @@
+%{
+  "castle": {:hex, :castle, "0.3.0", "47b1a550b2348a6d7e60e43ded1df19dca601ed21ef6f267c3dbb1b3a301fbf5", [:mix], [{:forecastle, "~> 0.1.0", [hex: :forecastle, repo: "hexpm", optional: false]}], "hexpm", "dbdc1c171520c4591101938a3d342dec70d36b7f5b102a5c138098581e35fcef"},
+  "certifi": {:hex, :certifi, "2.13.0", "e52be248590050b2dd33b0bb274b56678f9068e67805dca8aa8b1ccdb016bbf6", [:rebar3], [], "hexpm", "8f3d9533a0f06070afdfd5d596b32e21c6580667a492891851b0e2737bc507a1"},
+  "cowlib": {:hex, :cowlib, "2.13.0", "db8f7505d8332d98ef50a3ef34b34c1afddec7506e4ee4dd4a3a266285d282ca", [:make, :rebar3], [], "hexpm", "e1e1284dc3fc030a64b1ad0d8382ae7e99da46c3246b815318a4b848873800a4"},
+  "earmark_parser": {:hex, :earmark_parser, "1.4.41", "ab34711c9dc6212dda44fcd20ecb87ac3f3fce6f0ca2f28d4a00e4154f8cd599", [:mix], [], "hexpm", "a81a04c7e34b6617c2792e291b5a2e57ab316365c2644ddc553bb9ed863ebefa"},
+  "ex_doc": {:hex, :ex_doc, "0.34.2", "13eedf3844ccdce25cfd837b99bea9ad92c4e511233199440488d217c92571e8", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "5ce5f16b41208a50106afed3de6a2ed34f4acfd65715b82a0b84b49d995f95c1"},
+  "forecastle": {:hex, :forecastle, "0.1.2", "f8dab08962c7a33010ebd39182513129f17b8814aa16fa453ddd536040882daf", [:mix], [], "hexpm", "8efaeb2e7d0fa24c605605e42562e2dbb0ffd11dc1dd99ef77d78884536ce501"},
+  "gun": {:hex, :gun, "2.1.0", "b4e4cbbf3026d21981c447e9e7ca856766046eff693720ba43114d7f5de36e87", [:make, :rebar3], [{:cowlib, "2.13.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "52fc7fc246bfc3b00e01aea1c2854c70a366348574ab50c57dfe796d24a0101d"},
+  "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
+  "makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
+  "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
+  "makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},
+  "mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
+  "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
+  "nostrum": {:git, "https://github.com/Kraigie/nostrum.git", "1628880a3e6e45cacc53d2383eb110ff023050e1", []},
+}
diff --git a/test/haha_yes_test.exs b/test/haha_yes_test.exs
new file mode 100644
index 0000000..15f2768
--- /dev/null
+++ b/test/haha_yes_test.exs
@@ -0,0 +1,8 @@
+defmodule HahaYesTest do
+  use ExUnit.Case
+  doctest HahaYes
+
+  test "greets the world" do
+    assert HahaYes.hello() == :world
+  end
+end
diff --git a/test/test_helper.exs b/test/test_helper.exs
new file mode 100644
index 0000000..869559e
--- /dev/null
+++ b/test/test_helper.exs
@@ -0,0 +1 @@
+ExUnit.start()