radiobot/bot.sh

122 lines
3.0 KiB
Bash

#!/bin/bash
. bot.properties
input=".bot.cfg"
echo "Starting session: $(date "+[%y:%m:%d %T]")">$log
echo "NICK $nick" > $input
echo "USER $user" >> $input
echo "MODE $nick +B" >> $input
echo "JOIN #$channel" >> $input
function save_info {
now_playing=$(<$npfile)
}
function parse_dj {
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
}
# save info
save_info
dj=$(parse_dj)
# main loop
tail -f $input | telnet $server $port | while read res
do
# log the session
echo "$(date "+[%y:%m:%d %T]")$res" >> $log
# new dj!
if [[ $dj != $(parse_dj) ]]; then
dj=$(parse_dj)
if [[ ! -z "${dj}" ]]; then
echo "PRIVMSG #$channel :$dj is now online! tune in now here: https://bhh.sh/listen" >> $input
while read -u 10 subscriber; do
echo "PRIVMSG $subscriber :$dj is now online! tune in now here: https://bhh.sh/listen" >> $input
done 10< subscribers.txt
TOOT_JSON_PATH=. toot "dj $dj is now streaming live on https://tilderadio.org! tune in here: https://bhh.sh/listen"
fi
fi
# now playing
if [[ $now_playing != "$(<$npfile)" ]]; then
save_info
echo -e "PRIVMSG #$channel :\x0303$now_playing" >> $input
fi
# do things when you see output
case "$res" in
# respond to ping requests from the server
PING*)
echo "$res" | sed "s/I/O/" >> $input
;;
# make sure we're joined
*376*|*404*)
echo "JOIN #$channel" >> $input
;;
# run when a message is seen
*PRIVMSG*)
echo "$res"
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
from=$(echo "$res" | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
echo $who $from
# "#" would mean it's a channel
if [ "$(echo "$from" | grep '^#')" ]; then
test "$(echo "$res" | grep ":$nick:")" || continue
will=$(echo "$res" | perl -pe "s/.*:$nick:(.*)/\1/")
else
will=$(echo "$res" | perl -pe "s/.*$nick :(.*)/\1/")
from=$who
fi
will=$(echo "$will" | perl -pe "s/^ //")
com=$(echo "$will" | cut -d " " -f1)
case "$com" in
subscribe)
echo $who >> subscribers.txt
echo "PRIVMSG $from :i'll send you a direct message when a dj starts streaming!" >> $input
;;
unsubscribe)
sed -i "/$who/d" subscribers.txt
echo "PRIVMSG $from :i'll stop sending you updates." >> $input
;;
np)
echo "PRIVMSG $from :$now_playing" >> $input
;;
dj)
echo "PRIVMSG $from :${dj:-"no one"} is on the air now" >> $input
;;
help)
echo "PRIVMSG $from :hey hi, my commands are subscribe, unsubscribe, np, and dj" >> $input
;;
radio)
echo "PRIVMSG $from :eat the poop or die!!1" >> $input
;;
paymybills)
echo "PRIVMSG $from :whaddya mean?! i'm broker than you!" >> $input
;;
*)
echo "no command: $res"
;;
esac
;;
# else
*)
echo "$res"
;;
esac
done