Add TIL command for annnna - annna - Annna the nice friendly bot. | |
git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws6… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
commit cb81412efa9ed5137013987f5b676d9f8785917a | |
parent f7fc5dfb75555f14e781c15a30eeab2581e42e49 | |
Author: Scarlett McAllister <[email protected]> | |
Date: Mon, 23 Oct 2023 22:53:08 -0300 | |
Add TIL command for annnna | |
Signed-off-by: Annna Robert-Houdin <[email protected]> | |
Diffstat: | |
M annna-message-common | 18 ++++++++++++++++++ | |
A til | 62 +++++++++++++++++++++++++++++… | |
2 files changed, 80 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/annna-message-common b/annna-message-common | |
@@ -1238,6 +1238,24 @@ case "${text}" in | |
taguri="$(printf "%s\n" "${tagline}" | cut -d ' ' -f 2-)" | |
annna-say -s "${server}" -c "${channel}" "${tagname}: ${taguri}" | |
;; | |
+"${ircuser}, TIL "*) | |
+ minimum_learned_len=12 | |
+ offset=$(( ${#ircuser} + 6 )) | |
+ learned="$(printf "%s\n" "${text}" \ | |
+ | cut -c $offset- \ | |
+ | sed 's,\t, ,g')" | |
+ if [ ${#learned} -le $minimum_learned_len ]; | |
+ then | |
+ annna-say -s "${server}" -c "${channel}" "${user}, can you exp… | |
+ else | |
+ result="$(til ${user} "${learned}")" | |
+ if [ $? -eq 0 ]; | |
+ then | |
+ annna-say -s "${server}" -c "${channel}" "${user}, ${r… | |
+ fi | |
+ fi | |
+ ;; | |
+ | |
esac | |
printf '%s' "$text" | awk -v taglimit="$printnhashtags" ' | |
diff --git a/til b/til | |
@@ -0,0 +1,62 @@ | |
+#!/bin/sh | |
+ | |
+nick="${1}" | |
+learned="${2}" | |
+tilmodbase="$HOME/bin/modules/til" | |
+tildb="${tilmodbase}/til.txt" | |
+ | |
+# Make sure there's a file to store the TILs. | |
+if [ ! -f "${tildb}" ]; | |
+then | |
+ mkdir -p "${tilmodbase}" | |
+ touch "${tildb}" | |
+fi | |
+ | |
+# Make sure file is writable. | |
+if [ ! -w "${tildb}" ]; | |
+then | |
+ exit 1 | |
+fi | |
+ | |
+# Clean up the learned string. | |
+learned="$(printf "%s" "${learned}" \ | |
+ | tr -d [:cntrl:] \ | |
+ | sed 's,\n, ,g')" | |
+ | |
+# Check to see if its already been learned. | |
+already_learned_entry="$(grep -m 1 "${learned}" "${tildb}")" | |
+if [ $? -eq 0 ]; # Match found (grep exit status of 0) | |
+then | |
+ timestamp="$(printf "%s" "${already_learned_entry}" \ | |
+ | tr "\t" "\n" \ | |
+ | tail -n 1)" | |
+ user="$(printf "%s" "${already_learned_entry}" \ | |
+ | tr "\t" "\n" \ | |
+ | head -n 1)" | |
+ learned_text="$(printf "%s" "${already_learned_entry}" \ | |
+ | tr "\t" "\n" \ | |
+ | tail -n 2 \ | |
+ | head -n 1)" | |
+ paste="$(printf '%s\n%s learned %s\n' \ | |
+ '${timestamp}' \ | |
+ '${user}' \ | |
+ '${learned_text}' \ | |
+; | /br/bin/bitreich-paste)" | |
+ printf "looks like that's already been learned by %s on %s! %s" \ | |
+ "${user:-someone}" \ | |
+ "$(date -d "${timestamp}" +"%B %d, %Y")" \ | |
+ '${paste}' | |
+ exit | |
+fi | |
+ | |
+# Add the new TIL to the database. | |
+timestamp=$(date -u) | |
+printf "%s\n" "${nick} ${learned} $(date -u)" \ | |
+ >> "${tildb}" | |
+ | |
+# Count users TILs | |
+til_count="$(grep -cP "^${nick}\t" "${tildb}")" | |
+ | |
+# Successful return message. | |
+printf "well done! You've learned ${til_count} things so far! o.o" | |
+exit 0 |