2022-10-07 15:12:06 +00:00
require 'dotenv/load'
require 'yaml'
require 'discordrb'
2022-10-10 07:40:47 +00:00
2023-02-14 11:22:15 +00:00
MAX_COINS_PER_DAY = 999
2022-10-07 15:12:06 +00:00
COIN_TIMEOUT = 15
SCOREBOARD = './scoreboard.yml'
2022-11-09 14:12:41 +00:00
PASTA = 'You know what? Im done, i am leaving, we were friendly talking about the subject. This crossed the line of "ok killer of joy" to "no fun allowed"'
2022-11-07 12:20:44 +00:00
COIN_EMOJI = '🪙'
2023-02-14 11:21:32 +00:00
BAG_EMOJI = '💰'
2022-11-09 14:12:41 +00:00
2023-05-08 14:57:51 +00:00
COIN_STATE_IDLE = 0
COIN_STATE_ACTIVE = 1
COIN_STATE_MATH = 2
MATH_PROBLEM_COIN_GAIN = 3
$coin_state_active = COIN_STATE_IDLE
2023-02-08 10:30:39 +00:00
$coins_offered_today = 0
2022-10-07 15:12:06 +00:00
2023-05-08 14:57:51 +00:00
$mathproblem_op1 = 0
$mathproblem_op2 = 0
$mathproblem_operator = " + "
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
2022-10-10 07:40:47 +00:00
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
2022-10-11 12:45:15 +00:00
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
2022-10-27 22:10:53 +00:00
def clean_name ( input )
input . tr '@' , ''
end
2023-05-08 14:57:51 +00:00
def get_coin ( event , amt = 1 )
2022-10-07 15:12:06 +00:00
# update score
2023-02-08 10:30:39 +00:00
author = event . methods . include? ( :author ) ? event . author : event . user
if $coin_state_active
if author . bot_account?
2022-10-27 22:15:20 +00:00
event . respond " Bots don't get coins! "
end
2023-05-08 14:57:51 +00:00
$coin_state_active = COIN_STATE_IDLE
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 )
2023-02-08 10:30:39 +00:00
event . respond " What astounding luck!!! #{ clean_name 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
2023-05-08 14:57:51 +00:00
coins = increment_coin ( author . id , amt )
2023-02-08 10:30:39 +00:00
event . respond " Nice catch, #{ clean_name 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
2023-02-08 10:30:39 +00:00
event . respond " No coins for you, #{ clean_name author . display_name } ! "
2022-10-07 15:12:06 +00:00
end
end
2023-02-08 10:30:39 +00:00
def give_coin ( event )
reactor_coins = get_coin_count ( event . user . id )
2023-02-24 23:10:24 +00:00
if reactor_coins < = 0
2023-02-08 10:30:39 +00:00
event . respond ( " Nice gesture, <@ #{ event . user . id } >, but you're broke! To get more coins, type the words `GET COIN` whenever I post the coin event message! But be quick, your friends are trying their best to get it before you do! " )
return
end
if event . message . author == event . user
event . respond ( " <@ #{ event . user . id } >, you can't give coins to yourself! " )
return
end
2023-02-14 11:21:32 +00:00
coin_amount = 0
if event . emoji . name == COIN_EMOJI
coin_amount = 1
elsif event . emoji . name == BAG_EMOJI
2023-02-14 11:24:22 +00:00
coin_amount = 5
2023-02-14 11:21:32 +00:00
end
2023-03-29 12:05:48 +00:00
if reactor_coins < coin_amount
coin_amount = reactor_coins
end
2023-02-14 11:21:32 +00:00
increment_coin ( event . message . author . id , coin_amount )
increment_coin ( event . user . id , - coin_amount )
2023-02-08 10:30:39 +00:00
2023-02-14 11:29:59 +00:00
event . respond " How nice! <@ #{ event . message . author . id } > has received #{ plural ( 'coin' , coin_amount ) } from <@ #{ event . user . id } >! "
2023-02-08 10:30:39 +00:00
end
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! "
empty_coin ( event . author . id )
end
end
bot . message ( content : 'GET COIN' ) do | event |
get_coin ( event )
end
2022-10-07 15:12:06 +00:00
bot . message ( with_text : '/coins' ) do | event |
2022-10-11 12:45:15 +00:00
coins = get_coin_count ( event . author . id )
2022-10-27 22:10:53 +00:00
event . respond " #{ clean_name event . author . display_name } , you have #{ plural ( 'coin' , coins ) } ! "
2022-10-07 15:12:06 +00:00
end
2022-11-09 14:12:41 +00:00
bot . message ( with_text : '!leave' ) do | event |
event . respond PASTA
end
2023-01-26 16:33:15 +00:00
bot . message ( with_text : 'boobs' , in : Integer ( ENV [ 'CUM_CHANNEL' ] ) ) do | event |
2022-11-09 21:12:08 +00:00
next if not ENV [ 'CUM_CHANNEL' ]
event . respond " OHHHHHH IM GONNA CUM "
end
2022-11-07 12:20:44 +00:00
bot . reaction_add do | event |
2023-02-14 11:21:32 +00:00
next unless [ COIN_EMOJI , BAG_EMOJI ] . include? event . emoji . name
2022-11-07 12:20:44 +00:00
2023-02-08 10:30:39 +00:00
if event . message . author . id == bot . bot_application . id
get_coin ( event )
else
give_coin ( event )
2022-11-07 12:20:44 +00:00
end
end
2023-05-08 14:57:51 +00:00
# math problem
bot . message ( in : Integer ( ENV [ 'BOT_CHANNEL' ] ) ) do | event |
next unless $coin_state_active == COIN_STATE_MATH
result = eval ( " $mathproblem_op1 #{ $mathproblem_operator } $mathproblem_op2 " )
user_result = Integer ( event . message . content ) rescue false
if result == user_result
get_coin ( event , MATH_PROBLEM_COIN_GAIN )
else
event . respond " Nice try, but you're wrong! "
end
end
2022-10-07 15:12:06 +00:00
bot . run ( true )
2022-10-27 22:15:20 +00:00
loop do
2022-10-10 07:40:47 +00:00
# sleep random amount of time between 1 and 4 hours
sleep ( get_random_hours ( 1 , 4 ) )
2023-05-08 14:57:51 +00:00
if rand ( 6 ) == 1
# math problem
# activate coin state
$coin_state_active = COIN_STATE_MATH
$mathproblem_op1 = rand ( 10 .. 150 )
$mathproblem_op2 = rand ( 10 .. 150 )
2023-05-08 15:01:51 +00:00
$mathproblem_operator = rand ( 2 ) == 1 ? " + " : " - "
2022-10-07 15:12:06 +00:00
2023-05-08 14:57:51 +00:00
# send the math problem
2023-05-09 11:38:32 +00:00
bot . send_file ( ENV [ 'BOT_CHANNEL' ] , File . open ( './block.gif' , 'r' ) )
2023-05-09 14:03:33 +00:00
bot . send_message ( ENV [ 'BOT_CHANNEL' ] , " Solve the following for #{ MATH_PROBLEM_COIN_GAIN } coins: ` #{ $mathproblem_op1 } #{ $mathproblem_operator } #{ $mathproblem_op2 } ` " )
2023-05-08 14:57:51 +00:00
else
# activate coin state
$coin_state_active = COIN_STATE_ACTIVE
# send coin GIF
bot . send_file ( ENV [ 'BOT_CHANNEL' ] , File . open ( './coin.gif' , 'r' ) )
end
$coins_offered_today += 1
2022-10-10 07:40:47 +00:00
# if max coins per day has reached, sleep until midnight
2023-02-08 10:30:39 +00:00
if $coins_offered_today > = MAX_COINS_PER_DAY
2022-10-10 07:40:47 +00:00
sleep ( get_seconds_to_midnight ( Time . now ) )
2022-11-09 07:58:17 +00:00
sleep ( get_random_hours ( 5 , 11 ) )
2023-02-08 10:30:39 +00:00
$coins_offered_today = 0
2022-10-10 07:40:47 +00:00
end
2022-10-07 15:12:06 +00:00
end
bot . join ( )