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 untrusted user: mud
GPG Key ID: D29C9385CC54F61C

26
coin.rb
View File

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