Skip to content

Commit

Permalink
🆕 Fix for issue 102
Browse files Browse the repository at this point in the history
  • Loading branch information
pmonks committed Sep 10, 2020
1 parent 5b00a09 commit 34087e5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 14 deletions.
2 changes: 2 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
cheshire/cheshire {:mvn/version "5.10.0"}
clj-pdf/clj-pdf {:mvn/version "2.5.4"}
rm-hull/markov-chains {:mvn/version "0.1.1"}
pmonks/remus {:git/url "https://github.com/pmonks/remus.git"
:sha "2669acbb816ea843e45f4fb64aee0d96fe861e34"}
}
:aliases
{; clj -A:run -c /path/to/config.edn
Expand Down
2 changes: 2 additions & 0 deletions heroku-config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
"Tim Ford" "<:timford:732030914607186010>"
"Timothy Ford" "<:timford:732030914607186010>"
}

:quiz-channel-id "686439362291826694" ; Refereeing > training-and-resources
}
2 changes: 2 additions & 0 deletions resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
:muted-leagues #split [#env MUTED_LEAGUES "\\s*,\\s*"]

:referee-emoji {} ; TODO: Figure out how to parse a map out of an environment variable using aero...

:quiz-channel-id #env QUIZ_DISCORD_CHANNEL_ID
}
6 changes: 6 additions & 0 deletions src/futbot/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
(defstate referee-emoji
:start (:referee-emoji config))

(defstate quiz-channel-id
:start (let [channel-id (:quiz-channel-id config)]
(if-not (s/blank? channel-id)
channel-id
(throw (ex-info "Quiz Discord channel id not provided" {})))))

(def ^:private build-info
(if-let [deploy-info (io/resource "deploy-info.edn")]
(edn/read-string (slurp deploy-info))
Expand Down
29 changes: 24 additions & 5 deletions src/futbot/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@
:start (let [seven-past-the-hour-UTC (tm/with-clock (tm/system-clock "UTC") (tm/plus (tm/truncate-to (tm/zoned-date-time) :hours) (tm/minutes 7)))
every-hour-at-seven-past-the-hour-UTC (chime/periodic-seq (tm/instant seven-past-the-hour-UTC)
(tm/duration 1 :hours))]
(log/info (str "Scheduling GC job; first run will be at " (first every-hour-at-seven-past-the-hour-UTC)))
(chime/chime-at every-hour-at-seven-past-the-hour-UTC
(fn [_] (System/gc))))
:stop (.close ^java.lang.AutoCloseable gc-job))

(defstate daily-job
(defstate daily-schedule-job
:start (let [tomorrow-at-midnight-UTC (tm/with-clock (tm/system-clock "UTC") (tm/truncate-to (tm/plus (tm/zoned-date-time) (tm/days 1)) :days))
every-day-at-midnight-UTC (chime/periodic-seq (tm/instant tomorrow-at-midnight-UTC)
(tm/period 1 :days))]
(log/info (str "Scheduling daily schedule job; first run will be at " (first every-day-at-midnight-UTC)))
(chime/chime-at every-day-at-midnight-UTC
(fn [_]
(try
(log/info "Daily job started...")
(log/info "Daily schedule job started...")
(let [today (tm/with-clock (tm/system-clock "UTC") (tm/zoned-date-time))
todays-scheduled-matches (fd/scheduled-matches-on-day cfg/football-data-api-token today)]
(job/post-daily-schedule-to-channel! cfg/discord-message-channel
Expand All @@ -58,10 +60,27 @@
cfg/referee-emoji
todays-scheduled-matches))
(catch Exception e
(log/error e "Unexpected exception while generating daily schedule"))
(log/error e "Unexpected exception in daily schedule job"))
(finally
(log/info "Daily job finished"))))))
:stop (.close ^java.lang.AutoCloseable daily-job))
(log/info "Daily schedule job finished"))))))
:stop (.close ^java.lang.AutoCloseable daily-schedule-job))

(defstate dutch-referee-blog-quiz-job
:start (let [tomorrow-at-nine-am-UTC (tm/with-clock (tm/system-clock "UTC") (tm/plus (tm/truncate-to (tm/plus (tm/zoned-date-time) (tm/days 1)) :days) (tm/hours 9)))
every-day-at-nine-am-UTC (chime/periodic-seq (tm/instant tomorrow-at-nine-am-UTC)
(tm/period 1 :days))]
(log/info (str "Scheduling Dutch referee blog quiz job; first run will be at " (first every-day-at-nine-am-UTC)))
(chime/chime-at every-day-at-nine-am-UTC
(fn [_]
(try
(log/info "Dutch referee blog quiz job started...")
(job/check-for-new-dutch-referee-blog-quiz-and-post-to-channel! cfg/discord-message-channel
cfg/quiz-channel-id)
(catch Exception e
(log/error e "Unexpected exception in Dutch referee blog quiz job"))
(finally
(log/info "Dutch referee blog quiz job finished"))))))
:stop (.close ^java.lang.AutoCloseable dutch-referee-blog-quiz-job))

; Bot functionality
(defn start-bot!
Expand Down
35 changes: 35 additions & 0 deletions src/futbot/dutch_referee_blog.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
;
; Copyright © 2020 Peter Monks
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; SPDX-License-Identifier: Apache-2.0
;

(ns futbot.dutch-referee-blog
(:require [clojure.string :as s]
[java-time :as tm]
[remus :as rss]))

(def dutch-referee-blog-rss-feed-url "http://www.dutchreferee.com/feed/atom") ; I prefer atom...
(def quiz-title-substring "Laws of the Game Quiz")

(defn quizzes
"Returns a sequence of maps representing all of the quizzes in the Dutch Referee Blog's RSS feed, optionally since the given date, or nil if there aren't any."
([] (quizzes nil))
([since]
(if-let [all-quizzes (seq (filter #(s/index-of (:title %) quiz-title-substring)
(:entries (:feed (rss/parse-url dutch-referee-blog-rss-feed-url)))))]
(if since
(seq (filter #(tm/after? (tm/instant (:published-date %)) (tm/instant since)) all-quizzes))
all-quizzes))))
41 changes: 32 additions & 9 deletions src/futbot/jobs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
;

(ns futbot.jobs
(:require [clojure.string :as s]
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[java-time :as tm]
[chime.core :as chime]
[discljord.messaging :as dm]
[futbot.football-data :as fd]
[futbot.pdf :as pdf]
[futbot.flags :as fl]))
(:require [clojure.string :as s]
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[java-time :as tm]
[chime.core :as chime]
[discljord.messaging :as dm]
[futbot.football-data :as fd]
[futbot.dutch-referee-blog :as drb]
[futbot.pdf :as pdf]
[futbot.flags :as fl]))

(defn post-daily-schedule-to-channel!
"Generates and posts the daily-schedule (as an attachment) to the Discord channel identified by channel-id."
Expand Down Expand Up @@ -163,3 +164,25 @@
referee-emoji)
(distinct todays-scheduled-matches)))
(log/info "No matches remaining today - not scheduling any reminders."))))

(defn check-for-new-dutch-referee-blog-quiz-and-post-to-channel!
"Checks whether a new Dutch referee blog quiz has been posted in the last time-period-hours hours (defaults to 24), and posts it to the given channel if so."
([discord-message-channel channel-id] (check-for-new-dutch-referee-blog-quiz-and-post-to-channel! discord-message-channel channel-id 24))
([discord-message-channel
channel-id
time-period-hours]
(if-let [new-quizzes (drb/quizzes (tm/minus (tm/instant) (tm/hours time-period-hours)))]
(let [message (str "A new **Dutch Referee Blog Laws of the Game Quiz** has been posted: "
(:link (first new-quizzes))
"\nPlease click the reactions for any questions you got wrong, and we can chat about them.")
message-id (:id @(dm/create-message! discord-message-channel ; Note: dereferences the promise, blocking until the message is sent
channel-id
:content message))]
(when message-id
@(dm/create-reaction! discord-message-channel channel-id message-id "1️⃣") ; Note: wait for each promise to comnplete, to make sure reactions are added in numerical order
@(dm/create-reaction! discord-message-channel channel-id message-id "2️⃣")
@(dm/create-reaction! discord-message-channel channel-id message-id "3️⃣")
@(dm/create-reaction! discord-message-channel channel-id message-id "4️⃣")
@(dm/create-reaction! discord-message-channel channel-id message-id "5️⃣"))
nil)
(log/info "No new quizzes found"))))

0 comments on commit 34087e5

Please sign in to comment.