advanced_ruby_command_handler 0.1.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -1
  3. data/Gemfile +8 -0
  4. data/Gemfile.lock +22 -12
  5. data/advanced_ruby_command_handler.gemspec +1 -2
  6. data/lib/advanced_ruby_command_handler/base.rb +51 -0
  7. data/lib/advanced_ruby_command_handler/client.rb +20 -0
  8. data/lib/advanced_ruby_command_handler/command.rb +65 -0
  9. data/lib/advanced_ruby_command_handler/command_handler.rb +76 -0
  10. data/lib/advanced_ruby_command_handler/database.rb +16 -0
  11. data/lib/advanced_ruby_command_handler/defaults/events/message.rb +17 -0
  12. data/lib/advanced_ruby_command_handler/errors.rb +48 -0
  13. data/lib/advanced_ruby_command_handler/event.rb +9 -0
  14. data/lib/advanced_ruby_command_handler/logger.rb +38 -0
  15. data/lib/advanced_ruby_command_handler/utils.rb +9 -0
  16. data/lib/advanced_ruby_command_handler/version.rb +5 -5
  17. data/lib/advanced_ruby_command_handler.rb +16 -12
  18. data/sig/lib/advanced_ruby_command_handler/base.rbs +21 -0
  19. data/sig/lib/advanced_ruby_command_handler/database.rbs +7 -0
  20. data/sig/lib/advanced_ruby_command_handler/errors.rbs +19 -0
  21. data/tests/index.rb +16 -2
  22. data/tests/src/commands/tests/test.rb +13 -0
  23. data/tests/src/events/ready.rb +9 -0
  24. metadata +32 -17
  25. data/advanced_ruby_command_handler-0.1.1.gem +0 -0
  26. data/advanced_ruby_command_handler-0.1.2.gem +0 -0
  27. data/lib/advanced_ruby_command_handler/app/app.rb +0 -7
  28. data/lib/advanced_ruby_command_handler/app/client.rb +0 -59
  29. data/lib/advanced_ruby_command_handler/app/command.rb +0 -12
  30. data/lib/advanced_ruby_command_handler/app/command_handler.rb +0 -23
  31. data/lib/advanced_ruby_command_handler/app/defaults/events/message.rb +0 -17
  32. data/lib/advanced_ruby_command_handler/app/defaults/events/ready.rb +0 -9
  33. data/lib/advanced_ruby_command_handler/app/event_handler.rb +0 -27
  34. data/lib/advanced_ruby_command_handler/app/logger.rb +0 -63
  35. data/lib/advanced_ruby_command_handler/app/utils.rb +0 -11
  36. data/tests/commands/test/test.rb +0 -13
  37. data/tests/config.yml +0 -4
  38. data/tests/events/ready.rb +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97c6a7076b47b7d2dda46986b94f2972363dc81c17c55d619a7d668e8c39df09
4
- data.tar.gz: 3b6a580272ef1e6f5f86b49719fc50c5616aeb2a729592b913ea45ffb88857d3
3
+ metadata.gz: 8582a5dd17fdc5237ae0e4a1eae97868ec5d5c17f55f35a66edd38128a6d3246
4
+ data.tar.gz: 94e2882c42b578f1a05b85731828bbef17a652c2aecbfc87c8ab3861293b3909
5
5
  SHA512:
6
- metadata.gz: e8133e6028b5acb9095b4e48a653db545840091de10e4d0c6af5d390c21f2aab4762b1250a951ebf9f3545b81d31fd7a905a5f7ae39f7edd680780e70b8a7fcc
7
- data.tar.gz: 52d14801db9f0d424dd18a13b732e16fdcb504131c966b6ce243c12f3f8d4c961da6e5563445b7beda70ab0edfe008d1d75dedffa5795db1176404534287f0cf
6
+ metadata.gz: 0bb6817906ef0218909f3a440257eef6ea6596d4f45b53f67a108ac26fe8f8e5904376cd25d3b12ee0d24a8bead2c54519fc3760b4b83a2e7fbac0bd4740d129
7
+ data.tar.gz: '008acad9ad58e3bf93f1b940fa44ae472e2d3fd0f2857b281a040e68f62e6783d7069940e89a3157408117f6c61decd5c58834946ddd5aa83acb4d0b48178f62'
data/.gitignore CHANGED
@@ -6,4 +6,8 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /.idea/
9
+ /.idea/
10
+ *.gem
11
+ *.yml
12
+ /sig/
13
+ *.env
data/Gemfile CHANGED
@@ -5,8 +5,16 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in advanced_ruby_command_handler.gemspec
6
6
  gemspec
7
7
 
8
+ gem "rbs"
9
+
8
10
  gem "rake", "~> 13.0"
9
11
 
10
12
  gem "rubocop", "~> 1.7"
11
13
 
12
14
  gem "discordrb"
15
+
16
+ gem "mongo"
17
+
18
+ gem "dotenv", "~> 2.7", ">= 2.7.6"
19
+
20
+ gem "event_emitter"
data/Gemfile.lock CHANGED
@@ -1,12 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- advanced_ruby_command_handler (0.1.3)
4
+ advanced_ruby_command_handler (2.0.0)
5
+ discordrb
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  ast (2.4.2)
11
+ bson (4.12.1)
10
12
  discordrb (3.4.0)
11
13
  discordrb-webhooks (~> 3.3.0)
12
14
  ffi (>= 1.9.24)
@@ -17,22 +19,26 @@ GEM
17
19
  rest-client (>= 2.1.0.rc1)
18
20
  domain_name (0.5.20190701)
19
21
  unf (>= 0.0.5, < 1.0.0)
22
+ dotenv (2.7.6)
20
23
  event_emitter (0.2.6)
21
- ffi (1.15.0-x64-mingw32)
24
+ ffi (1.15.4-x64-mingw32)
22
25
  http-accept (1.7.0)
23
- http-cookie (1.0.3)
26
+ http-cookie (1.0.4)
24
27
  domain_name (~> 0.5)
25
28
  mime-types (3.3.1)
26
29
  mime-types-data (~> 3.2015)
27
- mime-types-data (3.2020.1104)
30
+ mime-types-data (3.2021.0901)
31
+ mongo (2.15.1)
32
+ bson (>= 4.8.2, < 5.0.0)
28
33
  netrc (0.11.0)
29
34
  opus-ruby (1.0.1)
30
35
  ffi
31
- parallel (1.20.1)
32
- parser (3.0.1.1)
36
+ parallel (1.21.0)
37
+ parser (3.0.2.0)
33
38
  ast (~> 2.4.1)
34
39
  rainbow (3.0.0)
35
- rake (13.0.3)
40
+ rake (13.0.6)
41
+ rbs (1.6.2)
36
42
  regexp_parser (2.1.1)
37
43
  rest-client (2.1.0-x64-mingw32)
38
44
  ffi (~> 1.9)
@@ -41,22 +47,22 @@ GEM
41
47
  mime-types (>= 1.16, < 4.0)
42
48
  netrc (~> 0.8)
43
49
  rexml (3.2.5)
44
- rubocop (1.18.1)
50
+ rubocop (1.22.1)
45
51
  parallel (~> 1.10)
46
52
  parser (>= 3.0.0.0)
47
53
  rainbow (>= 2.2.2, < 4.0)
48
54
  regexp_parser (>= 1.8, < 3.0)
49
55
  rexml
50
- rubocop-ast (>= 1.7.0, < 2.0)
56
+ rubocop-ast (>= 1.12.0, < 2.0)
51
57
  ruby-progressbar (~> 1.7)
52
58
  unicode-display_width (>= 1.4.0, < 3.0)
53
- rubocop-ast (1.7.0)
59
+ rubocop-ast (1.12.0)
54
60
  parser (>= 3.0.1.1)
55
61
  ruby-progressbar (1.11.0)
56
62
  unf (0.1.4)
57
63
  unf_ext
58
- unf_ext (0.0.7.7)
59
- unicode-display_width (2.0.0)
64
+ unf_ext (0.0.8-x64-mingw32)
65
+ unicode-display_width (2.1.0)
60
66
  websocket (1.2.9)
61
67
  websocket-client-simple (0.3.0)
62
68
  event_emitter
@@ -68,7 +74,11 @@ PLATFORMS
68
74
  DEPENDENCIES
69
75
  advanced_ruby_command_handler!
70
76
  discordrb
77
+ dotenv (~> 2.7, >= 2.7.6)
78
+ event_emitter
79
+ mongo
71
80
  rake (~> 13.0)
81
+ rbs
72
82
  rubocop (~> 1.7)
73
83
 
74
84
  BUNDLED WITH
@@ -29,8 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
32
+ spec.add_dependency "discordrb"
34
33
 
35
34
  # For more information and examples about making a new gem, checkout our
36
35
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dotenv/load"
4
+ require "fileutils"
5
+
6
+ class BaseDatabase
7
+ attr_reader :client
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ def to_s
14
+ @client.database.name
15
+ end
16
+
17
+ def options
18
+ @client.hash
19
+ end
20
+ end
21
+
22
+ class BaseClient < Discordrb::Bot
23
+ def initialize
24
+ %w[BOT_TOKEN BOT_BASE_PREFIX BOT_OWNER].each do |environment_variable|
25
+ raise Errors::MissingEnvironmentVariable.new(environment_variable, "Environment variable '#{environment_variable}' missing in your .env file") unless ENV[environment_variable]
26
+ end
27
+ super(:token => ENV["BOT_TOKEN"], :ignore_bots => true)
28
+ end
29
+ end
30
+
31
+ class BaseCommandHandler
32
+ attr_accessor :commands_dir, :events_dir
33
+ attr_reader :commands_module_name, :events_module_name
34
+
35
+ def initialize(options)
36
+ %i[commands_dir events_dir commands_module_name events_module_name].each do |dir|
37
+ raise Errors::MissingParameter.new(dir, "Parameter '#{dir}' unspecified") unless options[dir]
38
+ end
39
+ @commands_dir = options[:commands_dir]
40
+ @events_dir = options[:events_dir]
41
+ @commands_module_name = options[:commands_module_name]
42
+ @events_module_name = options[:events_module_name]
43
+ FileUtils.mkdir_p [@commands_dir, @events_dir]
44
+ end
45
+
46
+ def get_module_by_string(module_name)
47
+ raise Errors::NonExistingModule.new(module_name, "Module '#{module_name}' doesn't exist. Please specify an existing module") unless Object.const_defined?(module_name)
48
+
49
+ Object.const_get(module_name)
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "yaml"
5
+ require File.expand_path("base", File.dirname(__FILE__))
6
+
7
+ module AdvancedRubyCommandHandler
8
+ class Client < BaseClient
9
+ def run
10
+ Logger.check("Client run")
11
+ Thread.new do
12
+ Logger.info("Type '.exit' to turn off the bot")
13
+ loop do
14
+ Process.exit!(true) if $stdin.gets.chomp == ".exit"
15
+ end
16
+ end
17
+ super.run
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Command
4
+ attr_reader :run
5
+
6
+ def initialize(props, &block)
7
+ @run = block
8
+ load_proprieties(props)
9
+ end
10
+
11
+ def load_proprieties(props)
12
+ command_props = %i[
13
+ name
14
+ aliases
15
+ description
16
+ category
17
+ usage
18
+ args
19
+ strict_args
20
+ cooldown
21
+ owner_only
22
+ server_only
23
+ member_permissions
24
+ bot_permissions
25
+ ]
26
+ command_props.each do |prop|
27
+ Command.attr_accessor prop
28
+ end
29
+ @name = props[:name]
30
+ @aliases = props[:aliases] if props[:aliases]
31
+ @description = props[:description] if props[:description]
32
+ @category = props[:category] if props[:category]
33
+ @usage = props[:usage] if props[:usage]
34
+ @args = props[:args] if props[:args]
35
+ @strict_args = props[:strict_args] if props[:strict_args]
36
+ @cooldown = props[:cooldown] if props[:cooldown]
37
+ @owner_only = true if props[:owner_only]
38
+ @server_only = true if props[:server_only]
39
+
40
+ client_base_permissions = %i[
41
+ add_reactions
42
+ send_messages
43
+ embed_links
44
+ attach_files
45
+ use_external_emoji
46
+ ]
47
+ case props[:bot_permissions]
48
+ when String
49
+ permissions = props[:bot_permissions].split(/\s+/).map(&:to_sym)
50
+ client_base_permissions.each { |p| permissions << p unless permissions.include?(p) }
51
+ @bot_permissions = permissions
52
+ when Array
53
+ permissions = props[:bot_permissions].map(&:to_sym)
54
+ client_base_permissions.each { |p| permissions << p unless permissions.include?(p) }
55
+ @bot_permissions = permissions
56
+ else @bot_permissions = client_base_permissions end
57
+
58
+ @member_permissions = case props[:member_permissions]
59
+ when String
60
+ props[:member_permissions].split(/\s+/).map(&:to_sym)
61
+ else [] end
62
+ command_props.each { |p| instance_variable_set(:"@#{p}", false) unless instance_variable_get(:"@#{p}") }
63
+ end
64
+ private :load_proprieties
65
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "event_emitter"
4
+ require File.expand_path("base", File.dirname(__FILE__))
5
+ require File.expand_path("utils", File.dirname(__FILE__))
6
+
7
+ class CommandHandler < BaseCommandHandler
8
+ include EventEmitter
9
+ include Utils
10
+ attr_reader :client, :commands, :events, :commands_module, :events_module, :loaded_events
11
+
12
+ def initialize(options)
13
+ super(options)
14
+ @client = AdvancedRubyCommandHandler::Client.new
15
+ @loaded_events = []
16
+
17
+ Thread.new do
18
+ Logger.info("Type '.reload' to load/reload all the commands & events files")
19
+ loop do
20
+ next unless $stdin.gets.chomp == ".reload"
21
+
22
+ load_commands
23
+ load_events
24
+ Logger.check("All commands & events loaded")
25
+ end
26
+ end
27
+ end
28
+
29
+ def load_commands
30
+ @commands = []
31
+ Dir.entries(@commands_dir).each do |dir|
32
+ next if %w[. ..].include?(dir)
33
+
34
+ Dir.entries("#{@commands_dir}#{dir}").each do |file|
35
+ next if %w[. ..].include?(file)
36
+
37
+ load File.join(@commands_dir, dir, file)
38
+ @commands << File.basename(file, ".rb")
39
+ end
40
+ end
41
+ @commands_module = get_module_by_string(@commands_module_name)
42
+
43
+ emit :commands_loaded, @commands
44
+ self
45
+ end
46
+
47
+ def load_events
48
+ @events = []
49
+ Dir.entries(@events_dir).each do |file|
50
+ next if %w[. ..].include?(file)
51
+
52
+ load File.join(@events_dir, file)
53
+ @events << File.basename(file, ".rb")
54
+ end
55
+
56
+ Dir["#{File.dirname(__FILE__)}/defaults/events/*.rb"].sort.each do |default_event|
57
+ next if @loaded_events.include?(File.basename(default_event, ".rb")) || @events.include?(File.basename(default_event, ".rb"))
58
+
59
+ require default_event
60
+
61
+ @events << File.basename(default_event, ".rb")
62
+ end
63
+ @event_module = get_module_by_string(@events_module_name)
64
+
65
+ emit :events_loaded, @events
66
+
67
+ @events.each do |event|
68
+ next if @loaded_events.include?(event)
69
+
70
+ @event_module.method(event).call(self)
71
+ @loaded_events << event
72
+ end
73
+
74
+ self
75
+ end
76
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mongo"
4
+ require File.expand_path("base", File.dirname(__FILE__))
5
+
6
+ module AdvancedRubyCommandHandler
7
+ class Database < BaseDatabase
8
+ attr_accessor :database, :path
9
+
10
+ def initialize(path)
11
+ @path = path
12
+ @database = Mongo::Client.new(path)
13
+ super(@database)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Events
4
+ def self.message(command_handler)
5
+ Event.new(:message, command_handler.client) do |message|
6
+ next unless message.content.start_with?(ENV["BOT_BASE_PREFIX"])
7
+
8
+ args = message.content.slice(ENV["BOT_BASE_PREFIX"].size, message.content.size).strip.split(/\s+/)
9
+ command = command_handler.find_command(args.shift)
10
+
11
+ next unless command
12
+
13
+ command.run.call(command_handler.client, message.message)
14
+ Logger.check("Command #{command.name} used!")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errors
4
+ class LoadCommandError < RuntimeError
5
+ attr_reader :command
6
+
7
+ def initialize(command, message = nil)
8
+ super(message)
9
+ @command = command
10
+ end
11
+ end
12
+
13
+ class LoadEventError < RuntimeError
14
+ attr_reader :event
15
+
16
+ def initialize(event, message = nil)
17
+ super(message)
18
+ @event = event
19
+ end
20
+ end
21
+
22
+ class MissingEnvironmentVariable < RuntimeError
23
+ attr_reader :environment_variable
24
+
25
+ def initialize(environment_variable, message = nil)
26
+ super(message)
27
+ @environment_variable = environment_variable
28
+ end
29
+ end
30
+
31
+ class MissingParameter < RuntimeError
32
+ attr_reader :parameter
33
+
34
+ def initialize(parameter, message = nil)
35
+ super(message)
36
+ @parameter = parameter
37
+ end
38
+ end
39
+
40
+ class NonExistingModule < RuntimeError
41
+ attr_reader :module_name
42
+
43
+ def initialize(module_name, message = nil)
44
+ super(message)
45
+ @module_name = module_name
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Event
4
+ def initialize(event, client, &block)
5
+ return unless client.methods.include?(event)
6
+
7
+ client.method(event).call { |e| block.call(e) }
8
+ end
9
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Logger
4
+ COLORS = {
5
+ :default => "\e[38m",
6
+ :white => "\e[39m",
7
+ :black => "\e[30m",
8
+ :red => "\e[31m",
9
+ :green => "\e[32m",
10
+ :brown => "\e[33m",
11
+ :blue => "\e[34m",
12
+ :magenta => "\e[35m",
13
+ :cyan => "\e[36m",
14
+ :gray => "\e[37m",
15
+ :yellow => "\e[33m"
16
+ }.freeze
17
+ MODES = {
18
+ :info => :cyan,
19
+ :error => :red,
20
+ :warn => :yellow,
21
+ :check => :green
22
+ }.freeze
23
+
24
+ class << self
25
+ def console_color(color, message)
26
+ "#{COLORS[color]}#{message}\e[0m"
27
+ end
28
+
29
+ private :console_color
30
+ end
31
+
32
+ MODES.each do |mode, color|
33
+ define_singleton_method(mode) do |message|
34
+ time = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
35
+ puts "[#{console_color(:magenta, time)}] - [#{console_color(color, mode.to_s.upcase)}] : #{message.to_s}"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utils
4
+ def find_command(name)
5
+ return unless commands_module.methods.include?(name.to_sym)
6
+
7
+ commands_module.method(name.to_sym).call
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module AdvancedRubyCommandHandler
4
- VERSION = "0.1.3"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module AdvancedRubyCommandHandler
4
+ VERSION = "2.0.0"
5
+ end
@@ -1,12 +1,16 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "advanced_ruby_command_handler/app/app"
4
- require_relative "advanced_ruby_command_handler/app/command"
5
- require_relative "advanced_ruby_command_handler/app/utils"
6
-
7
-
8
- module CommandHandler
9
- def self.new(commands_dir: "commands", events_dir: "events", config_file: "config.yml")
10
- AdvancedRubyCommandHandler::Client.new(:commands_dir => commands_dir, :events_dir => events_dir, :config_file => config_file)
11
- end
12
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "discordrb"
4
+ require File.expand_path("advanced_ruby_command_handler/database", File.dirname(__FILE__))
5
+ require File.expand_path("advanced_ruby_command_handler/errors", File.dirname(__FILE__))
6
+ require File.expand_path("advanced_ruby_command_handler/client", File.dirname(__FILE__))
7
+ require File.expand_path("advanced_ruby_command_handler/utils", File.dirname(__FILE__))
8
+ require File.expand_path("advanced_ruby_command_handler/command_handler", File.dirname(__FILE__))
9
+ require File.expand_path("advanced_ruby_command_handler/event", File.dirname(__FILE__))
10
+ require File.expand_path("advanced_ruby_command_handler/logger", File.dirname(__FILE__))
11
+
12
+ module AdvancedRubyCommandHandler
13
+ include Discordrb
14
+ include Errors
15
+ include Utils
16
+ end
@@ -0,0 +1,21 @@
1
+ class BaseDatabase
2
+ attr_reader client: Mongo::Client
3
+
4
+ def initialize: (Mongo::CLient client) -> void
5
+
6
+ def to_s: () -> String
7
+
8
+ def options: () -> Hash|String
9
+ end
10
+
11
+ class BaseClient < Discordrb::Bot
12
+ def initialize: () -> void
13
+ end
14
+
15
+ class BaseCommandHandler
16
+ attr_accessor commands_dir: String
17
+
18
+ attr_accessor events_dir: String
19
+
20
+ def initialize: (Hash options) -> void
21
+ end
@@ -0,0 +1,7 @@
1
+ class Database < BaseDatabase
2
+ attr_accessor database: Mongo::Client
3
+
4
+ attr_accessor path: String
5
+
6
+ def initialize: (String path) -> void
7
+ end
@@ -0,0 +1,19 @@
1
+ module Errors
2
+ class LoadCommandError < RuntimeError
3
+ attr_reader command: String
4
+
5
+ def initialize: (String command, ?String? message) -> void
6
+ end
7
+
8
+ class LoadEventError < RuntimeError
9
+ attr_reader event: String
10
+
11
+ def initialize: (String event, ?String? message) -> void
12
+ end
13
+
14
+ class MissingEnvironmentVariable < RuntimeError
15
+ attr_reader environment_variable: String
16
+
17
+ def initialize: (String environment_variable, ?String? message) -> void
18
+ end
19
+ end
data/tests/index.rb CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  require_relative "../lib/advanced_ruby_command_handler"
4
4
 
5
+ command_handler = CommandHandler.new(Hash[
6
+ :commands_dir => "tests/src/commands/",
7
+ :events_dir => "tests/src/events/",
8
+ :commands_module_name => "Commands",
9
+ :events_module_name => "Events"
10
+ ])
11
+ command_handler.on :commands_loaded do |commands|
12
+ Logger.check("#{commands.size} commands loaded!")
13
+ end
5
14
 
6
- CommandHandler.new(:commands_dir => "tests/commands", :events_dir => "tests/events", :config_file => "tests/config.yml")
7
- .run
15
+ command_handler.on :events_loaded do |events|
16
+ Logger.check("#{events.size} events loaded!")
17
+ end
18
+
19
+ command_handler.load_commands
20
+ .load_events
21
+ command_handler.client.run
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "././../../../src/../../lib/advanced_ruby_command_handler/command"
4
+
5
+ module Commands
6
+ def self.test
7
+ Command.new(Hash[
8
+ :name => "test"
9
+ ]) do |client, message|
10
+ message.respond "#{client.profile.username} prêt!"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Events
4
+ def self.ready(command_handler)
5
+ Event.new(:ready, command_handler.client) do
6
+ puts "Ready!"
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: advanced_ruby_command_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - senchuu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-27 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: discordrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: an advanced discord bot command handler
14
28
  email:
15
29
  - senchuuuu@gmail.com
@@ -27,26 +41,27 @@ files:
27
41
  - LICENSE.txt
28
42
  - README.md
29
43
  - Rakefile
30
- - advanced_ruby_command_handler-0.1.1.gem
31
- - advanced_ruby_command_handler-0.1.2.gem
32
44
  - advanced_ruby_command_handler.gemspec
33
45
  - bin/console
34
46
  - bin/setup
35
47
  - lib/advanced_ruby_command_handler.rb
36
- - lib/advanced_ruby_command_handler/app/app.rb
37
- - lib/advanced_ruby_command_handler/app/client.rb
38
- - lib/advanced_ruby_command_handler/app/command.rb
39
- - lib/advanced_ruby_command_handler/app/command_handler.rb
40
- - lib/advanced_ruby_command_handler/app/defaults/events/message.rb
41
- - lib/advanced_ruby_command_handler/app/defaults/events/ready.rb
42
- - lib/advanced_ruby_command_handler/app/event_handler.rb
43
- - lib/advanced_ruby_command_handler/app/logger.rb
44
- - lib/advanced_ruby_command_handler/app/utils.rb
48
+ - lib/advanced_ruby_command_handler/base.rb
49
+ - lib/advanced_ruby_command_handler/client.rb
50
+ - lib/advanced_ruby_command_handler/command.rb
51
+ - lib/advanced_ruby_command_handler/command_handler.rb
52
+ - lib/advanced_ruby_command_handler/database.rb
53
+ - lib/advanced_ruby_command_handler/defaults/events/message.rb
54
+ - lib/advanced_ruby_command_handler/errors.rb
55
+ - lib/advanced_ruby_command_handler/event.rb
56
+ - lib/advanced_ruby_command_handler/logger.rb
57
+ - lib/advanced_ruby_command_handler/utils.rb
45
58
  - lib/advanced_ruby_command_handler/version.rb
46
- - tests/commands/test/test.rb
47
- - tests/config.yml
48
- - tests/events/ready.rb
59
+ - sig/lib/advanced_ruby_command_handler/base.rbs
60
+ - sig/lib/advanced_ruby_command_handler/database.rbs
61
+ - sig/lib/advanced_ruby_command_handler/errors.rbs
49
62
  - tests/index.rb
63
+ - tests/src/commands/tests/test.rb
64
+ - tests/src/events/ready.rb
50
65
  homepage: https://github.com/Senchuu/Advanced-Bot.rb
51
66
  licenses:
52
67
  - MIT
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../version"
4
- require_relative "logger"
5
- require_relative "client"
6
-
7
- module AdvancedRubyCommandHandler; end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "discordrb"
4
- require "fileutils"
5
- require "yaml"
6
- require_relative "command_handler"
7
- require_relative "event_handler"
8
-
9
- module AdvancedRubyCommandHandler
10
- class Client < Discordrb::Bot
11
- attr_reader :commands_dir, :events_dir, :commands, :config, :console_logger, :file_logger
12
-
13
- def initialize(commands_dir: "commands", events_dir: "events", config_file: "config.yml")
14
- FileUtils.mkdir_p [commands_dir, events_dir]
15
- @commands_dir = commands_dir
16
- @events_dir = events_dir
17
- @file_logger = AdvancedRubyCommandHandler::Logger.new(:file)
18
- @console_logger = AdvancedRubyCommandHandler::Logger.new(:console)
19
-
20
- base_data = YAML.dump({
21
- :token => "",
22
- :prefix => "",
23
- :owners => []
24
- })
25
-
26
- File.open(config_file, "w+") { |file| file.write(base_data) } unless File.exist? config_file
27
-
28
- @config = YAML.load_file(config_file)
29
-
30
- %i[token prefix owners].each do |prop|
31
- next if @config[prop] && !@config[prop].empty?
32
-
33
- @console_logger.error("You have to add '#{prop.to_s}' value in your config file")
34
- raise "'#{prop}' missing or empty"
35
- end
36
-
37
- super(:token => @config[:token])
38
-
39
- @commands = AdvancedRubyCommandHandler::CommandHandler.load_commands(self)
40
- AdvancedRubyCommandHandler::EventHandler.load_events(self)
41
- .each do |event|
42
- Events.method(event).call(self)
43
- end
44
- end
45
-
46
- def run
47
- @console_logger.info("Client login!")
48
-
49
- Thread.new do
50
- @console_logger.info("Type '.exit' to turn off the bot")
51
- # @console_logger.info("Type '.reload' to reload the bot")
52
-
53
- exit if $stdin.gets.chomp == ".exit"
54
- end
55
-
56
- super.run
57
- end
58
- end
59
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CommandHandler
4
- class Command
5
- attr_reader :props, :run
6
-
7
- def initialize(props, &run)
8
- @props = props
9
- @run = run
10
- end
11
- end
12
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "app"
4
-
5
- module AdvancedRubyCommandHandler
6
- module CommandHandler
7
- def self.load_commands(client)
8
- commands = []
9
- Dir.entries(client.commands_dir).each do |dir|
10
- next if %w[. ..].include?(dir)
11
-
12
- Dir.entries("#{client.commands_dir}/#{dir}").each do |file|
13
- next if %w(. ..).include?(file)
14
-
15
- load "#{client.commands_dir}/#{dir}/#{file}"
16
-
17
- commands << File.basename(file, ".rb")
18
- end
19
- end
20
- commands
21
- end
22
- end
23
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../../utils"
4
-
5
- module Events
6
- def self.message(client)
7
- client.message(:start_with => client.config[:prefix]) do |message|
8
- args = message.content.slice(client.config[:prefix].size, message.content.size).split(" ")
9
- name = args.shift
10
- command = CommandHandler::Utils.get_command(name)
11
-
12
- next unless command
13
-
14
- command.run.call(message, client)
15
- end
16
- end
17
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Events
4
- def self.ready(client)
5
- client.ready do
6
- client.console_logger.info("Client ready!")
7
- end
8
- end
9
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "app"
4
-
5
- module AdvancedRubyCommandHandler
6
- module EventHandler
7
- def self.load_events(client)
8
- events = []
9
- Dir.entries(client.events_dir).each do |file|
10
- next if %w[. ..].include?(file)
11
-
12
- load "#{client.events_dir}/#{file}"
13
- events << File.basename(file, ".rb")
14
- end
15
-
16
- Dir["#{File.dirname(__FILE__)}/defaults/events/*.rb"].each do |file|
17
- next if events.include?(File.basename(file, ".rb"))
18
-
19
- require file
20
-
21
- events << File.basename(file, ".rb")
22
- end
23
-
24
- events
25
- end
26
- end
27
- end
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "fileutils"
4
-
5
- module AdvancedRubyCommandHandler
6
- class Logger
7
- attr_reader :mode
8
- attr_accessor :write_targets
9
-
10
- COLORS = {
11
- :default => "\e[38m",
12
- :white => "\e[39m",
13
- :black => "\e[30m",
14
- :red => "\e[31m",
15
- :green => "\e[32m",
16
- :brown => "\e[33m",
17
- :blue => "\e[34m",
18
- :magenta => "\e[35m",
19
- :cyan => "\e[36m",
20
- :gray => "\e[37m",
21
- :yellow => "\e[33m"
22
- }.freeze
23
- MODES = {
24
- :info => :cyan,
25
- :error => :red,
26
- :warn => :yellow,
27
- :check => :green
28
- }.freeze
29
- def initialize(mode = :console, write_targets = { :errors => "logs/errors.txt", :informations => "logs/infos.txt" })
30
- @mode = mode.to_sym
31
- @write_targets = write_targets if @mode == :file
32
- end
33
-
34
- def console_color(color, message)
35
- "#{COLORS[color]}#{message}\e[0m"
36
- end
37
-
38
- def write(message, write_target = :informations)
39
- return Logger.new(:console).warn("The file logs aren't available on #{@mode} mode") unless @mode == :file
40
-
41
- time = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
42
-
43
- FileUtils.mkdir_p File.dirname @write_targets[write_target]
44
-
45
- File.open(@write_targets[write_target], "w") unless File.exist? @write_targets[write_target]
46
-
47
- File.open(@write_targets[write_target], "a+") do |file|
48
- file.write("#{time} - #{write_target.to_s.upcase} : #{message}\n")
49
- end
50
- end
51
-
52
- MODES.each do |mode, color|
53
- define_method(mode) do |message|
54
- return Logger.new(:console).warn("The console logs aren't available on #{@mode} mode") unless @mode == :console
55
-
56
- time = Time.now.strftime("%Y-%m-%d-%H:%M:%S")
57
- puts "[#{console_color(:magenta, time)}] - [#{console_color(color, mode.to_s.upcase)}] : #{message.to_s}"
58
- end
59
- end
60
-
61
- private :console_color
62
- end
63
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CommandHandler
4
- module Utils
5
- def self.get_command(command_name)
6
- return Commands.method(command_name).call if defined?(Commands) && Commands.respond_to?(command_name)
7
-
8
- false
9
- end
10
- end
11
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../../../lib/advanced_ruby_command_handler/app/command"
4
-
5
- module Commands
6
- def self.test
7
- CommandHandler::Command.new({
8
- :name => "test"
9
- }) do |message, client|
10
- message.respond "Command test!"
11
- end
12
- end
13
- end
data/tests/config.yml DELETED
@@ -1,4 +0,0 @@
1
- ---
2
- :token: 'NzEwMTIxODk2OTM0NjM3NjY5.Xrv2jQ.4v9551dYI5nqRSIGfYy19APUmbA'
3
- :prefix: '!'
4
- :owners: [123456789]
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Events
4
- def self.ready(client)
5
- client.ready do
6
- client.console_logger.info("Client ready!")
7
- end
8
- end
9
- end
10
-