2021-02-07 03:28:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Custom Commands: Add custom "echo" commands, to post a text message
|
|
|
|
#
|
|
|
|
# For example, "!hello" might print:
|
|
|
|
# "Welcome to the channel! You can follow [this link](link) to visit
|
|
|
|
# our website!"
|
2021-02-15 02:12:13 +00:00
|
|
|
require 'json'
|
2021-02-07 03:28:30 +00:00
|
|
|
|
|
|
|
module Chronicle
|
2021-02-15 02:12:13 +00:00
|
|
|
module Addon
|
|
|
|
class CustomCommander
|
|
|
|
def self.register(bot)
|
|
|
|
addon_instance = new(bot)
|
|
|
|
addon_command = ['addcommand','modcommand','remcommand']
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
[addon_instance, addon_command]
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
def initialize(bot)
|
|
|
|
@bot = bot
|
|
|
|
@msgid = 'tmp_until_per-room'
|
|
|
|
@custom_commands = read_commands(@msgid)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
@bot.available_commands(self, @custom_commands.keys)
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 03:37:56 +00:00
|
|
|
# Provide help for the commands of this addon
|
|
|
|
#
|
|
|
|
# @param message [Message object] The relevant message object
|
|
|
|
def help_command(message)
|
|
|
|
pfx = @bot.cmd_prefix
|
|
|
|
cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '')
|
|
|
|
|
|
|
|
case cmd
|
|
|
|
when "addcommand"
|
2021-02-17 04:13:46 +00:00
|
|
|
cmd_add_usage
|
2021-02-15 03:37:56 +00:00
|
|
|
when "modcommand"
|
2021-02-17 04:13:46 +00:00
|
|
|
cmd_mod_usage
|
2021-02-15 03:37:56 +00:00
|
|
|
when "remcommand"
|
2021-02-17 04:13:46 +00:00
|
|
|
cmd_rem_usage
|
2021-02-15 03:37:56 +00:00
|
|
|
else
|
2021-02-17 04:13:46 +00:00
|
|
|
cmd_custom_usage(cmd)
|
2021-02-15 03:37:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# 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
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
room = @bot.client.ensure_room(message.room_id)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
room.send_notice(res)
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Add a new custom command
|
|
|
|
def addcmd(message)
|
|
|
|
command = message.slice!(/\w+\s+/).strip
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
return cmd_add_error(command) if verify_commands(command)
|
|
|
|
return cmd_addon_error if addon_command(command)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
@custom_commands[command] = message
|
|
|
|
@bot.available_commands(self, [command])
|
|
|
|
save_commands(@msgid)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
"New command saved: !#{command}"
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Adds a new custom command
|
|
|
|
#
|
|
|
|
# @param message [String] The command plus response
|
|
|
|
# @return
|
|
|
|
def handle_addcommand(message)
|
|
|
|
res = 'Usage: !addcommand NAME RESPONSE'
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
if message.split(/\s+/).count > 1
|
|
|
|
res = addcmd(message)
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
res
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# 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)
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
res
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Remove an existing custom command
|
|
|
|
#
|
|
|
|
# @param message [hash] The message data from Matrix
|
|
|
|
def handle_remcommand(message)
|
|
|
|
res = 'Usage: !remcommand NAME'
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
if message.split(/\s+/).count == 1
|
|
|
|
res = remcmd(message)
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
res
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Modify an existing custom command
|
|
|
|
def modcmd(message)
|
|
|
|
command = message.slice!(/\w+\s+/).strip
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
return cmd_mod_error(command) unless verify_commands(command)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
@custom_commands[command] = message
|
|
|
|
save_commands(@msgid)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
"!#{command} modified."
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Delete an existing custom command
|
|
|
|
def remcmd(message)
|
|
|
|
command = message.strip
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
return cmd_rem_error unless verify_commands(command)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
@custom_commands.delete(command)
|
|
|
|
@bot.disable_commands(command)
|
|
|
|
save_commands(@msgid)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
"!#{command} removed."
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Execute a custom command
|
|
|
|
def runcmd(command)
|
|
|
|
return cmd_mod_error(command) unless verify_commands(command)
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
@custom_commands[command]
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
private
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Check if a custom command conflicts with an existing addon command
|
|
|
|
def addon_command(command)
|
|
|
|
@bot.all_commands.keys.include?(command)
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# 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
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 03:37:56 +00:00
|
|
|
# Help message for addcommand
|
|
|
|
def cmd_add_usage
|
|
|
|
'Add a custom command. '\
|
|
|
|
"\nUsage: !addcommand COMMAND TEXT"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Help message for modcommand
|
|
|
|
def cmd_custom_usage(cmd)
|
|
|
|
'Prints text associated with the custom command'\
|
|
|
|
"\nUsage: !#{cmd}"
|
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Error message when trying to add an existing command
|
|
|
|
def cmd_addon_error
|
|
|
|
'This command is already used by another addon.'
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# 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
|
|
|
|
|
2021-02-15 03:37:56 +00:00
|
|
|
# Help message for modcommand
|
|
|
|
def cmd_mod_usage
|
|
|
|
'Modify a custom command. '\
|
|
|
|
"\nUsage: !modcommand EXISTING-COMMAND TEXT"
|
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Error message when trying to delete a non-existing command
|
|
|
|
def cmd_rem_error
|
|
|
|
'This custom command does not exist. '\
|
|
|
|
'Nothing to remove.'
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 03:37:56 +00:00
|
|
|
# Help message for modcommand
|
|
|
|
def cmd_rem_usage
|
|
|
|
'Remove a custom command. '\
|
|
|
|
"\nUsage: !remcommand EXISTING-COMMAND"
|
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
# Read the existing saved commands into memory
|
|
|
|
def read_commands(msgid)
|
|
|
|
cmds = {}
|
|
|
|
cmds_file = "#{msgid}_custom_commands.json"
|
2021-02-07 03:28:30 +00:00
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
if File.exist?(cmds_file) && !File.empty?(cmds_file)
|
|
|
|
File.open(cmds_file, 'r') do |f|
|
|
|
|
cmds = JSON.parse(f.read)
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-15 02:12:13 +00:00
|
|
|
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)
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
end
|
2021-02-15 02:12:13 +00:00
|
|
|
|
|
|
|
# Check if a command already exists
|
|
|
|
def verify_commands(command)
|
|
|
|
@custom_commands.keys.include?(command)
|
|
|
|
end
|
2021-02-07 03:28:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|