Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🆕 First cut of implementation of issue #201 #202

Merged
merged 2 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/futbot/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
(:require [clojure.string :as s]
[clojure.tools.logging :as log]
[java-time :as tm]
[discljord.formatting :as df]
[futbot.util :as u]
[futbot.message-util :as mu]
[futbot.config :as cfg]
Expand All @@ -42,6 +43,36 @@
:icon_url "https://yt3.ggpht.com/ytc/AAUvwnjhzwc9yNfyfX8C1N820yMhaS27baWlSz2wqaRE=s176-c-k-c0x00ffffff-no-rj"}))
(log/info (str "Ignoring " prefix "ist command in channel " channel-id)))))

(defn move-command!
"Moves a conversation to the specified channel"
[args event-data]
(when (not (mu/direct-message? event-data)) ; Only respond if the message was sent to a real channel in a server (i.e. not in a DM)
(let [guild-id (:guild-id event-data)
channel-id (:channel-id event-data)
message-id (:id event-data)]
(mu/delete-message! cfg/discord-message-channel channel-id message-id)
(if (not (s/blank? args))
(if-let [target-channel-id (second (re-find df/channel-mention args))]
(if (not= channel-id target-channel-id)
(let [target-message-id (:id (mu/create-message! cfg/discord-message-channel
target-channel-id
:embed (assoc (mu/embed-template)
:description (str "Continuing the conversation from " (mu/channel-link channel-id) "..."))))
target-message-url (mu/message-url guild-id target-channel-id target-message-id)
source-message-id (:id (mu/create-message! cfg/discord-message-channel
channel-id
:embed (assoc (mu/embed-template)
:description (str "Let's continue this conversation in " (mu/channel-link target-channel-id) " ([link](" target-message-url "))."))))
source-message-url (mu/message-url guild-id channel-id source-message-id)]
(mu/edit-message! cfg/discord-message-channel
target-channel-id
target-message-id
:embed (assoc (mu/embed-template)
:description (str "Continuing the conversation from " (mu/channel-link channel-id) " ([link](" source-message-url "))..."))))
(log/info "Cannot move a conversation to the same channel."))
(log/warn "Could not find target channel in move command."))
(log/warn "move-command! arguments missing a target channel.")))))

(defn privacy-command!
"Provides a link to the futbot privacy policy"
[_ event-data]
Expand All @@ -66,7 +97,7 @@
{:name "Clojure" :value (str "v" (clojure-version)) :inline true}
{:name "JVM" :value (str (System/getProperty "java.vm.vendor") " v" (System/getProperty "java.vm.version") " (" (System/getProperty "os.name") "/" (System/getProperty "os.arch") ")") :inline true}
; Force a newline (Discord is hardcoded to show 3 fields per line), by using Unicode zero width spaces (empty/blank strings won't work!)
{:name "​" :value "​" :inline true}
{:name "​" :value "​" :inline true}
{:name "Heap memory in use" :value (u/human-readable-size (.getUsed (.getHeapMemoryUsage (java.lang.management.ManagementFactory/getMemoryMXBean)))) :inline true}
{:name "Non-heap memory in use" :value (u/human-readable-size (.getUsed (.getNonHeapMemoryUsage (java.lang.management.ManagementFactory/getMemoryMXBean)))) :inline true}
]))))
Expand Down Expand Up @@ -113,7 +144,8 @@

; Table of "public" commands; those that can be used in any channel, group or DM
(def public-command-dispatch-table
{"ist" #'ist-command!})
{"ist" #'ist-command!
"move" #'move-command!})

(declare help-command!)

Expand Down
28 changes: 20 additions & 8 deletions src/futbot/message_util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
(ns futbot.message-util
(:require [clojure.tools.logging :as log]
[java-time :as tm]
[discljord.messaging :as dm]))
[discljord.messaging :as dm]
[discljord.formatting :as df]))

(defn- check-response-and-throw
[response]
Expand All @@ -46,18 +47,24 @@
(log/debug "Sending message to Discord channel" (str channel-id " with args:") args)
(check-response-and-throw @(apply dm/create-message! discord-message-channel channel-id args)))

(defn create-reaction!
"A version of discljord.message/create-reaction! that throws errors."
[discord-message-channel channel-id message-id reaction]
(log/debug "Adding reaction" reaction "to message-id" message-id)
(check-response-and-throw @(dm/create-reaction! discord-message-channel channel-id message-id reaction)))
(defn edit-message!
"A version of discljord.message/edit-message! that throws errors."
[discord-message-channel channel-id message-id & args]
(log/debug "Editing message" message-id "in Discord channel" (str channel-id " with args:") args)
(check-response-and-throw @(apply dm/edit-message! discord-message-channel channel-id message-id args)))

(defn delete-message!
"A version of discljord.message/delete-message! that throws errors."
[discord-message-channel channel-id message-id]
(log/debug "Deleting message-id" message-id)
(check-response-and-throw @(dm/delete-message! discord-message-channel channel-id message-id)))

(defn create-reaction!
"A version of discljord.message/create-reaction! that throws errors."
[discord-message-channel channel-id message-id reaction]
(log/debug "Adding reaction" reaction "to message-id" message-id)
(check-response-and-throw @(dm/create-reaction! discord-message-channel channel-id message-id reaction)))

(defn create-dm!
"A version of discljord.message/create-dm! that throws errors."
[discord-message-channel user-id]
Expand Down Expand Up @@ -103,11 +110,16 @@
"Convenience method that creates a link to the given channel-id, for embedding in message bodies"
[channel-id]
(when channel-id
(str "<#" channel-id ">")))
(df/mention-channel channel-id)))

(defn user-link
"Convenience method that creates a link to the given user-id, for embedding in message bodies"
[user-id]
(when user-id
(str "<@" user-id ">")))
(df/mention-user user-id)))

(defn message-url
"Convenience method that creates a URL to a specific message"
[guild-id channel-id message-id]
(when (and guild-id channel-id message-id)
(str "https://discord.com/channels/" guild-id "/" channel-id "/" message-id)))