offer coin up to a max, between 1 and 4 hours

This commit is contained in:
mud mud 2022-10-10 09:40:47 +02:00
parent 6b586596ae
commit ed0f5d33e7
Signed by: mud
GPG Key ID: D29C9385CC54F61C

26
coin.rb
View File

@ -2,9 +2,12 @@ require 'dotenv/load'
require 'yaml' require 'yaml'
require 'discordrb' require 'discordrb'
MAX_COINS_PER_DAY = 3
COIN_TIMEOUT = 15 COIN_TIMEOUT = 15
SCOREBOARD = './scoreboard.yml' SCOREBOARD = './scoreboard.yml'
coin_state_active = false coin_state_active = false
coins_offered_today = 0
bot = Discordrb::Bot.new token: ENV['DISCORD_TOKEN'] bot = Discordrb::Bot.new token: ENV['DISCORD_TOKEN']
@ -13,9 +16,10 @@ def get_seconds_to_midnight(time)
return t2 - time return t2 - time
end end
def get_random_duration(time) def get_random_hours(min, max)
max = get_seconds_to_midnight(time) min_hours = min * 60 * 60
return rand(0..max) max_hours = max * 60* 60
return rand(min_hours..max_hours)
end end
def load_scoreboard() def load_scoreboard()
@ -62,14 +66,20 @@ bot.run(true)
loop do loop do
# activate coin state # activate coin state
coin_state_active = true coin_state_active = true
# sleep random amount of time between 1 and 4 hours
sleep(get_random_hours(1, 4))
# send coin GIF # send coin GIF
bot.send_file(ENV['BOT_CHANNEL'], File.open('./coin.gif', 'r')) bot.send_file(ENV['BOT_CHANNEL'], File.open('./coin.gif', 'r'))
coins_offered_today += 1
sleep(1)
# sleep until midnight to reset cycle # if max coins per day has reached, sleep until midnight
sleep(get_seconds_to_midnight(Time.now)) if coins_offered_today >= MAX_COINS_PER_DAY
# sleep(get_seconds_to_midnight(Time.now))
# sleep until random time from now to next midnight coins_offered_today = 0
sleep(get_random_duration(Time.now)) end
end end
bot.join() bot.join()