Class: AdvancedRubyCommandHandler::Client

Inherits:
Discordrb::Bot
  • Object
show all
Defined in:
lib/advanced_ruby_command_handler/app/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands_dir: "commands", events_dir: "events", config_file: "config.yml") ⇒ Client

Returns a new instance of Client.



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
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 14

def initialize(commands_dir: "commands", events_dir: "events", config_file: "config.yml")
  FileUtils.mkdir_p [commands_dir, events_dir]
  @commands_dir = commands_dir
  @events_dir = events_dir
  @file_logger = AdvancedRubyCommandHandler::Logger.new(:file)
  @console_logger = AdvancedRubyCommandHandler::Logger.new(:console)

  base_data = YAML.dump({
                          :token => "",
                          :prefix => "",
                          :owners => []
                        })

  File.open(config_file, "w+") { |file| file.write(base_data) } unless File.exist? config_file

  @config = YAML.load_file(config_file)

  %i[token prefix owners].each do |prop|
    next if @config[prop] && !@config[prop].empty?

    @console_logger.error("You have to add '#{prop.to_s}' value in your config file")
    raise "'#{prop}' missing or empty"
  end

  super(:token => @config[:token])

  @commands = AdvancedRubyCommandHandler::CommandHandler.load_commands(self)
  AdvancedRubyCommandHandler::EventHandler.load_events(self)
                                          .each do |event|
    Events.method(event).call(self)
  end

  at_exit { @console_logger.info("Application exited") }
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def commands
  @commands
end

#commands_dirObject (readonly)

Returns the value of attribute commands_dir.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def commands_dir
  @commands_dir
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def config
  @config
end

#console_loggerObject (readonly)

Returns the value of attribute console_logger.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def console_logger
  @console_logger
end

#events_dirObject (readonly)

Returns the value of attribute events_dir.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def events_dir
  @events_dir
end

#file_loggerObject (readonly)

Returns the value of attribute file_logger.



12
13
14
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 12

def file_logger
  @file_logger
end

Instance Method Details

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/advanced_ruby_command_handler/app/client.rb', line 49

def run
  @console_logger.info("Client login!")

  Thread.new do
    @console_logger.info("Type '.exit' to turn off the bot")
    # @console_logger.info("Type '.reload' to reload the bot")
    loop do
      Process.exit!(true) if $stdin.gets.chomp == ".exit"
    end
  end

  super.run
end