From a94ee39711db83352ef1a9a289412adbdeb2c305 Mon Sep 17 00:00:00 2001 From: kandayo Date: Sun, 25 Nov 2018 03:46:13 -0300 Subject: [PATCH] Hi git --- .gitignore | 9 +++++++++ README.md | 14 ++++++++++++++ shard.yml | 13 +++++++++++++ src/dbl.cr | 5 +++++ src/dbl/client.cr | 29 +++++++++++++++++++++++++++++ src/dbl/mappings.cr | 10 ++++++++++ src/dbl/server.cr | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 shard.yml create mode 100644 src/dbl.cr create mode 100644 src/dbl/client.cr create mode 100644 src/dbl/mappings.cr create mode 100644 src/dbl/server.cr diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e29dae7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/docs/ +/lib/ +/bin/ +/.shards/ +*.dwarf + +# Libraries don't need dependency lock +# Dependencies will be locked in application that uses them +/shard.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..482b731 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# DBL Crystal Library (WIP) + +Crystal API wrapper for discordbots.org. + +## Example + +```crystal +dbl_stats = Dbl::Client.new(ENV["DBL_TOKEN"], client) +dbl = Dbl::Server(ENV["DBL_PASS"]) + +dbl.on_vote do |payload| + ... +end +``` diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..845ce26 --- /dev/null +++ b/shard.yml @@ -0,0 +1,13 @@ +name: dbl +version: 0.1.0 + +authors: + - kandayo + +dependencies: + raze: + github: samueleaton/raze + +crystal: 0.27.0 + +license: MIT diff --git a/src/dbl.cr b/src/dbl.cr new file mode 100644 index 0000000..a437124 --- /dev/null +++ b/src/dbl.cr @@ -0,0 +1,5 @@ +require "./dbl/*" + +module Dbl + VERSION = "0.1.0" +end diff --git a/src/dbl/client.cr b/src/dbl/client.cr new file mode 100644 index 0000000..a4fd505 --- /dev/null +++ b/src/dbl/client.cr @@ -0,0 +1,29 @@ +require "http/client" +require "json" + +module Dbl + class Client + BASE = URI.parse("https://discordbots.org") + + def initialize(authorization_token : String, bot : Discord::Client) + @client = HTTP::Client.new(BASE) + @cache = bot.cache.as(Discord::Cache) + + @client.before_request do |req| + req.headers["Authorization"] = authorization_token + req.headers["content-type"] = "application/json" + end + + bot.on_ready do |payload| + spawn(start) + end + end + + private def start + loop do + @client.post("/api/bots/stats", body: {server_count: @cache.guilds.size}.to_json) + sleep 1800 + end + end + end +end diff --git a/src/dbl/mappings.cr b/src/dbl/mappings.cr new file mode 100644 index 0000000..68aa238 --- /dev/null +++ b/src/dbl/mappings.cr @@ -0,0 +1,10 @@ +module Dbl + struct Payload + JSON.mapping( + bot: UInt64, + user: UInt64, + isWeekend: Bool, + type: String + ) + end +end diff --git a/src/dbl/server.cr b/src/dbl/server.cr new file mode 100644 index 0000000..42aba26 --- /dev/null +++ b/src/dbl/server.cr @@ -0,0 +1,33 @@ +require "raze" + +module Dbl + class Authenticator < Raze::Handler + def initialize(@authorization : String) + end + + def call(ctx, done) + return unless ctx.request.headers["Authorization"] == @authorization + done.call + end + end + + class Server + def initialize(@authorization : String, @port = 3500) + post "/", Authenticator.new(@authorization) do |ctx| + payload = Payload.from_json(ctx.request.body.as(IO)) + @handlers.each(&.call(payload)) + end + + Raze.config.env = "production" + Raze.config.logging = false + Raze.config.port = @port + + spawn Raze.run + end + + @handlers = [] of Payload -> + def on_vote(&handler : Payload ->) + @handlers << handler + end + end +end