Last active
February 18, 2022 05:46
-
-
Save LdBeth/b59448896a8aff20be5292914e631308 to your computer and use it in GitHub Desktop.
discord bot that evals april
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#| | |
BSD 2-Clause License | |
Copyright (c) 2022, LdBeth | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|# | |
;;; sbcl --dynamic-space-size 512 --load aplbot.lisp | |
(ql:quickload :lispcord) | |
(ql:quickload :april) | |
(ql:quickload :cl-ppcre) | |
(defpackage :apl.bot | |
(:use :cl :lispcord)) | |
(in-package :apl.bot) | |
(defvar *apl-bot*) | |
(defun get-apl (msg) | |
(multiple-value-bind (has-code codes) | |
(cl-ppcre:scan-to-strings "^```\\w*\\n\\s*⋄([\\s\\S]+?)```|`(⋄.+?)`" msg) | |
(when has-code | |
(or (aref codes 0) | |
(aref codes 1))))) | |
(defun post-apl (apl) | |
(multiple-value-bind | |
(res err) | |
(ignore-errors | |
(sb-ext:with-timeout 3 | |
(april:april (with (:state :output-printed :only)) | |
apl))) | |
(if err | |
(format nil "APL ERROR: ~A" err) | |
(let ((output (string-trim '(#\Space #\Newline) | |
res))) | |
(if (= 0 (length output)) | |
"Response looks like a 0-by-0 matrix." | |
(format nil "```~%~A~%```~%" | |
output)))))) | |
;;specify the behaviour of the bot for "on-message-create" events: | |
(defun message-create (msg) | |
(when (not (botp (lc:author msg))) | |
;; If the command was invoked via an @mention, we want to get rid of | |
;; that as well as any surrounding whitespace | |
;; #'ME simply returns the user-instance of the current bot | |
(if (commandp msg) | |
(let ((cmd (string-trim " " (remove-mention (me) (lc:content msg))))) | |
(cond ((string= cmd "bye!") | |
(if (string= "LdBeth" (lc:name (lc:author msg))) | |
(disconnect *apl-bot*) | |
(reply msg "You are not admin."))) | |
((string= cmd "clear!") | |
(reply msg (april:april-clear-workspace april::common))))) | |
(let ((apl (get-apl (lc:content msg)))) | |
(when apl | |
(reply msg (post-apl apl))))))) | |
(defun run (token) | |
(setf *apl-bot* (make-bot token)) | |
;; set up a handler waiting for message_create events | |
(add-event-handler :on-message-create 'message-create) | |
(add-event-handler :on-ready | |
(lambda (ready) | |
(format t "User: ~a~%Session: ~a~%Connected!" | |
(lc:name (lc:user ready)) | |
(lc:session-id ready)))) | |
(connect *apl-bot*)) | |
(defun exit () | |
(disconnect *apl-bot*) | |
(sb-ext:exit)) | |
(defconstant +token+ "TOKEN") | |
(run +token+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment