class BotOptionsPlugin
Constants
- DEFAULT_OPTIONS
Public Class Methods
Source
# File plugins/bot_options.rb, line 95 def self.admin_allowed?(network:, channel:, nick:, is_op:) RabbotDb.user_allowed_at_level?( network: network, channel: channel, nick: nick, is_op: is_op, minimum: RabbotDb::LEVEL_ADMIN ) end
Source
# File plugins/bot_options.rb, line 62 def self.allowed_key?(key) key.to_s.match?(/\A[a-z][a-z0-9_]{0,40}\z/) end
Source
# File plugins/bot_options.rb, line 105 def self.denied_message "Only channel ops and registered admins can manage bot options." end
Source
# File plugins/bot_options.rb, line 109 def self.format_options(network:, channel:, yaml_default: nil) DEFAULT_OPTIONS.map do |key, default| value = option_value(network: network, channel: channel, key: key, default: default, yaml_default: yaml_default) "#{key}=#{value}" end.join(", ") end
Source
# File plugins/bot_options.rb, line 37 def self.nick_candidates(bot) ([bot.nick, bot.config.nick] + Array(bot.config.nicks)).compact.map(&:to_s).reject(&:empty?).uniq end
Source
# File plugins/bot_options.rb, line 116 def self.option_value(network:, channel:, key:, default:, yaml_default: nil) if key == AiModel::KEY fallback = default.to_s.strip.empty? ? AiModel::DEFAULT : default return AiModel.resolve(network: network, channel: channel, yaml_default: yaml_default, fallback: fallback) end BotOptions.get(network: network, channel: channel, key: key, default: default) end
Source
# File plugins/bot_options.rb, line 66 def self.parse_command(args) parts = args.to_s.strip.split(/\s+/, 3) return { error: usage_message } if parts.empty? || parts[0].to_s.empty? action = parts[0].downcase case action when "list" { action: :list } when "get" key = parts[1].to_s return { error: usage_message } unless allowed_key?(key) { action: :get, key: key } when "set" key = parts[1].to_s value = parts[2].to_s.strip return { error: usage_message } unless allowed_key?(key) && !value.empty? { action: :set, key: key, value: value } when "unset" key = parts[1].to_s return { error: usage_message } unless allowed_key?(key) { action: :unset, key: key } else { error: usage_message } end end
Source
# File plugins/bot_options.rb, line 41 def self.parse_nick_option_args(message, bot:) text = message.to_s.lstrip return nil if text.empty? nick_candidates(bot).each do |nick| if (match = text.match(/\A!#{Regexp.escape(nick)}\s+option\s+(.+)\z/i)) return match[1] end if (match = text.match(/\A!#{Regexp.escape(nick)}\s+(list)\z/i)) return "list" end if (match = text.match(/\A!#{Regexp.escape(nick)}\s+(get|set|unset)\s+(.+)\z/i)) return "#{match[1].downcase} #{match[2].strip}" end end nil end
Source
# File plugins/bot_options.rb, line 125 def self.seed_ai_model!(network:, channels:, yaml_default:) model = yaml_default.to_s.strip return if model.empty? Array(channels).each do |channel| next if channel.to_s.strip.empty? existing = BotOptions.get(network: network, channel: channel, key: AiModel::KEY, default: nil) next unless existing.nil? || existing.to_s.strip.empty? BotOptions.set(network: network, channel: channel, key: AiModel::KEY, value: model) end end
Source
# File plugins/bot_options.rb, line 139 def self.set_option_on_all_channels!(network:, channels:, key:, value:) Array(channels).each do |channel| next if channel.to_s.strip.empty? BotOptions.set(network: network, channel: channel, key: key, value: value) end end
Source
# File plugins/bot_options.rb, line 147 def self.unset_option_on_all_channels!(network:, channels:, key:) Array(channels).each do |channel| next if channel.to_s.strip.empty? BotOptions.unset(network: network, channel: channel, key: key) end end
Source
# File plugins/bot_options.rb, line 33 def self.usage_message "Usage: !bot option <list|get|set|unset> [key] [value] — or !<nick> <list|get|set|unset> ..." end
Public Instance Methods
Source
# File plugins/bot_options.rb, line 183 def execute(m, args) network = bot.config.server channel = m.channel.name RabbotUser.seed_channel_defaults(network: network, channel: channel) unless self.class.admin_allowed?(network: network, channel: channel, nick: m.user.nick, is_op: m.channel.opped?(m.user)) m.reply self.class.denied_message return end parsed = self.class.parse_command(args) if parsed[:error] m.reply parsed[:error] return end case parsed[:action] when :list flood_safe_reply( m, self.class.format_options( network: network, channel: channel, yaml_default: bot.config.shared[:ai_model] ) ) when :get default = DEFAULT_OPTIONS[parsed[:key]] value = self.class.option_value( network: network, channel: channel, key: parsed[:key], default: default, yaml_default: bot.config.shared[:ai_model] ) m.reply "#{parsed[:key]}=#{value}" when :set if parsed[:key] == AiModel::KEY self.class.set_option_on_all_channels!( network: network, channels: bot.config.channels, key: parsed[:key], value: parsed[:value] ) else BotOptions.set(network: network, channel: channel, key: parsed[:key], value: parsed[:value]) end m.reply "Set #{parsed[:key]}=#{parsed[:value]}" when :unset if parsed[:key] == AiModel::KEY self.class.unset_option_on_all_channels!( network: network, channels: bot.config.channels, key: parsed[:key] ) else BotOptions.unset(network: network, channel: channel, key: parsed[:key]) end m.reply "Unset #{parsed[:key]}" end end
Source
# File plugins/bot_options.rb, line 155 def on_connect(_m) RabbotUser.seed_configured_channels(network: bot.config.server, channels: bot.config.channels) RabbotUser.join_configured_channels(bot) self.class.seed_ai_model!( network: bot.config.server, channels: bot.config.channels, yaml_default: bot.config.shared[:ai_model] ) end
Source
# File plugins/bot_options.rb, line 165 def on_invite(m) channel_name = m.channel&.name || m.params[1] return if channel_name.to_s.strip.empty? return unless RabbotUser.join_invited_channel(bot, channel_name) RabbotUser.seed_channel_defaults(network: bot.config.server, channel: channel_name) end
Source
# File plugins/bot_options.rb, line 173 def try_nick_option_command(m) return unless m.channel return if m.action? args = self.class.parse_nick_option_args(m.message, bot: bot) return unless args execute(m, args) end