Implement facts api for remembering things

This commit is contained in:
Nick Chambers 2021-06-15 12:23:16 -05:00
parent 8e6bcdb0e5
commit ee8011c0bf
1 changed files with 53 additions and 4 deletions

57
rowbot
View File

@ -92,6 +92,8 @@ nick=${opts[nick]:-rowbot-dev}
ident=${opts[ident]:-rowbot}
realname=${opts[realname]:-rowbot}
chan=${opts[chan]:-}
trigger=${opts[trigger]:-\`}
fact_root=${opts[fact-root]:-.}
###
# net code
@ -128,6 +130,10 @@ on_ERROR() {
exit
}
on_JOIN() {
info "%s has joined %s" "$from" "${params[0]}"
}
on_MODE() {
if (( ${#params[@]} == 2 )); then
info "%s sets mode(s) %s on %s" "$from" "${params[1]}" "${params[0]}"
@ -258,7 +264,7 @@ pong() {
}
privmsg() {
send "PRIVMSG %s :%s" "$1" "$2"
send "PRIVMSG %s :\u200b%s" "$1" "$2"
}
user() {
@ -266,11 +272,54 @@ user() {
}
###
# utility hooks
# app hooks
##
hook_JOIN_greet() {
privmsg "${params[0]}" "Hello, $from!"
hook_PRIVMSG_factoids() {
if [[ ${params[0]:0:1} != \# ]]; then
return 0
elif [[ ${words[0]} = "$trigger"* ]]; then
case ${words[0]:${#trigger}} in
is)
if (( ${#words[@]} < 3 )); then
return 0
fi
local key val
key=${params[1]#*"$trigger"is} key=${key# }
val=${key#* } key=${key%% *}
info "%s said in %s to remember %s as %s" "$from" "${params[0]}" "$key" "$val"
privmsg "${params[0]}" "I'm sure I'll remember that."
mkdir -p "$fact_root"/"${params[0]}"
printf %s "$val" > "$fact_root"/"${params[0]}"/"$key"
;;
isnt)
if (( ${#words[@]} < 2 )); then
return 0
fi
local key
key=${params[1]#*"$trigger"isnt} key=${key# }
if [[ -f $fact_root/${params[0]}/$key ]]; then
info "%s said in %s to delete %s" "$from" "${params[0]}" "$key"
privmsg "${params[0]}" "I forgot what that was anyways."
rm -f "$fact_root"/"${params[0]}"/"$key"
fi
;;
ls)
local facts=( "$fact_root"/"${params[0]}"/* )
privmsg "${params[0]}" "${facts[*]##*/}"
;;
*)
local key=${params[1]:1}
if [[ -f $fact_root/${params[0]}/$key ]]; then
privmsg "${params[0]}" "$from: $(<"$fact_root"/"${params[0]}"/"$key")"
fi
esac
fi
}
###