bankerbot-rb/coin.rb

141 lines
3.8 KiB
Ruby
Raw Normal View History

2022-10-07 15:12:06 +00:00
require 'dotenv/load'
require 'yaml'
require 'discordrb'
MAX_COINS_PER_DAY = 3
2022-10-07 15:12:06 +00:00
COIN_TIMEOUT = 15
SCOREBOARD = './scoreboard.yml'
2022-11-04 13:31:32 +00:00
DUMBASS = 521596582962003968
DUMBASS_BOT = 1034966228755820575
2022-11-07 12:20:44 +00:00
COIN_EMOJI = '🪙'
2022-11-04 13:31:32 +00:00
2022-10-07 15:12:06 +00:00
coin_state_active = false
coins_offered_today = 0
2022-10-07 15:12:06 +00:00
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)
2022-10-07 15:12:06 +00:00
end
def load_scoreboard()
YAML::load_file(SCOREBOARD) || {}
end
def get_coin_count(user)
scoreboard = load_scoreboard()
2022-10-07 15:12:06 +00:00
scoreboard[user] || 0
end
2022-10-11 14:14:09 +00:00
def increment_coin(user, amount=1)
2022-10-11 11:24:06 +00:00
scoreboard = load_scoreboard()
2022-10-11 14:14:09 +00:00
coins = get_coin_count(user) + amount
2022-10-07 15:12:06 +00:00
scoreboard[user] = coins
File.write(SCOREBOARD, scoreboard.to_yaml)
2022-10-11 14:14:09 +00:00
return coins
2022-10-07 15:12:06 +00:00
end
2022-10-11 11:24:06 +00:00
def empty_coin(user)
scoreboard = load_scoreboard()
scoreboard[user] = 0
File.write(SCOREBOARD, scoreboard.to_yaml)
end
2022-10-07 15:12:06 +00:00
def plural(string, count)
2022-10-07 18:50:01 +00:00
"#{count} #{string}#{count != 1 ? 's' : ''}"
2022-10-07 15:12:06 +00:00
end
def clean_name(input)
input.tr '@', ''
end
2022-10-11 11:24:06 +00:00
bot.message(in: Integer(ENV['BOT_CHANNEL'])) do |event|
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! #{clean_name event.author.display_name}, your coin balance has now been emptied. Let that be a lesson for next time!"
2022-10-11 11:24:06 +00:00
empty_coin(event.author.id)
end
end
bot.message(content: 'GET COIN' ) do |event|
2022-10-07 15:12:06 +00:00
# update score
if coin_state_active
2022-10-27 22:15:20 +00:00
if event.author.bot_account?
event.respond "Bots don't get coins!"
end
coin_state_active = false
2022-10-27 22:15:20 +00:00
if(rand(100) == 47)
2022-10-11 14:14:09 +00:00
hyper_coin_amount = rand(100)
coins = increment_coin(event.author.id, hyper_coin_amount)
event.respond "What astounding luck!!! #{clean_name 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!"
2022-10-11 14:14:09 +00:00
else
coins = increment_coin(event.author.id)
event.respond "Nice catch, #{clean_name event.author.display_name}! You have #{plural('coin', coins)} now!"
2022-10-11 14:14:09 +00:00
end
2022-10-07 15:12:06 +00:00
else
event.respond "No coins for you, #{clean_name event.author.display_name}!"
2022-10-07 15:12:06 +00:00
end
end
bot.message(with_text: '/coins') do |event|
coins = get_coin_count(event.author.id)
event.respond "#{clean_name event.author.display_name}, you have #{plural('coin', coins)}!"
2022-10-07 15:12:06 +00:00
end
2022-11-04 13:31:32 +00:00
bot.message(from: DUMBASS_BOT) do |event|
next unless event.message.content.scan(/(?=no swearing)/).count > 0
coins = increment_coin(DUMBASS, -1)
event.respond "<@#{DUMBASS}>, you have been penalized 1 coin for the grossly negligent behavior of your Intellectual Offspring! You now have #{plural('coin', coins)}."
end
2022-11-07 12:20:44 +00:00
bot.reaction_add do |event|
next unless event.emoji.name == COIN_EMOJI
reactor_coins = get_coin_count(event.user.id)
if reactor_coins == 0
event.respond("<@#{event.user.id}>, you're a poor shmuck!")
next
end
if event.message.author == event.user
event.respond("<@#{event.user.id}>, you can't give coins to yourself!")
next
end
increment_coin(event.message.author.id)
increment_coin(event.user.id, -1)
event.respond "<@#{event.message.author.id}>, you have been given a coin by <@#{event.user.id}>!"
end
2022-10-07 15:12:06 +00:00
bot.run(true)
2022-10-27 22:15:20 +00:00
loop do
# sleep random amount of time between 1 and 4 hours
sleep(get_random_hours(1, 4))
2022-10-10 19:47:09 +00:00
# activate coin state
coin_state_active = true
2022-10-07 15:12:06 +00:00
# send coin GIF
bot.send_file(ENV['BOT_CHANNEL'], File.open('./coin.gif', 'r'))
coins_offered_today += 1
2022-10-07 15:12:06 +00:00
# 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
2022-10-07 15:12:06 +00:00
end
bot.join()