diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 |
commit | 7f134adccb20abe773234acecf526c9951b418e8 (patch) | |
tree | eeb4b4f96a963591f244c8f661b84dd194cf1484 /module | |
parent | 7e7aa3278a9e478b02eb152575fb00ee0802ac81 (diff) | |
download | dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.gz dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.xz |
Improved namespace and directory structure.
Diffstat (limited to 'module')
-rw-r--r-- | module/admin.rb | 79 | ||||
-rw-r--r-- | module/module.rb | 25 | ||||
-rw-r--r-- | module/test.rb | 46 |
3 files changed, 150 insertions, 0 deletions
diff --git a/module/admin.rb b/module/admin.rb new file mode 100644 index 0000000..d59fd49 --- /dev/null +++ b/module/admin.rb @@ -0,0 +1,79 @@ +require_relative 'module' + +module Dinobot + module Module + class Admin < Module + def initialize(bot) + super + + @commands << :join << :part << :load << :unload + @commands << :listadmins << :listmodules << :listchannels + + @admins = Array.new + end + + def add(user) + @admins << user unless @admins.include?(user) + end + + def remove(user) + @admins.delete(user) + end + + def is_admin?(user) + # FIXME: Using hostname for testing purposes. Need better solution. + @admins.include?(user.sub(/.+@/, '')) + end + + def join(user, channel, argument) + return unless is_admin?(user) + + [[:join, argument.strip]] + end + + def part(user, channel, argument) + return unless is_admin?(user) + + [[:part, argument.strip]] + end + + def listadmins(user, channel, argument) + return unless is_admin?(user) + + [[:say, channel, @admins.join(' ')]] + end + + def load(user, channel, argument) + return unless is_admin?(user) + + argument.split.each do |x| + @bot.load_module x.intern + end + + nil + end + + def unload(user, channel, argument) + return unless is_admin?(user) + + argument.split.each do |x| + @bot.unload_module x.intern + end + + nil + end + + def listmodules(user, channel, argument) + return unless is_admin?(user) + + [[:say, channel, "Modules: #{@bot.modules.keys.sort.join(' ')}"]] + end + + def listchannels(user, channel, argument) + return unless is_admin?(user) + + [[:say, channel, "Channels: #{@bot.channels.sort.join(' ')}"]] + end + end + end +end diff --git a/module/module.rb b/module/module.rb new file mode 100644 index 0000000..9d72b09 --- /dev/null +++ b/module/module.rb @@ -0,0 +1,25 @@ +module Dinobot + module Module + class Module + attr_accessor :commands + + def initialize(bot) + @bot = bot + + @commands = [:commands] + end + + def call(user, channel, message) + command, argument = message.split(' ', 3)[1..2] + + if @commands.map { |x| x.to_s }.include?(command) + send(command, user, channel, argument) + end + end + + def commands(user, channel, argument) + [[:say, channel, "Commands: #{@commands.sort.join(' ')}"]] + end + end + end +end diff --git a/module/test.rb b/module/test.rb new file mode 100644 index 0000000..e1d8358 --- /dev/null +++ b/module/test.rb @@ -0,0 +1,46 @@ +require_relative 'module' + +module Dinobot + module Module + class Test < Module + def initialize(bot) + super + + @commands << :echo << :ping << :x3 << :fooify + @commands << :error << :timeout << :wrongreturn << :invalidmethods + end + + def echo(user, channel, argument) + [[:say, channel, argument]] + end + + def ping(user, channel, argument) + [[:say, channel, 'pong']] + end + + def x3(user, channel, argument) + [[:say, channel, argument]] * 3 + end + + def fooify(user, channel, argument) + [[:say, channel, 'foo' + argument]] + end + + def error(user, channel, argument) + x + end + + def timeout(user, channel, argument) + sleep 60 + end + + def wrongreturn(user, channel, argument) + 0 + end + + def invalidmethods(user, channel, argument) + [[:say, channel]] + end + end + end +end |