gizmo385/discord.clj

Starting a new bot after closing an old bot restarts (maybe?) the old bot

JasonKDarby opened this issue · 5 comments

(defn start
  "Starts the bot asynchronously."
  []
  (dosync
    (bot/load-extension-folders!)
    (ref
      (bot/create-bot (config/get-bot-name) (config/get-prefix)))))

(defn stop
  "Stops the bot."
  [client]
  (.close @client))

...

(bot/defcommand working
  [client message]
  "Posts the Star Wars Episode 1 'It's working' gif in the channel"
  (bot/say "https://giphy.com/gifs/9K2nFglCAQClO"))

If I start a bot and call !working it responds perfectly. If I stop that bot and start another, when I call !working it posts the same response twice. If you do it again you get 3 responses and so on. Am I not stopping correctly? I'm relatively new to clojure (and nearly entirely new to discord.clj) so apologies if I'm missing something basic!

Also, thanks for making this!

@JasonKDarby Have you tried using the start macro exposed by the bot namespace?

@gizmo385 I did but I re-implemented it so that start could be asynchronous and would return the bot. I want to be able to write tests and run it in the REPL so I can't have start be blocking and I need a way to shut it down.

@JasonKDarby In the start function, you're loading the the clojure in your configured extensions directories. The defextension and defcommand macros modify a global atom, which contains all loaded commands and extensions. Reloading those files without resetting that atom might be causing those extensions and commands to get loaded multiple times.

Can you try moving the load call outside of your start function?

That was it, thanks! Again, really appreciating your effort 👍

Glad I could help and glad you're enjoying the framework! 👍