Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Hi git
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasintel committed Nov 25, 2018
0 parents commit a94ee39
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
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
14 changes: 14 additions & 0 deletions README.md
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
```
13 changes: 13 additions & 0 deletions shard.yml
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
5 changes: 5 additions & 0 deletions src/dbl.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "./dbl/*"

module Dbl
VERSION = "0.1.0"
end
29 changes: 29 additions & 0 deletions src/dbl/client.cr
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
10 changes: 10 additions & 0 deletions src/dbl/mappings.cr
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
33 changes: 33 additions & 0 deletions src/dbl/server.cr
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

0 comments on commit a94ee39

Please sign in to comment.