chronicle_bot/lib/addons/utils.rb
Bill Niblock 6fe1a7a251 Update how Addons are used
After some more reading, update the logic for handling the addons.

Leverage an "addons module" (Addon), and require that each addon be a
class within that module. Within that class, optionally include a
`self.register` method, which will be called when the bot is
initialized. This method takes one parameter: the bot
instance.Registering the addon adds the bot's instance to an instance of
the addon, and vice versa, allowing the instances to communicate.
Additionally, registering adds the addons commands.

Each registered addon must also include a `matrix_command` method, which
takes the message String from Matrix. The intention here is to include
additional `*_command` methods for different protocols, without having
to change much else. Adding additional protocols will determine how well
that works.

chronicle_bot.rb: Redo addon logic (details above)
addons/utils.rb: Update for new addon logic (details above)
addons/roller.rb: Update for new addon logic (details above)
2021-02-14 21:06:46 -05:00

82 lines
2 KiB
Ruby

# frozen_string_literal: true
module Chronicle
module Addon
# Ping - Pong. Useful for testing
class Ping
def self.register(bot)
addon_instance = new(bot)
addon_command = ['ping']
[addon_instance, addon_command]
end
def initialize(bot)
@bot = bot
end
# Handle a command from the Matrix protocol
#
# @param message [Message object] The relevant message object
def matrix_command(message)
room = @bot.client.ensure_room(message.room_id)
room.send_notice('Pong!')
end
end
# 8-Ball: Give a random, vague response to a question
class Eightball
def self.register(bot)
addon_instance = new(bot)
addon_command = ['8ball']
[addon_instance, addon_command]
end
def initialize(bot)
@bot = bot
end
# Handle a command from the Matrix protocol
#
# @param message [Message object] The relevant message object
def matrix_command(message)
msgstr = message.content[:body]
.gsub(/!8ball\s*/, '')
.strip
room = @bot.client.ensure_room(message.room_id)
fates = [
'Resoundingly, yes.',
'Chances are good.',
'Signs point to yes.',
'Wheel.',
'It is worth the attempt.',
'Uncertainty clouds my sight.',
'Wheel and woe.',
'Neither wheel nor woe.',
'Concentrate, and ask again.',
'I cannot say for sure.',
'The fates do not know.',
"Why are you asking me? I'm just a bot.",
'Error: Fate API returned 404. Try again later.',
'Woe.',
'Chances are poor.',
'Signs point to no.',
'Very doubtful.',
'Absolutely no.'
]
res = if msgstr[-1] == '?'
fates.sample
else
'You must ask a question. Try again'
end
room.send_notice(res)
end
end
end
end