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