radiobot/bot.sh

141 lines
3.4 KiB
Bash

#!/bin/bash
. bot.properties
input=".bot.cfg"
echo "Starting session: $(date "+[%y:%m:%d %T]")">$log
echo "NICK $nick" > $input
echo "USER $nick 0 * :$nick" >> $input
echo "MODE $nick +B" >> $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
for chan in $notify_channels; do
echo "PRIVMSG #$chan :$dj is now online! tune in now here: $link" >> $input
done
while read -u 10 subscriber; do
echo "PRIVMSG $subscriber :$dj is now online! tune in now here: $link" >> $input
done 10< subscribers.txt
for json in . ./yourtilde ./team; do
TOOT_JSON_PATH=$json toot "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
done
fi
fi
# now playing
if [[ $now_playing != "$(<$npfile)" ]]; then
save_info
[[ ! -z "${dj}" ]] && 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*)
for chan in $notify_channels; do
echo "JOIN #$chan" >> $input
done
;;
# 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)
if grep -q $who subscribers.txt; then
echo "PRIVMSG $from :you're already subscribed! :)" >> $input
else
echo $who >> subscribers.txt
echo "PRIVMSG $from :i'll send you a direct message when a dj starts streaming!" >> $input
fi
;;
unsubscribe)
sed -i "/$who/d" subscribers.txt
echo "PRIVMSG $from :i'll stop sending you updates." >> $input
;;
time)
echo "PRIVMSG $from :$(TZ=UTC date)" >> $input
;;
np)
echo "PRIVMSG $from :$now_playing" >> $input
;;
dj)
echo "PRIVMSG $from :${dj:-"no one"} is on the air now" >> $input
;;
link)
echo "PRIVMSG $from :$link" >> $input
;;
help)
echo "PRIVMSG $from :hey hi, my commands are subscribe, unsubscribe, np, dj, link, radio" >> $input
;;
radio|slogan)
echo "PRIVMSG $from :$(curl -s https://ben.ttm.sh/slogans.txt | shuf -n1)" >> $input
;;
paymybills)
echo "PRIVMSG $from :whaddya mean?! i'm broker than you!" >> $input
;;
*)
echo "no command: $res"
;;
esac
;;
# else
*)
echo "$res"
;;
esac
done