Skip to content

Instantly share code, notes, and snippets.

@clooth
Created December 25, 2011 23:06
Show Gist options
  • Save clooth/1519921 to your computer and use it in GitHub Desktop.
Save clooth/1519921 to your computer and use it in GitHub Desktop.
Channel specific Settings Plugin for Cinch
#
# Mirras Runescape IRC Bot
# Author: Clooth <[email protected]>
# Feature: Settings
#
require 'cinch/storage/yaml'
class Setting
@name = nil
@value = nil
@possible_values = []
@errors = nil
def initialize(name, value, possible_values)
@name = name
@value = value
@possible_values = possible_values
end
def set(value)
if @possible_values.include? value
@value = value
@errors = false
true
else
@errors = "Invalid value for #{@name}, possible values include: #{@possible_values.join(', ')}"
false
end
end
def to_s
@value
end
attr_reader :name, :value, :possible_values, :errors
end
class Settings
# Initial channel-specific settings
CHANNEL_SETTINGS = {
# Public replies for bot commands via the @ command prefix
"public" => Setting.new("public", "on", ["on", "off"])
}
# Error messages
ERR_UNKNOWN_SETTING_NAME = "Unknown setting name \"%s\"."
# Notice messsages
MSG_SETTING_SET_SUCCESSFULLY = "Successfully set \"%s\" to \"%s\"."
MSG_SETTING_GET_VALUE = "\"%s\" is currently set to \"%s\""
include Cinch::Plugin
include Authentication
def initialize(*args)
super
storage[:settings] = {}
end
# Example command formats:
# !set setting_name setting_value
# !get setting_name
match /set (.+) (.+)/i, :method => :set_setting
match /get (.+)/i, :method => :get_setting
# Set a setting for the current channel
def set_setting(m, setting_name, setting_value)
channel = m.channel
user = m.user
# Only ops of the channel or owners of the bot
# are allowed to set a value for a setting
if (is_admin?(user))
channel_name = channel.name
# Check if a settings object exists for this channel
# If not, create it with default settings
if storage[:settings][channel_name].nil?
create_initial_settings_for channel_name
end
channel_settings = storage[:settings][channel_name]
# Check if a setting object exists for the setting key
if channel_settings[setting_name].nil?
return m.reply(ERR_UNKNOWN_SETTING_NAME % setting_name)
end
# Attempt to set the value
if channel_settings[setting_name].set(setting_value) == true
return m.reply(MSG_SETTING_SET_SUCCESSFULLY % [setting_name, setting_value])
end
# If there were any errors, we're here still
return m.reply(channel_settings[setting_name].errors)
end
end
# Get a setting's value by its name for the current channel
def get_setting(m, setting_name)
if (is_admin?(m.user))
channel = m.channel.name
# Check if the setting exists
if storage[:settings][channel_name].nil?
create_initial_settings_for channel_name
end
channel_settings = storage[:settings][channel_name]
# Unknown setting name error
if channel_settings[setting_name].nil?
return m.reply(ERR_UNKNOWN_SETTING_NAME % setting_name)
end
# Setting was found
return m.reply(MSG_SETTING_GET_VALUE % [setting_name, channel_settings[setting_name]])
end
end
private
# Set up initial settings hash for the channel in question
def create_initial_settings_for(channel_name)
storage[:settings][channel_name] = CHANNEL_SETTINGS
storage.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment