This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a94ee39
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: dbl | ||
version: 0.1.0 | ||
|
||
authors: | ||
- kandayo <[email protected]> | ||
|
||
dependencies: | ||
raze: | ||
github: samueleaton/raze | ||
|
||
crystal: 0.27.0 | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "./dbl/*" | ||
|
||
module Dbl | ||
VERSION = "0.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Dbl | ||
struct Payload | ||
JSON.mapping( | ||
bot: UInt64, | ||
user: UInt64, | ||
isWeekend: Bool, | ||
type: String | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |