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

Commit

Permalink
[WIP] Shard bot
Browse files Browse the repository at this point in the history
  • Loading branch information
greenbigfrog committed Dec 30, 2018
1 parent 4830f4a commit 56dd01e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docker/Dockerfile.launcher
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ RUN apt-get update \
RUN adduser --disabled-password --gecos "" docker
USER docker
COPY --from=0 /src/launcher /
ENTRYPOINT ["/launcher", "config.json"]
CMD ["/launcher", "config.json"]
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ shards:
github: greenbigfrog/raven.cr
commit: 1060c969371e4fb4f03f75f048b9d90c0b8f77ef

shardmaster:
github: greenbigfrog/shardmaster
commit: 4a07b295dd496b969a0dfbcaa1905d9aaf6d9e6e

3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dependencies:
github: kemalcr/kemal
bot_list:
github: z64/bot_list
shardmaster:
github: greenbigfrog/shardmaster
branch: master

development_dependencies:
ameba:
Expand Down
7 changes: 5 additions & 2 deletions src/discordtipbot.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "discordcr"
require "big"
require "big/json"
require "discordcr-middleware"
require "shardmaster/peer"
require "./cache"
require "./discordtipbot/**"

Expand All @@ -29,17 +30,19 @@ class DiscordTipBot

log.debug("Tipbot network getting started")

shard = Shardmaster::Peer.get_shard

shared_cache = Discord::Cache.new(Discord::Client.new(""))

log.debug("starting forking")
Config.current.each do |name, config|
raven_spawn(name: "#{name} Bot") do
bot = Discord::Client.new(config.discord_token)
bot = Discord::Client.new(config.discord_token, config.discord_client_id, {shard_id: shard[:id], num_shards: shard[:shard_count]})
cache = Discord::Cache.new(bot)
shared_cache.bind(cache)
bot.cache = cache

DiscordBot.new(bot, cache, config, log).run
DiscordBot.new(bot, cache, shard[:id], config, log).run
end
end
log.debug("finished forking")
Expand Down
10 changes: 8 additions & 2 deletions src/discordtipbot/discordbot.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DiscordBot
@unavailable_guilds = Set(UInt64).new
@available_guilds = Set(UInt64).new

def initialize(@bot : Discord::Client, @cache : Discord::Cache, @config : Config, @log : Logger)
def initialize(@bot : Discord::Client, @cache : Discord::Cache, @shard_id : Int32, @config : Config, @log : Logger)
@log.debug("#{@config.coinname_short}: starting bot: #{@config.coinname_full}")
@tip = TipBot.new(@config, @log)
@active_users_cache = ActivityCache.new(10.minutes)
Expand All @@ -35,7 +35,7 @@ class DiscordBot
typing = TriggerTyping.new

@bot.on_message_create(error, config, Command.new("ping"),
rl, Ping.new)
rl, Ping.new(@shard_id))
@bot.on_message_create(error, config, Command.new("withdraw"),
rl, Withdraw.new(@tip, @config))
@bot.on_message_create(error, config, Command.new(["deposit", "address"]),
Expand Down Expand Up @@ -264,6 +264,12 @@ class DiscordBot
@cache.cache(Discord::User.new(presence.user)) if presence.user.full?
end

raven_spawn do
Discord.every(1.minutes) do
Shardmaster::Peer.post_ping(@shard_id)
end
end

# receive wallet transactions and insert into coin_transactions
raven_spawn do
server = HTTP::Server.new do |context|
Expand Down
13 changes: 12 additions & 1 deletion src/discordtipbot/discordbot/ping.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Ping
def initialize(@shard_id : Int32)
end

def call(payload, ctx)
client = ctx[Discord::Client]
latency = Discord::EmbedField.new("Latency", "#{(Time.utc_now - payload.timestamp).total_milliseconds} ms")
Expand All @@ -7,8 +10,16 @@ class Ping
return unless m.is_a?(Discord::Message)
roundtrip = Discord::EmbedField.new("Roundtrip", "#{(Time.utc_now - payload.timestamp).total_milliseconds} ms")
footer = Discord::EmbedFooter.new("Processing of the whole command took #{(Time.utc_now - ctx[Command].time).total_milliseconds} ms")

string = String.build do |io|
io.puts "Pong! Deatils below are for **Shard #{@shard_id}**"
Shardmaster::Peer.get_ping.each do |x|
io.puts x
end
end

client.edit_message(m.channel_id, m.id,
"Pong!",
string,
Discord::Embed.new(footer: footer,
fields: [latency, processing_time, roundtrip]))
yield
Expand Down

0 comments on commit 56dd01e

Please sign in to comment.