GithubHelp home page GithubHelp logo

discordml's Introduction

Discord.ml

A Discord library purely written in OCaml, based on Eio.

Usage

Discord.ml has not yet been published to OPAM, so you need to pin this GitHub repository before using it:

opam pin https://github.com/ushitora-anqou/discordml.git#master

Example

A music bot for YouTube can be written as follows (see example directory for complete code):

type command = Ping | Join | Leave | Play of string (* youtube url *)

let parse_command s = (* ... snip ... *)

let handle_event token env ~sw:_ agent state =
  let open Discord.Event in
  function
  | Dispatch (MESSAGE_CREATE msg) -> (
      let guild_id = Option.get msg.guild_id in
      match parse_command msg.content with
      | Ok Ping ->
          Logs.info (fun m -> m "ping");
          if
            Discord.Rest.make_create_message_param
              ~embeds:[ Discord.Object.make_embed ~description:"pong" () ]
              ()
            |> Discord.Rest.create_message env ~token msg.channel_id
            |> Result.is_error
          then Logs.err (fun m -> m "Failed to send pong");
          state
      | Ok Join ->
          (match
             agent
             |> Discord.Agent.get_voice_states ~guild_id ~user_id:msg.author.id
           with
          | None -> ()
          | Some vstate -> (
              match vstate.Discord.Event.channel_id with
              | None -> ()
              | Some channel_id ->
                  agent |> Discord.Agent.join_channel ~guild_id ~channel_id));
          state
      | Ok Leave ->
          agent |> Discord.Agent.leave_channel ~guild_id;
          state
      | Ok (Play url) ->
          Logs.info (fun m -> m "Playing %s" url);
          agent |> Discord.Agent.play_voice ~guild_id ~src:(`Ytdl url);
          state
      | _ -> state)
  | _ -> state

let () =
  (* ... snip ... *)

  Eio_main.run @@ fun env ->
  Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
  Eio.Switch.run @@ fun sw ->
  let _consumer : _ Discord.Consumer.t =
    Discord.Consumer.start env ~sw ~token ~intents
      (fun () -> ())
      (handle_event token)
  in
  ()

Text !join to join the bot to your voice channel and use !play YOUTUBE-URL to play your favourite music.

Build locally

Set up OPAM first. Then, run the following steps:

opam switch create --no-install . 5.0.0
opam install . --deps-only
dune build example/main.exe

Note

Discord.ml was originally written for a Discord bot Yomer, which reads text messages aloud.

discordml's People

Contributors

ushitora-anqou avatar

Stargazers

Celal Alkadı avatar  avatar Enogad avatar Anri avatar Seonghyun Park avatar David Sancho avatar Jürgen Hötzel avatar Matheus Maia avatar Eyal Chojnowski avatar lentil32 avatar yuki avatar Rintaro Itokawa avatar blank71 avatar Andrejs Agejevs avatar Matthew O'Connor avatar pug avatar  avatar Joseph Price avatar Evgeny avatar alex a avatar Leandro Ostera avatar Fardale avatar Raph avatar Virgile Robles avatar andrii avatar ᴍᴜǫɪᴜ ʜᴀɴ (韩暮秋) avatar Sudha Parimala avatar leonidas avatar Masaki Waga avatar savi2w avatar Sora Morimoto avatar Thomas Gazagnaire avatar Seb Mondet avatar Tim ats avatar Corentin Leruth avatar  avatar Rashid Almheiri avatar Fukuda Yūma avatar Naoki MATSUMOTO avatar

Watchers

James Cloos avatar  avatar  avatar pug avatar

Forkers

hitgif lentil32

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.