cogsdrb 0.1.6 → 0.2.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93ee70ecaddfe627a3559db5799bfedd5301c256231d8b48fb3e2ad93520de97
4
- data.tar.gz: 695d51fd6712fd1c65885a51ca5a5ede23baeb16c9d5fc0cab7dd6d0590d1102
3
+ metadata.gz: aa139fe677fab4e74fa37690536c56e26a6f7a5916ebac60ef6e97c10d9cedf7
4
+ data.tar.gz: d608f26282fd37d6ff977566d46b25c5937ac48fe5148ca8c8954acf3428a60f
5
5
  SHA512:
6
- metadata.gz: a6e598efcde6e809992c79a39de90d96216f69ce5f73b07934e0a0202937516731e4eddc2d1de645ad42f4454dde9fe84827df1f51a05d7baa0df9ecf1621822
7
- data.tar.gz: 55427a2092555d1499ee4a31b2c181da54ca9b6d7fe8a7f6d90fe5d60636507e884ba42daae5188dd3e71efb182dcf2040a789a4fcc09bafb422b69d5fba83fe
6
+ metadata.gz: 870020397d6104e2e23815cb09cffba6d4ee99e34c1c0b87b35475838be2924196fbb5b691dc04d4e237bfdacef09d9901ee259d889c20b00aad7aced709e7ad
7
+ data.tar.gz: a1b54950fbd4077ce0f96d59a6cbeaa55fdb8e7746e10f2abbf5c1abdf4c802117a6bd4a142ff490f919346eb59ce4bede37e8c49d659d58c12027ae832c1f42
data/README.md CHANGED
@@ -5,7 +5,7 @@ Cogs for [discordrb](https://github.com/discordrb/discordrb), in the style of [d
5
5
  Cogs allow you to load commands, events, and variables from outside of the main bot file,
6
6
  which helps improve organization and readability of code.
7
7
 
8
- #### Note: This gem is in extremely early development. As of now, it is extremely bare bones. More features will be added soon, and everyone is welcome to contribute. Additionally, the code is short but manages to be wack.
8
+ #### Note: This gem is in extremely early development. As of now, it is extremely bare bones. More features will be added soon, and everyone is welcome to contribute. Additionally, the code is kinda hacky in some parts, and that will be fixed.
9
9
 
10
10
  ## Installation
11
11
 
@@ -81,7 +81,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
81
81
 
82
82
  ## Contributing
83
83
 
84
- Bug reports and pull requests are welcome on GitHub at https://github.com/No-Jons/drb-cogs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/No-Jons/drb-cogs/blob/master/CODE_OF_CONDUCT.md).
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/No-Jons/cogsdrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/No-Jons/cogsdrb/blob/master/CODE_OF_CONDUCT.md).
85
85
 
86
86
 
87
87
  ## License
@@ -90,4 +90,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
90
90
 
91
91
  ## Code of Conduct
92
92
 
93
- Everyone interacting in the Drb::Cogs project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/drb-cogs/blob/master/CODE_OF_CONDUCT.md).
93
+ Everyone interacting in the CogsDRB project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/No-Jons/cogsdrb/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,118 @@
1
+ require 'discordrb'
2
+
3
+ module Cogs
4
+ class BaseBotClass < Discordrb::Commands::CommandBot
5
+ def initialize(token, prefix)
6
+ super token: token, prefix: prefix
7
+ @cached_cogs = {}
8
+ @loaded_cogs = {}
9
+
10
+ end
11
+
12
+ attr_reader :cached_cogs
13
+ attr_reader :loaded_cogs
14
+
15
+ def add_commands
16
+ nil
17
+ end
18
+
19
+ def run_bot
20
+ self.before
21
+ self.add_commands
22
+ begin
23
+ self.run
24
+ ensure
25
+ self.after
26
+ end
27
+ end
28
+
29
+ def before
30
+ nil
31
+ end
32
+
33
+ def after
34
+ nil
35
+ end
36
+
37
+ def command(name, attributes = {}, &block)
38
+ @commands ||= {}
39
+
40
+ # TODO: Remove in 4.0
41
+ if name.is_a?(Array)
42
+ name, *aliases = name
43
+ attributes[:aliases] = aliases if attributes[:aliases].nil?
44
+ Discordrb::LOGGER.warn("While registering command #{name.inspect}")
45
+ Discordrb::LOGGER.warn('Arrays for command aliases is removed. Please use `aliases` argument instead.')
46
+ end
47
+
48
+ new_command = Cogs::Command.new(self, name, attributes, &block)
49
+ new_command.attributes[:aliases].each do |aliased_name|
50
+ @commands[aliased_name] = CommandAlias.new(aliased_name, new_command)
51
+ end
52
+ @commands[name] = new_command
53
+ end
54
+
55
+ def _remove_all_cog_commands(cogname)
56
+ @commands ||= {}
57
+ @commands.each do |cmd|
58
+ if cmd[1]::cog == cogname
59
+ self.remove_command cmd[1]::name
60
+ p cmd[1]::name
61
+ end
62
+ end
63
+ end
64
+
65
+ def load_extension(fp)
66
+ fp = fp + ".rb" unless fp.end_with?".rb"
67
+ raise Cogs::CogError.new "Nonexistant filepath #{fp}" unless File.file?fp + ".rb" unless fp.end_with?".rb"
68
+ begin
69
+ load fp
70
+ cog = setup(self)
71
+ cog::fp = fp
72
+ @loaded_cogs[cog::name] = cog
73
+ rescue LoadError, NoMethodError => error
74
+ self.log_exception(error)
75
+ end
76
+ end
77
+
78
+ def add_cog(cog)
79
+ begin
80
+ cog.commands
81
+ @cached_cogs[cog::name] = cog.class
82
+ return cog
83
+ rescue NoMethodError => error
84
+ self.log_exception(error)
85
+ end
86
+ end
87
+
88
+ def unload_cog(cog: nil, cogname: "")
89
+ if cog.is_a?Cogs::Cog
90
+ if @cached_cogs.keys.include? cog::name
91
+ @loaded_cogs = @loaded_cogs.tap { |key| delete(key) if key == cog::name }
92
+ self._remove_all_cog_commands(cog::name)
93
+ Discordrb::LOGGER.info "Unloaded cog #{cog::name}"
94
+ return
95
+ end
96
+ end
97
+ if @cached_cogs.keys.include?cogname
98
+ @loaded_cogs = @loaded_cogs.tap { |key| delete(key) if key == cogname }
99
+ self._remove_all_cog_commands(cogname)
100
+ Discordrb::LOGGER.info "Unloaded cog #{cogname}"
101
+ return
102
+ end
103
+ raise Cogs::CogError.new "Cog already unloaded"
104
+ end
105
+
106
+ def reload_cog(cog)
107
+ raise Cogs::CogError.new "Cog not found" unless @cached_cogs.keys.include?cog or @loaded_cogs.keys.include?cog
108
+ if @loaded_cogs.include?cog
109
+ self.unload_cog(cogname: cog)
110
+ end
111
+ self.load_extension(@loaded_cogs[cog]::fp)
112
+ end
113
+
114
+ def load_cog(cog)
115
+ self.reload_cog(cog: cog)
116
+ end
117
+ end
118
+ end
@@ -1,73 +1,26 @@
1
1
  require 'discordrb'
2
2
  require "cogsdrb/version"
3
+ require "bot"
4
+ require "commands"
3
5
 
4
6
  module Cogs
5
- class BaseBotClass < Discordrb::Commands::CommandBot
6
- def initialize(token, prefix)
7
- super token: token, prefix: prefix
8
- @cogs = []
9
- end
10
-
11
- def cogs
12
- @cogs
13
- end
14
-
15
- def add_commands
16
- nil
17
- end
18
-
19
- def run_bot
20
- self.before
21
- self.add_commands
22
- begin
23
- self.run
24
- ensure
25
- self.after
26
- end
27
- end
28
-
29
- def before
30
- nil
31
- end
32
-
33
- def after
34
- nil
35
- end
36
-
37
- def load_cog(fp)
38
- begin
39
- require fp
40
- setup(self)
41
- rescue LoadError, NoMethodError => error
42
- self.log_exception(error)
43
- end
44
- end
45
-
46
- def add_cog(cog)
47
- begin
48
- cog.commands
49
- @cogs.append(cog)
50
- rescue NoMethodError => error
51
- self.log_exception(error)
52
- end
53
- end
54
- end
55
-
56
7
  class Cog
57
8
  def initialize(name: "Cog")
58
9
  @name = name
10
+ @fp = fp
59
11
  end
60
12
 
61
- def name
62
- @name
63
- end
13
+ attr_reader :name
14
+ attr_accessor :fp
64
15
  end
65
16
 
66
17
  # TODO:
67
- # - Loading and unloading of cogs
18
+ # Loading and unloading of cogs
68
19
  # - Event listeners
69
20
  # - Cog meta attributes
70
21
  # - More dpy-like implementation
71
22
  # - Clean it up and make it not hacky af
72
23
 
24
+ class CogError < StandardError; end
25
+
73
26
  end
@@ -1,3 +1,3 @@
1
1
  module Cogs
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.8"
3
3
  end
@@ -0,0 +1,57 @@
1
+ require 'discordrb'
2
+
3
+ module Cogs
4
+ class Command < Discordrb::Commands::Command
5
+ def initialize(bot, name, attributes = {}, &block)
6
+ super(name=name, attributes=attributes, &block=block)
7
+ @bot = bot
8
+ @cog = attributes[:cog] || "None"
9
+ end
10
+
11
+ attr_reader :cog
12
+
13
+ def call(event, arguments, chained = false, check_permissions = true)
14
+ return unless @bot::cached_cogs.include?(@cog) || @cog == "None"
15
+ if arguments.length < @attributes[:min_args]
16
+ response = "Too few arguments for command `#{name}`!"
17
+ response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage]
18
+ event.respond(response)
19
+ return
20
+ end
21
+ if @attributes[:max_args] >= 0 && arguments.length > @attributes[:max_args]
22
+ response = "Too many arguments for command `#{name}`!"
23
+ response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage]
24
+ event.respond(response)
25
+ return
26
+ end
27
+ unless @attributes[:chain_usable]
28
+ if chained
29
+ event.respond "Command `#{name}` cannot be used in a command chain!"
30
+ return
31
+ end
32
+ end
33
+
34
+ if check_permissions
35
+ rate_limited = event.bot.rate_limited?(@attributes[:bucket], event.author)
36
+ if @attributes[:bucket] && rate_limited
37
+ event.respond @attributes[:rate_limit_message].gsub('%time%', rate_limited.round(2).to_s) if @attributes[:rate_limit_message]
38
+ return
39
+ end
40
+ end
41
+
42
+ result = @block.call(event, *arguments)
43
+ event.drain_into(result)
44
+ rescue LocalJumpError => e # occurs when breaking
45
+ result = e.exit_value
46
+ event.drain_into(result)
47
+ rescue StandardError => e # Something went wrong inside our @block!
48
+ rescue_value = @attributes[:rescue] || event.bot.attributes[:rescue]
49
+ if rescue_value
50
+ event.respond(rescue_value.gsub('%exception%', e.message)) if rescue_value.is_a?(String)
51
+ rescue_value.call(event, e) if rescue_value.respond_to?(:call)
52
+ end
53
+
54
+ raise e
55
+ end
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cogsdrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - No-Jons
@@ -27,8 +27,10 @@ files:
27
27
  - bin/console
28
28
  - bin/setup
29
29
  - cogsdrb.gemspec
30
+ - lib/bot.rb
30
31
  - lib/cogsdrb.rb
31
32
  - lib/cogsdrb/version.rb
33
+ - lib/commands.rb
32
34
  homepage: https://github.com/No-Jons/cogsdrb
33
35
  licenses:
34
36
  - MIT