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:
parent
6fe1a7a251
commit
b8ffa3db28
1 changed files with 157 additions and 136 deletions
|
@ -5,176 +5,197 @@
|
|||
# For example, "!hello" might print:
|
||||
# "Welcome to the channel! You can follow [this link](link) to visit
|
||||
# our website!"
|
||||
require 'json'
|
||||
|
||||
module Chronicle
|
||||
module Matrix
|
||||
# Add addon-specific commands to the allowed commands array
|
||||
add_allowed_commands(%w[addcommand modcommand remcommand])
|
||||
module Addon
|
||||
class CustomCommander
|
||||
def self.register(bot)
|
||||
addon_instance = new(bot)
|
||||
addon_command = ['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'
|
||||
|
||||
@@commander_instances = {}
|
||||
|
||||
def self.commanders(message)
|
||||
msgid = message.room_id
|
||||
|
||||
unless @@commander_instances.keys.include?(msgid)
|
||||
@@commander_instances[msgid] = CustomCommander.new(msgid)
|
||||
end
|
||||
|
||||
@@commander_instances[msgid]
|
||||
[addon_instance, addon_command]
|
||||
end
|
||||
|
||||
class CustomCommander
|
||||
def initialize(msgid)
|
||||
# build custom commands DB in memory
|
||||
@msgid = msgid
|
||||
@custom_commands = read_commands(@msgid)
|
||||
def initialize(bot)
|
||||
@bot = bot
|
||||
@msgid = 'tmp_until_per-room'
|
||||
@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
|
||||
|
||||
# Add a new custom command
|
||||
def addcmd(message)
|
||||
command = message.slice!(/\w+\s+/).strip
|
||||
room = @bot.client.ensure_room(message.room_id)
|
||||
|
||||
return cmd_add_error(command) if verify_commands(command)
|
||||
|
||||
@custom_commands[command] = message
|
||||
save_commands(@msgid)
|
||||
room.send_notice(res)
|
||||
end
|
||||
|
||||
"New command saved: !#{command}"
|
||||
# Add a new custom command
|
||||
def addcmd(message)
|
||||
command = message.slice!(/\w+\s+/).strip
|
||||
|
||||
return cmd_add_error(command) if verify_commands(command)
|
||||
return cmd_addon_error if addon_command(command)
|
||||
|
||||
@custom_commands[command] = message
|
||||
@bot.available_commands(self, [command])
|
||||
save_commands(@msgid)
|
||||
|
||||
"New command saved: !#{command}"
|
||||
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
|
||||
|
||||
# Modify an existing custom command
|
||||
def modcmd(message)
|
||||
command = message.slice!(/\w+\s+/).strip
|
||||
res
|
||||
end
|
||||
|
||||
return cmd_mod_error(command) unless verify_commands(command)
|
||||
|
||||
@custom_commands[command] = message
|
||||
save_commands(@msgid)
|
||||
# Modify an existing custom command
|
||||
#
|
||||
# @param message [hash] The message data from Matrix
|
||||
def handle_modcommand(message)
|
||||
res = 'Usage: !modcommand NAME NEW-RESPONSE'
|
||||
|
||||
"!#{command} modified."
|
||||
if message.split(/\s+/).count > 1
|
||||
res = modcmd(message)
|
||||
end
|
||||
|
||||
# Delete an existing custom command
|
||||
def remcmd(message)
|
||||
command = message.strip
|
||||
res
|
||||
end
|
||||
|
||||
return cmd_rem_error(command) unless verify_commands(command)
|
||||
# Remove an existing custom command
|
||||
#
|
||||
# @param message [hash] The message data from Matrix
|
||||
def handle_remcommand(message)
|
||||
res = 'Usage: !remcommand NAME'
|
||||
|
||||
@custom_commands.delete(command)
|
||||
save_commands(@msgid)
|
||||
|
||||
"!#{command} removed."
|
||||
if message.split(/\s+/).count == 1
|
||||
res = remcmd(message)
|
||||
end
|
||||
|
||||
# Execute a custom command
|
||||
def runcmd(command)
|
||||
return cmd_mod_error(command) unless verify_commands(command)
|
||||
res
|
||||
end
|
||||
|
||||
@custom_commands[command]
|
||||
end
|
||||
# Modify an existing custom command
|
||||
def modcmd(message)
|
||||
command = message.slice!(/\w+\s+/).strip
|
||||
|
||||
private
|
||||
return cmd_mod_error(command) unless verify_commands(command)
|
||||
|
||||
# Error message when trying to add an existing command
|
||||
def cmd_add_error(command)
|
||||
"This command already exists. \
|
||||
You can modify it by typing `!modcommand #{command}`"
|
||||
end
|
||||
@custom_commands[command] = message
|
||||
save_commands(@msgid)
|
||||
|
||||
# Error message when trying to modify a non-existing command
|
||||
def cmd_mod_error(command)
|
||||
"This command does not exist. \
|
||||
You can add it by typing `!addcommand #{command}`"
|
||||
end
|
||||
"!#{command} modified."
|
||||
end
|
||||
|
||||
# Error message when trying to delete a non-existing command
|
||||
def cmd_rem_error(command)
|
||||
"This command does not exist. \
|
||||
Nothing to remove."
|
||||
end
|
||||
# Delete an existing custom command
|
||||
def remcmd(message)
|
||||
command = message.strip
|
||||
|
||||
# Read the existing saved commands into memory
|
||||
def read_commands(msgid)
|
||||
cmds = {}
|
||||
cmds_file = "#{msgid}_custom_commands.json"
|
||||
return cmd_rem_error unless verify_commands(command)
|
||||
|
||||
if File.exists?(cmds_file) && !File.empty?(cmds_file)
|
||||
File.open(cmds_file, 'r') do |f|
|
||||
cmds = JSON.parse(f.read)
|
||||
end
|
||||
end
|
||||
@custom_commands.delete(command)
|
||||
@bot.disable_commands(command)
|
||||
save_commands(@msgid)
|
||||
|
||||
cmds
|
||||
end
|
||||
"!#{command} removed."
|
||||
end
|
||||
|
||||
# Save the existing commands to a local file
|
||||
def save_commands(msgid)
|
||||
cmds_file = "#{msgid}_custom_commands.json"
|
||||
# Execute a custom command
|
||||
def runcmd(command)
|
||||
return cmd_mod_error(command) unless verify_commands(command)
|
||||
|
||||
File.open(cmds_file, 'w') do |f|
|
||||
f.write(@custom_commands.to_json)
|
||||
@custom_commands[command]
|
||||
end
|
||||
|
||||
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
|
||||
def cmd_add_error(command)
|
||||
'This custom command already exists. '\
|
||||
"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
|
||||
|
||||
# Error message when trying to modify a non-existing command
|
||||
def cmd_mod_error(command)
|
||||
'This custom command does not exist. '\
|
||||
"You can add it by typing `!addcommand #{command}`"
|
||||
end
|
||||
|
||||
# Error message when trying to delete a non-existing command
|
||||
def cmd_rem_error
|
||||
'This custom command does not exist. '\
|
||||
'Nothing to remove.'
|
||||
end
|
||||
|
||||
# Read the existing saved commands into memory
|
||||
def read_commands(msgid)
|
||||
cmds = {}
|
||||
cmds_file = "#{msgid}_custom_commands.json"
|
||||
|
||||
if File.exist?(cmds_file) && !File.empty?(cmds_file)
|
||||
File.open(cmds_file, 'r') do |f|
|
||||
cmds = JSON.parse(f.read)
|
||||
end
|
||||
end
|
||||
|
||||
# Check if a command already exists
|
||||
def verify_commands(command)
|
||||
@custom_commands.keys.include?(command)
|
||||
cmds
|
||||
end
|
||||
|
||||
# Save the existing commands to a local file
|
||||
def save_commands(msgid)
|
||||
cmds_file = "#{msgid}_custom_commands.json"
|
||||
|
||||
File.open(cmds_file, 'w') do |f|
|
||||
f.write(@custom_commands.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
# Check if a command already exists
|
||||
def verify_commands(command)
|
||||
@custom_commands.keys.include?(command)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue