diff options
| author | Adelyn Breedlove <[email protected]> | 2019-03-02 18:30:46 -0700 |
|---|---|---|
| committer | Adelyn Breedlove <[email protected]> | 2019-03-02 18:30:46 -0700 |
| commit | 6163027a715b31d87e1f8e4fde8f7f3b4db2bc42 (patch) | |
| tree | d84b6f956fd43d8a3bb2dff3a1bde9e27dcc1cc2 /bin | |
| parent | Style improvements who dis (diff) | |
| download | disml-6163027a715b31d87e1f8e4fde8f7f3b4db2bc42.tar.xz disml-6163027a715b31d87e1f8e4fde8f7f3b4db2bc42.zip | |
Initial Lwt changes. Successfully compiles
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/bot.ml | 37 | ||||
| -rw-r--r-- | bin/commands.ml | 108 | ||||
| -rw-r--r-- | bin/dune | 2 |
3 files changed, 78 insertions, 69 deletions
@@ -1,8 +1,9 @@ -open Async
-open Core
+open Lwt.Infix
open Disml
open Models
+module String = Base.String
+
(* Define a function to handle message_create *)
let check_command (message:Message.t) =
(* Simple example of command parsing. *)
@@ -16,37 +17,39 @@ let check_command (message:Message.t) = | "!embed" -> Commands.embed message rest
| "!status" -> Commands.status message rest
| "!echo" -> Commands.echo message rest
- | "!cache" -> Commands.cache message rest
+ (* | "!cache" -> Commands.cache message rest *)
| "!shutdown" -> Commands.shutdown message rest
| "!rgm" -> Commands.request_members message rest
- | "!new" -> Commands.new_guild message rest
- | "!delall" -> Commands.delete_guilds message rest
- | "!roletest" -> Commands.role_test message rest
- | "!perms" -> Commands.check_permissions message rest
- | _ -> () (* Fallback case, no matched command. *)
+ (* | "!new" -> Commands.new_guild message rest *)
+ (* | "!delall" -> Commands.delete_guilds message rest *)
+ (* | "!roletest" -> Commands.role_test message rest *)
+ (* | "!perms" -> Commands.check_permissions message rest *)
+ | _ -> Lwt.return_unit (* Fallback case, no matched command. *)
(* Example logs setup *)
let setup_logger () =
- Logs.set_reporter (Logs_fmt.reporter ());
- Logs.set_level ~all:true (Some Logs.Debug)
+ let open Logs in
+ set_reporter (format_reporter ());
+ set_level (Some Info)
let main () =
- setup_logger ();
(* Register some event handlers *)
Client.message_create := check_command;
- Client.ready := (fun ready -> Logs.info (fun m -> m "Logged in as %s" (User.tag ready.user)));
- Client.guild_create := (fun guild -> Logs.info (fun m -> m "Joined guild %s" guild.name));
- Client.guild_delete := (fun {id;_} -> let `Guild_id id = id in Logs.info (fun m -> m "Left guild %d" id));
+ Client.ready := (fun ready -> Logs_lwt.info (fun m -> m "Logged in as %s" (User.tag ready.user)));
+ Client.guild_create := (fun guild -> Logs_lwt.info (fun m -> m "Joined guild %s" guild.name));
+ Client.guild_delete := (fun {id;_} -> let `Guild_id id = id in Logs_lwt.info (fun m -> m "Left guild %d" id));
(* Pull token from env var. It is not recommended to hardcode your token. *)
- let token = match Sys.getenv "DISCORD_TOKEN" with
+ let token = match Stdlib.Sys.getenv_opt "DISCORD_TOKEN" with
| Some t -> t
| None -> failwith "No token in env"
in
(* Start client. *)
Client.start ~large:250 ~compress:true token
(* Fill that ivar once its done *)
- >>> Ivar.fill Commands.client
+ >|= Lwt.wakeup_later Commands.r_client >>= fun _ ->
+ fst (Lwt.wait ())
(* Lastly, we have to register this to the Async Scheduler for anything to work *)
let _ =
- Scheduler.go_main ~main ()
+ setup_logger ();
+ Lwt_main.run @@ main ()
diff --git a/bin/commands.ml b/bin/commands.ml index afdc40a..a278b5d 100644 --- a/bin/commands.ml +++ b/bin/commands.ml @@ -1,34 +1,39 @@ -open Async -open Core +open Lwt.Infix open Disml open Models +module Option = Base.Option +module Error = Base.Error +module List = Base.List +module Int = Base.Int + (* Client object will be stored here after creation. *) -let client = Ivar.create () +let client, r_client = Lwt.wait () (* Example ping command with REST round trip time edited into the response. *) let ping message _args = - Message.reply message "Pong!" >>> function - | Ok message -> - let diff = Time.diff (Time.now ()) (Time.of_string message.timestamp) in - Message.set_content message (Printf.sprintf "Pong! `%d ms`" (Time.Span.to_ms diff |> Float.abs |> Float.to_int)) >>> ignore - | Error e -> Error.raise e + Message.reply message "Pong!" >>= function + | Ok _message -> Lwt.return_unit + (* let diff = Time.diff (Time.now ()) (Time.of_string message.timestamp) in + Message.set_content message (Printf.sprintf "Pong! `%d ms`" (Time.Span.to_ms diff |> Float.abs |> Float.to_int)) *) + | Error e -> Error.(of_string e |> raise) (* Send a list of consecutive integers of N size with 1 message per list item. *) let spam message args = let count = Option.((List.hd args >>| Int.of_string) |> value ~default:0) in List.range 0 count - |> List.iter ~f:(fun i -> Message.reply message (string_of_int i) >>> ignore) + |> List.iter ~f:(fun i -> Lwt.async (fun () -> Message.reply message (string_of_int i))) + |> Lwt.return (* Send a list of consecutive integers of N size in a single message. *) let list message args = let count = Option.((List.hd args >>| Int.of_string) |> value ~default:0) in let list = List.range 0 count |> List.sexp_of_t Int.sexp_of_t - |> Sexp.to_string_hum in - Message.reply message list >>> function + |> Sexplib.Sexp.to_string_hum in + Message.reply message list >|= function | Ok msg -> print_endline msg.content - | Error err -> print_endline (Error.to_string_hum err) + | Error err -> print_endline err (* Example of setting pretty much everything in an embed using the Embed module builders *) let embed message _args = @@ -37,7 +42,7 @@ let embed message _args = |> title "Foo" |> description "Bar" |> url "https://gitlab.com/Mishio595/disml" - |> timestamp Time.(now () |> to_string_iso8601_basic ~zone:Time.Zone.utc) + (* |> timestamp Time.(now () |> to_string_iso8601_basic ~zone:Time.Zone.utc) *) |> colour 0xff |> footer (fun f -> footer_text "boop" f) |> image image_url @@ -50,42 +55,42 @@ let embed message _args = |> field ("field 2", "test", true) |> field ("field 1", "test", true) ) in - Message.reply_with ~embed message >>> ignore + Message.reply_with ~embed message >|= ignore (* Set the status of all shards to a given string. *) let status message args = let name = List.fold ~init:"" ~f:(fun a v -> a ^ " " ^ v) args in - Ivar.read client >>> fun client -> + client >>= fun client -> Client.set_status ~name client - >>> fun _ -> - Message.reply message "Updated status" >>> ignore + >>= fun _ -> + Message.reply message "Updated status" >|= ignore (* Fetches a message by ID in the current channel, defaulting to the sent message, and prints in s-expr form. *) let echo (message:Message.t) args = let `Message_id id = message.id in let id = Option.((List.hd args >>| Int.of_string) |> value ~default:id) in - Channel_id.get_message ~id message.channel_id >>> function + Channel_id.get_message ~id message.channel_id >>= function | Ok msg -> - let str = Message.sexp_of_t msg |> Sexp.to_string_hum in - Message.reply message (Printf.sprintf "```lisp\n%s```" str) >>> ignore - | _ -> () + let str = Message.sexp_of_t msg |> Sexplib.Sexp.to_string_hum in + Message.reply message (Printf.sprintf "```lisp\n%s```" str) >|= ignore + | _ -> Lwt.return_unit (* Output cache counts as a a basic embed. *) -let cache message _args = +(* let cache message _args = let module C = Cache.ChannelMap in let module G = Cache.GuildMap in let module U = Cache.UserMap in let cache = Mvar.peek_exn Cache.cache in - let gc = G.length cache.guilds in - let ug = G.length cache.unavailable_guilds in - let tc = C.length cache.text_channels in - let vc = C.length cache.voice_channels in - let cs = C.length cache.categories in - let gr = C.length cache.groups in - let pr = C.length cache.private_channels in - let uc = U.length cache.users in - let pre = U.length cache.presences in - let user = Option.(value ~default:"None" (cache.user >>| User.tag)) in + let gc = G.cardinal cache.guilds in + let ug = G.cardinal cache.unavailable_guilds in + let tc = C.cardinal cache.text_channels in + let vc = C.cardinal cache.voice_channels in + let cs = C.cardinal cache.categories in + let gr = C.cardinal cache.groups in + let pr = C.cardinal cache.private_channels in + let uc = U.cardinal cache.users in + let pre = U.cardinal cache.presences in + let user = Option.(value ~default:"None" (map cache.user ~f:User.tag)) in let embed = Embed.(default |> description (Printf.sprintf "Guilds: %d\nUnavailable Guilds: %d\n\ @@ -94,32 +99,33 @@ let cache message _args = Private Channels: %d\nUsers: %d\n\ Presences: %d\nCurrent User: %s" gc ug tc vc cs gr pr uc pre user)) in - Message.reply_with ~embed message >>> ignore + Message.reply_with ~embed message >|= ignore *) (* Issue a shutdown to all shards, then exits the process. *) let shutdown (message:Message.t) _args = if message.author.id = `User_id 242675474927583232 then - Ivar.read client >>= Client.shutdown_all ~restart:false >>> fun _ -> - exit 0 + client >>= Client.shutdown_all ~restart:false >|= fun _ -> + exit 0 + else Lwt.return_unit (* Request guild members to be sent over the gateway for the guild the command is run in. This will cause multiple GUILD_MEMBERS_CHUNK events. *) let request_members (message:Message.t) _args = - Ivar.read client >>> fun client -> + client >>= fun client -> match message.guild_id with - | Some guild -> Client.request_guild_members ~guild client >>> ignore - | None -> () + | Some guild -> Client.request_guild_members ~guild client >|= ignore + | None -> Lwt.return_unit (* Creates a guild named testing or what the user provided *) -let new_guild message args = +(* let new_guild message args = let name = List.fold ~init:"" ~f:(fun a v -> a ^ " " ^ v) args in let name = if String.length name = 0 then "Testing" else name in Guild.create [ "name", `String name ] >>= begin function | Ok g -> Message.reply message (Printf.sprintf "Created guild %s" g.name) - | Error e -> Message.reply message (Printf.sprintf "Failed to create guild. Error: %s" (Error.to_string_hum e)) - end >>> ignore + | Error e -> Message.reply message (Printf.sprintf "Failed to create guild. Error: %s" e) + end *) (* Deletes all guilds made by the bot *) -let delete_guilds message _args = +(* let delete_guilds message _args = let cache = Mvar.peek_exn Cache.cache in let uid = match cache.user with | Some u -> u.id @@ -131,9 +137,9 @@ let delete_guilds message _args = | Ok () -> res := Printf.sprintf "%s\nDeleted %s" !res g.name | Error _ -> ()) |> to_alist) |> List.map ~f:(snd) in Deferred.all all >>= (fun _ -> - Message.reply message !res) >>> ignore + Message.reply message !res) >|= ignore *) -let role_test (message:Message.t) args = +(* let role_test (message:Message.t) args = let exception Member_not_found in let cache = Mvar.peek_exn Cache.cache in let name = List.fold ~init:"" ~f:(fun a v -> a ^ " " ^ v) args in @@ -176,10 +182,10 @@ let role_test (message:Message.t) args = with | Member_not_found -> Message.reply message "Error: Member not found" | exn -> Message.reply message (Printf.sprintf "Error: %s" Error.(of_exn exn |> to_string_hum)) - end >>> ignore - | None -> () + end >|= ignore + | None -> () *) -let check_permissions (message:Message.t) _args = +(* let check_permissions (message:Message.t) _args = let cache = Mvar.peek_exn Cache.cache in let empty = Permissions.empty in let permissions = match message.guild_id, message.member with @@ -194,7 +200,7 @@ let check_permissions (message:Message.t) _args = | _ -> empty in let allow, deny = match message.member with | Some m -> - begin match Cache.text_channel cache message.channel_id with + begin match Cache.text_channel message.channel_id cache with | Some c -> List.fold c.permission_overwrites ~init:(empty, empty) ~f:(fun (a,d) {allow; deny; id; kind} -> let `User_id uid = message.author.id in @@ -207,10 +213,10 @@ let check_permissions (message:Message.t) _args = | None -> empty, empty in let g_perms = Permissions.elements permissions |> List.sexp_of_t Permissions.sexp_of_elt - |> Sexp.to_string_hum in + |> Sexplib.Sexp.to_string_hum in let c_perms = Permissions.(union permissions allow |> diff deny |> elements) |> List.sexp_of_t Permissions.sexp_of_elt - |> Sexp.to_string_hum in - Message.reply message (Printf.sprintf "Global Permissions: %s\nChannel Permissions: %s" g_perms c_perms) >>> ignore
\ No newline at end of file + |> Sexplib.Sexp.to_string_hum in + Message.reply message (Printf.sprintf "Global Permissions: %s\nChannel Permissions: %s" g_perms c_perms) >|= ignore *)
\ No newline at end of file @@ -1,5 +1,5 @@ (executable (name bot) (modules bot commands) - (libraries core async_ssl disml) + (libraries lwt disml base) )
\ No newline at end of file |