Custom Commands: Make custom commands dynamically

With the updated means of handling addons, update Custom Commands to
include the new logic and be functional.

addons/custom_commands.rb: Update for new addons logic
This commit is contained in:
Bill Niblock 2021-02-14 21:12:13 -05:00
parent 6fe1a7a251
commit b8ffa3db28

View file

@ -5,83 +5,52 @@
# For example, "!hello" might print: # For example, "!hello" might print:
# "Welcome to the channel! You can follow [this link](link) to visit # "Welcome to the channel! You can follow [this link](link) to visit
# our website!" # our website!"
module Chronicle
module Matrix
# Add addon-specific commands to the allowed commands array
add_allowed_commands(%w[addcommand modcommand remcommand])
# Add a custom command
#
# @param commander [CustomCommander instance] The CustomCommander to work
# with
# @param client [Matrix Client instance] The client to work with
# @param message [hash] The message data from Matrix
def handle_addcommand(client, message)
msgstr = message.content[:body]
.gsub(/!addcommand\s*/, '')
.strip
room = client.ensure_room(message.room_id)
commander = CustomCmd.commanders(message)
res = commander.addcmd(msgstr)
room.send_notice(res)
end
# Modify an existing custom command
#
# @param message [hash] The message data from Matrix
def handle_modcommand(client, message)
msgstr = message.content[:body]
.gsub(/!modcommand\s*/, '')
.strip
room = client.ensure_room(message.room_id)
commander = CustomCmd.commanders(message)
res = commander.modcmd(msgstr)
room.send_notice(res)
end
# Remove an existing custom command
#
# @param message [hash] The message data from Matrix
def handle_remcommand(client, message)
msgstr = message.content[:body]
.gsub(/!remcommand\s*/, '')
.strip
room = client.ensure_room(message.room_id)
commander = CustomCmd.commanders(message)
res = commander.remcmd(msgstr)
room.send_notice(res)
end
module CustomCmd
require 'json' require 'json'
@@commander_instances = {} module Chronicle
module Addon
def self.commanders(message)
msgid = message.room_id
unless @@commander_instances.keys.include?(msgid)
@@commander_instances[msgid] = CustomCommander.new(msgid)
end
@@commander_instances[msgid]
end
class CustomCommander class CustomCommander
def initialize(msgid) def self.register(bot)
# build custom commands DB in memory addon_instance = new(bot)
@msgid = msgid addon_command = ['addcommand','modcommand','remcommand']
[addon_instance, addon_command]
end
def initialize(bot)
@bot = bot
@msgid = 'tmp_until_per-room'
@custom_commands = read_commands(@msgid) @custom_commands = read_commands(@msgid)
@bot.available_commands(self, @custom_commands.keys)
end
# Handle a command from the Matrix protocol
#
# @param message [Message object] The relevant message object
def matrix_command(message)
pfx = @bot.cmd_prefix
cmd = message.content[:body].split(/\s+/)[0].gsub(/#{pfx}/, '')
msgstr = message.content[:body]
.gsub(/#{pfx}\w+\s*/, '')
.strip
res = 'Invalid command'
case cmd
when "addcommand"
res = handle_addcommand(msgstr)
when "modcommand"
res = handle_modcommand(msgstr)
when "remcommand"
res = handle_remcommand(msgstr)
else
res = runcmd(cmd)
end
room = @bot.client.ensure_room(message.room_id)
room.send_notice(res)
end end
# Add a new custom command # Add a new custom command
@ -89,13 +58,55 @@ module Chronicle
command = message.slice!(/\w+\s+/).strip command = message.slice!(/\w+\s+/).strip
return cmd_add_error(command) if verify_commands(command) return cmd_add_error(command) if verify_commands(command)
return cmd_addon_error if addon_command(command)
@custom_commands[command] = message @custom_commands[command] = message
@bot.available_commands(self, [command])
save_commands(@msgid) save_commands(@msgid)
"New command saved: !#{command}" "New command saved: !#{command}"
end end
# Adds a new custom command
#
# @param message [String] The command plus response
# @return
def handle_addcommand(message)
res = 'Usage: !addcommand NAME RESPONSE'
if message.split(/\s+/).count > 1
res = addcmd(message)
end
res
end
# Modify an existing custom command
#
# @param message [hash] The message data from Matrix
def handle_modcommand(message)
res = 'Usage: !modcommand NAME NEW-RESPONSE'
if message.split(/\s+/).count > 1
res = modcmd(message)
end
res
end
# Remove an existing custom command
#
# @param message [hash] The message data from Matrix
def handle_remcommand(message)
res = 'Usage: !remcommand NAME'
if message.split(/\s+/).count == 1
res = remcmd(message)
end
res
end
# Modify an existing custom command # Modify an existing custom command
def modcmd(message) def modcmd(message)
command = message.slice!(/\w+\s+/).strip command = message.slice!(/\w+\s+/).strip
@ -112,9 +123,10 @@ module Chronicle
def remcmd(message) def remcmd(message)
command = message.strip command = message.strip
return cmd_rem_error(command) unless verify_commands(command) return cmd_rem_error unless verify_commands(command)
@custom_commands.delete(command) @custom_commands.delete(command)
@bot.disable_commands(command)
save_commands(@msgid) save_commands(@msgid)
"!#{command} removed." "!#{command} removed."
@ -129,22 +141,32 @@ module Chronicle
private private
# Check if a custom command conflicts with an existing addon command
def addon_command(command)
@bot.all_commands.keys.include?(command)
end
# Error message when trying to add an existing command # Error message when trying to add an existing command
def cmd_add_error(command) def cmd_add_error(command)
"This command already exists. \ 'This custom command already exists. '\
You can modify it by typing `!modcommand #{command}`" "You can modify it by typing `!modcommand #{command}`"
end
# Error message when trying to add an existing command
def cmd_addon_error
'This command is already used by another addon.'
end end
# Error message when trying to modify a non-existing command # Error message when trying to modify a non-existing command
def cmd_mod_error(command) def cmd_mod_error(command)
"This command does not exist. \ 'This custom command does not exist. '\
You can add it by typing `!addcommand #{command}`" "You can add it by typing `!addcommand #{command}`"
end end
# Error message when trying to delete a non-existing command # Error message when trying to delete a non-existing command
def cmd_rem_error(command) def cmd_rem_error
"This command does not exist. \ 'This custom command does not exist. '\
Nothing to remove." 'Nothing to remove.'
end end
# Read the existing saved commands into memory # Read the existing saved commands into memory
@ -152,7 +174,7 @@ module Chronicle
cmds = {} cmds = {}
cmds_file = "#{msgid}_custom_commands.json" cmds_file = "#{msgid}_custom_commands.json"
if File.exists?(cmds_file) && !File.empty?(cmds_file) if File.exist?(cmds_file) && !File.empty?(cmds_file)
File.open(cmds_file, 'r') do |f| File.open(cmds_file, 'r') do |f|
cmds = JSON.parse(f.read) cmds = JSON.parse(f.read)
end end
@ -177,4 +199,3 @@ module Chronicle
end end
end end
end end
end