Class: Cogs::BaseBotClass

Inherits:
Discordrb::Commands::CommandBot
  • Object
show all
Defined in:
lib/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, prefix) ⇒ BaseBotClass

Returns a new instance of BaseBotClass.



5
6
7
8
9
10
# File 'lib/bot.rb', line 5

def initialize(token, prefix)
  super token: token, prefix: prefix
  @cached_cogs = {}
  @loaded_cogs = {}

end

Instance Attribute Details

#cached_cogsObject (readonly)

Returns the value of attribute cached_cogs.



13
14
15
# File 'lib/bot.rb', line 13

def cached_cogs
  @cached_cogs
end

#cogsObject

Returns the value of attribute cogs.



12
13
14
# File 'lib/bot.rb', line 12

def cogs
  @cogs
end

#loaded_cogsObject (readonly)

Returns the value of attribute loaded_cogs.



14
15
16
# File 'lib/bot.rb', line 14

def loaded_cogs
  @loaded_cogs
end

Instance Method Details

#_remove_all_cog_commands(cogname) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/bot.rb', line 47

def _remove_all_cog_commands(cogname)
  @commands ||= {}
  @commands.each do |cmd|
    if cmd[1]::cog == cogname
      self.remove_command cmd[1]::name
      p cmd[1]::name
    end
  end
end

#add_cog(cog) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/bot.rb', line 72

def add_cog(cog)
  begin
    cog = cog.new bot: self
    cog.commands
    return cog
  rescue NoMethodError => error
    self.log_exception(error)
  end
end

#add_commandsObject



26
# File 'lib/bot.rb', line 26

def add_commands; end

#afterObject



28
# File 'lib/bot.rb', line 28

def after; end

#beforeObject



27
# File 'lib/bot.rb', line 27

def before; end

#command(name, attributes = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bot.rb', line 30

def command(name, attributes = {}, &block)
  @commands ||= {}

  if name.is_a?(Array)
    name, *aliases = name
    attributes[:aliases] = aliases if attributes[:aliases].nil?
    Discordrb::LOGGER.warn("While registering command #{name.inspect}")
    Discordrb::LOGGER.warn('Arrays for command aliases is removed. Please use `aliases` argument instead.')
  end

  new_command = Cogs::Command.new(self, name, attributes, &block)
  new_command.attributes[:aliases].each do |aliased_name|
    @commands[aliased_name] = CommandAlias.new(aliased_name, new_command)
  end
  @commands[name] = new_command
end

#load_cog(cog) ⇒ Object



109
110
111
# File 'lib/bot.rb', line 109

def load_cog(cog)
  self.reload_cog(cog)
end

#load_extension(fp) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bot.rb', line 57

def load_extension(fp)
  fp = fp + ".rb" unless fp.end_with?".rb"
  raise Cogs::CogError.new "Nonexistant filepath #{fp}" unless File.file?fp + ".rb" unless fp.end_with?".rb"
  begin
    load fp
    cog = setup(self)
    cog::fp = fp
    cog::name = cog.to_s[2..].split(':')[0]
    @loaded_cogs[cog::name] = cog
    @cached_cogs[cog::name] = cog.class
  rescue LoadError, NoMethodError => error
    self.log_exception(error)
  end
end

#reload_cog(cog) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/bot.rb', line 100

def reload_cog(cog)
  raise Cogs::CogError.new "Cog not found" unless @cached_cogs.include?cog or @loaded_cogs.include?cog
  if @loaded_cogs.include?cog
    self.unload_cog(cogname: cog)
  end
  self.load_extension(@loaded_cogs[cog]::fp)
  Discordrb::LOGGER.info "Loaded cog #{cog}"
end

#run_botObject



16
17
18
19
20
21
22
23
24
# File 'lib/bot.rb', line 16

def run_bot
  self.before
  self.add_commands
  begin
    self.run
  ensure
    self.after
  end
end

#unload_cog(cog: nil, cogname: "") ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bot.rb', line 82

def unload_cog(cog: nil, cogname: "")
  if cog.is_a?Cogs::Cog
    if @cached_cogs.keys.include? cog::name
      @loaded_cogs = @loaded_cogs.tap { |key| delete(key) if key == cog::name }
      self._remove_all_cog_commands(cog::name)
      Discordrb::LOGGER.info "Unloaded cog #{cog::name}"
      return
    end
  end
  if @loaded_cogs.keys.include?cogname
    @loaded_cogs = @loaded_cogs.tap { |key| delete(key) if key == cogname }
    self._remove_all_cog_commands(cogname)
    Discordrb::LOGGER.info "Unloaded cog #{cogname}"
    return
  end
  raise Cogs::CogError.new "Cog already unloaded"
end