forked from mud/bankerbot-rb
107 lines
2.7 KiB
Ruby
107 lines
2.7 KiB
Ruby
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']
|
|
|
|
def get_seconds_to_midnight(time)
|
|
t2 = Time.new(time.year, time.month, time.day + 1)
|
|
return t2 - time
|
|
end
|
|
|
|
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()
|
|
YAML::load_file(SCOREBOARD) || {}
|
|
end
|
|
|
|
def get_coin_count(user)
|
|
scoreboard = load_scoreboard()
|
|
scoreboard[user] || 0
|
|
end
|
|
|
|
def increment_coin(user, amount=1)
|
|
scoreboard = load_scoreboard()
|
|
coins = get_coin_count(user) + amount
|
|
scoreboard[user] = coins
|
|
|
|
File.write(SCOREBOARD, scoreboard.to_yaml)
|
|
|
|
return coins
|
|
end
|
|
|
|
def empty_coin(user)
|
|
scoreboard = load_scoreboard()
|
|
scoreboard[user] = 0
|
|
|
|
File.write(SCOREBOARD, scoreboard.to_yaml)
|
|
end
|
|
|
|
def plural(string, count)
|
|
"#{count} #{string}#{count != 1 ? 's' : ''}"
|
|
end
|
|
|
|
bot.message(in: Integer(ENV['BOT_CHANNEL'])) do |event|
|
|
puts event.message
|
|
if event.message.attachments.count { |x| x.filename.scan(/coin*/).length > 0 } > 0
|
|
event.respond "Oh no! Looks like you tried to post a fradulent coin! #{event.author.display_name}, your coin balance has now been emptied. Let that be a lesson for next time!"
|
|
empty_coin(event.author.id)
|
|
end
|
|
end
|
|
|
|
bot.message(content: 'GET COIN' ) do |event|
|
|
# update score
|
|
if coin_state_active
|
|
if(rand(100) == 47)
|
|
hyper_coin_amount = rand(100)
|
|
coins = increment_coin(event.author.id, hyper_coin_amount)
|
|
event.respond "What astounding luck!!! #{event.author.display_name}, you've just activated HYPER COIN EVENT and have received #{hyper_coin_amount} coins for a grand total of #{coins} coins!"
|
|
else
|
|
coins = increment_coin(event.author.id)
|
|
event.respond "Nice catch, #{event.author.display_name}! You have #{plural('coin', coins)} now!"
|
|
end
|
|
|
|
coin_state_active = false
|
|
|
|
else
|
|
event.respond "No coins for you, #{event.author.display_name}!"
|
|
end
|
|
end
|
|
|
|
bot.message(with_text: '/coins') do |event|
|
|
coins = get_coin_count(event.author.id)
|
|
event.respond "#{event.author.display_name}, you have #{plural('coin', coins)}!"
|
|
end
|
|
|
|
bot.run(true)
|
|
|
|
loop do
|
|
# sleep random amount of time between 1 and 4 hours
|
|
sleep(get_random_hours(1, 4))
|
|
# activate coin state
|
|
coin_state_active = true
|
|
# send coin GIF
|
|
bot.send_file(ENV['BOT_CHANNEL'], File.open('./coin.gif', 'r'))
|
|
coins_offered_today += 1
|
|
|
|
# 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))
|
|
sleep(get_random_hours(0, 11))
|
|
coins_offered_today = 0
|
|
end
|
|
end
|
|
|
|
bot.join()
|