Class: CommandrbBot

Inherits:
Object
  • Object
show all
Defined in:
lib/commandrb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_hash) ⇒ CommandrbBot


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/commandrb.rb', line 9

def initialize(init_hash)

  @commands = {}
  @prefixes = []
  @config = init_hash

  @config[:prefix_type] = 'rescue' if @config[:prefix_type].nil?
  @config[:typing_default] =  false if @config[:typing_default].nil?

  if @config[:token].nil? or init_hash[:token] == ''
    puts 'No token supplied in init hash!'
    return false
  end

  init_parse_self = init_hash[:parse_self] rescue nil
  init_type = init_hash[:type] rescue :bot

  if init_type == :bot
    if init_hash[:client_id].nil?
      puts 'No client ID or invalid client ID supplied in init hash!'
      return false
    end
  end

  @prefixes = []

  @config[:owners] = init_hash[:owners]
  puts 'Invalid owners supplied in init hash!'

  @prefixes = init_hash[:prefixes]
  puts 'Invalid prefixes supplied in init hash!'

  @bot = Discordrb::Bot.new(
      token: @config[:token],
      client_id: @config[:client_id],
      parse_self: init_parse_self,
      type: init_type
  )

  unless init_hash[:ready].nil?
    @bot.ready do |event|
      init_hash[:ready].call(event)
    end
  end

  def add_command(name, attributes = {})
    @commands[name.to_sym] = attributes
  end

  # Command processing
  @bot.message do |event|
    @continue = false
    @prefixes.each { |prefix|
      if event.message.content.start_with?(prefix)

        @commands.each { | key, command |
          if command[:triggers].nil?
            triggers = [key.to_s]
          else
            triggers = command[:triggers]
          end

          triggers.each { |trigger|
            @activator = prefix + trigger
            @activator = @activator.downcase
            if event.message.content.downcase.start_with?(@activator)
              @continue = true
              break
            else
              next
            end
          }

          next unless @continue

          # Command flag defaults
          command[:catch_errors] = @config[:catch_errors] if command[:catch_errors].nil?
          command[:owners_only] = false if command[:owners_only].nil?
          command[:max_args] = 2000 if command[:max_args].nil?
          command[:server_only] = false if command[:server_only].nil?
          command[:typing] = @config[:typing_default] if command[:typing_default].nil?

          if command[:owners_only]
            unless YuukiBot.config['owners'].include?(event.user.id)
              event.respond('❌ You don\'t have permission for that!')
              break
            end
          end

          if command[:server_only] && event.channel.private?
            event.respond('❌ This command will only work in servers!')
            next
          end

          if (event.user.bot_account? && command[:parse_bots] == false) || (event.user.bot_account? && @config[:parse_bots] == false)
            next
          end

          event.channel.start_typing if command[:typing]

          args = event.message.content.slice!(@activator.length, event.message.content.size)
          args = args.split(' ')

          if args.length > command[:max_args]
            event.respond("❌ Too many arguments! \nMax arguments: `#{command[:max_args]}`")
            next
          end
          if !command[:catch_errors] || @config['catch_errors']
            command[:code].call(event, args)
          else
            begin
              command[:code].call(event, args)
            rescue Exception => e
              event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
            end
          end
          break
        }
        break
      end
    }
  end
end

Instance Attribute Details

#botObject

Returns the value of attribute bot


4
5
6
# File 'lib/commandrb.rb', line 4

def bot
  @bot
end

#commandsObject

Returns the value of attribute commands


2
3
4
# File 'lib/commandrb.rb', line 2

def commands
  @commands
end

#ownersObject

Returns the value of attribute owners


6
7
8
# File 'lib/commandrb.rb', line 6

def owners
  @owners
end

#prefix_typeObject

Returns the value of attribute prefix_type


5
6
7
# File 'lib/commandrb.rb', line 5

def prefix_type
  @prefix_type
end

#prefixesObject

Returns the value of attribute prefixes


3
4
5
# File 'lib/commandrb.rb', line 3

def prefixes
  @prefixes
end

#typing_defaultObject

Returns the value of attribute typing_default


7
8
9
# File 'lib/commandrb.rb', line 7

def typing_default
  @typing_default
end

Instance Method Details

#add_command(name, attributes = {}) ⇒ Object


54
55
56
# File 'lib/commandrb.rb', line 54

def add_command(name, attributes = {})
  @commands[name.to_sym] = attributes
end