cogsdrb 0.2.8 → 0.3.9

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: aa139fe677fab4e74fa37690536c56e26a6f7a5916ebac60ef6e97c10d9cedf7
4
- data.tar.gz: d608f26282fd37d6ff977566d46b25c5937ac48fe5148ca8c8954acf3428a60f
3
+ metadata.gz: ab7973e0ed90dd965f13cb40b77729eabf0171464f5eaffacbb342b482cdb949
4
+ data.tar.gz: 5cf09cd4e2d94320e39a9b214f66eb1e5caf1046a83366ed9d4d227a3446acac
5
5
  SHA512:
6
- metadata.gz: 870020397d6104e2e23815cb09cffba6d4ee99e34c1c0b87b35475838be2924196fbb5b691dc04d4e237bfdacef09d9901ee259d889c20b00aad7aced709e7ad
7
- data.tar.gz: a1b54950fbd4077ce0f96d59a6cbeaa55fdb8e7746e10f2abbf5c1abdf4c802117a6bd4a142ff490f919346eb59ce4bede37e8c49d659d58c12027ae832c1f42
6
+ metadata.gz: 7d99022760e60aa8858e80aa48ff140654607e6393e2b5ce177a00ec9b7074dee3a3869adc4c1dde75d4e25403de52b109a40d759bc391741cbb923418480a6d
7
+ data.tar.gz: '0234997375f4ef43041164f7a5346fbc5db672373ee779106aaf47c6e6654b68c89febc169db52c323a886e8c6eaca0dce19e2d0fcb167bce2427f8fbee2ee36'
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CogsDRB
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cogsdrb.svg)](https://badge.fury.io/rb/cogsdrb)
4
+ [![Gem Downloads](https://badgen.net/rubygems/dt/cogsdrb)](https://badgen.net/rubygems/dt/cogsdrb)
5
+
3
6
  Cogs for [discordrb](https://github.com/discordrb/discordrb), in the style of [discord.py](https://github.com/Rapptz/discord.py)'s cog implementation
4
7
 
5
8
  Cogs allow you to load commands, events, and variables from outside of the main bot file,
@@ -39,10 +42,13 @@ require 'discordrb'
39
42
  require 'cogsdrb'
40
43
 
41
44
  bot = Cogs::BaseBotClass.new(token="<token>", prefix="r!")
42
- path_to_cogs = "C:/FULL/PATH/TO/COGS/" # As of now, cogs can only be loaded using the full path to the file
45
+ path_to_cogs = "Path/to/cogs/dir" # Not necessary, but cuts down on repeated values.
46
+ # Cogs are loaded by using ruby's `load` keyword to allow updating while the bot runs.
47
+ # as such they must be loaded by using a filepath to the ruby file. It's like requiring,
48
+ # but you load it with the function `bot.load_extension`.
43
49
 
44
50
  %w(mod fun).each do |i| # in this example, the path leads to the folder containing `mod.rb` and `fun.rb`
45
- bot.load_cog(fp="#{path_to_cogs}#{i}")
51
+ bot.load_extension(fp="#{path_to_cogs}#{i}")
46
52
  end
47
53
 
48
54
  bot.run
@@ -56,22 +62,47 @@ require 'cogsdrb'
56
62
 
57
63
  class Fun < Cogs::Cog # Class *must* inherit from `Cogs::Cog`
58
64
  def initialize(bot: Discordrb::Commands::CommandBot)
59
- @bot = bot
65
+ super(bot) # Required for command registration.
60
66
  end
61
67
 
62
- def commands # all commands for a cog *must* be contained in a function named `commands`
63
- @bot.command :test do |event| # As many commands as you want can be added in this function
68
+ def commands # commands can either be declared in the `initialize` function or a function named `commands`
69
+ self.command :test do |event| # As many commands as you want can be added in either function
64
70
  event.respond "Works"
65
71
  end
66
72
  end
67
73
  end
68
74
 
69
75
  def setup(bot)
70
- bot.add_cog(Fun.new bot: bot) # Additionally, all cog files *must* contain this function, or else an error will be thrown and the cog not loaded
76
+ bot.add_cog(Fun) # Additionally, all cog files *must* contain this function, or else an error will be thrown and the cog not loaded
77
+ end
78
+ ```
79
+
80
+ Additionally, Cogs will allow you to manage what cogs are currently "loaded" on your bot. Here are come example commands for managing cogs.
81
+ ```ruby
82
+ self.command :unload do |event, cogname|
83
+ begin
84
+ @bot.unload_cog(cogname: cogname)
85
+ event.respond "Unloaded cog #{cogname}!"
86
+ rescue Cogs::CogError
87
+ event.respond "Whoops! Cog #{cogname} either doesn't exist or is already unloaded! Did you spell it right?"
88
+ end
89
+ end
90
+
91
+ self.command :load do |event, cogname|
92
+ begin
93
+ @bot.reload_cog(cogname)
94
+ event.respond "Loaded cog #{cogname}!"
95
+ rescue Cogs::CogError
96
+ event.respond "Whoops! Cog #{cogname} doesn't exist! Did you spell it correctly?"
97
+ end
71
98
  end
72
99
  ```
73
100
 
74
- Support for events and unloading/loading cogs will come in the near future.
101
+ "Unloading" a cog removes all commands categorized in the cog from the bot. "Reloading" allows you to apply updates to
102
+ the bot without terminating the process, as the cog file can be edited and reloaded without terminating
103
+ the bot process.
104
+
105
+ Support for events will come in the near future.
75
106
 
76
107
  ## Development
77
108
 
data/lib/bot.rb CHANGED
@@ -9,13 +9,10 @@ module Cogs
9
9
 
10
10
  end
11
11
 
12
+ attr_accessor :cogs
12
13
  attr_reader :cached_cogs
13
14
  attr_reader :loaded_cogs
14
15
 
15
- def add_commands
16
- nil
17
- end
18
-
19
16
  def run_bot
20
17
  self.before
21
18
  self.add_commands
@@ -26,18 +23,13 @@ module Cogs
26
23
  end
27
24
  end
28
25
 
29
- def before
30
- nil
31
- end
32
-
33
- def after
34
- nil
35
- end
26
+ def add_commands; end
27
+ def before; end
28
+ def after; end
36
29
 
37
30
  def command(name, attributes = {}, &block)
38
31
  @commands ||= {}
39
32
 
40
- # TODO: Remove in 4.0
41
33
  if name.is_a?(Array)
42
34
  name, *aliases = name
43
35
  attributes[:aliases] = aliases if attributes[:aliases].nil?
@@ -69,7 +61,9 @@ module Cogs
69
61
  load fp
70
62
  cog = setup(self)
71
63
  cog::fp = fp
64
+ cog::name = cog.to_s[2..].split(':')[0]
72
65
  @loaded_cogs[cog::name] = cog
66
+ @cached_cogs[cog::name] = cog.class
73
67
  rescue LoadError, NoMethodError => error
74
68
  self.log_exception(error)
75
69
  end
@@ -77,8 +71,8 @@ module Cogs
77
71
 
78
72
  def add_cog(cog)
79
73
  begin
74
+ cog = cog.new bot: self
80
75
  cog.commands
81
- @cached_cogs[cog::name] = cog.class
82
76
  return cog
83
77
  rescue NoMethodError => error
84
78
  self.log_exception(error)
@@ -94,7 +88,7 @@ module Cogs
94
88
  return
95
89
  end
96
90
  end
97
- if @cached_cogs.keys.include?cogname
91
+ if @loaded_cogs.keys.include?cogname
98
92
  @loaded_cogs = @loaded_cogs.tap { |key| delete(key) if key == cogname }
99
93
  self._remove_all_cog_commands(cogname)
100
94
  Discordrb::LOGGER.info "Unloaded cog #{cogname}"
@@ -104,15 +98,16 @@ module Cogs
104
98
  end
105
99
 
106
100
  def reload_cog(cog)
107
- raise Cogs::CogError.new "Cog not found" unless @cached_cogs.keys.include?cog or @loaded_cogs.keys.include?cog
101
+ raise Cogs::CogError.new "Cog not found" unless @cached_cogs.include?cog or @loaded_cogs.include?cog
108
102
  if @loaded_cogs.include?cog
109
103
  self.unload_cog(cogname: cog)
110
104
  end
111
105
  self.load_extension(@loaded_cogs[cog]::fp)
106
+ Discordrb::LOGGER.info "Loaded cog #{cog}"
112
107
  end
113
108
 
114
109
  def load_cog(cog)
115
- self.reload_cog(cog: cog)
110
+ self.reload_cog(cog)
116
111
  end
117
112
  end
118
113
  end
@@ -5,13 +5,19 @@ require "commands"
5
5
 
6
6
  module Cogs
7
7
  class Cog
8
- def initialize(name: "Cog")
9
- @name = name
10
- @fp = fp
8
+ def initialize(bot)
9
+ @bot = bot
11
10
  end
12
11
 
13
- attr_reader :name
12
+ attr_accessor :name
14
13
  attr_accessor :fp
14
+
15
+ def command(name, attributes = {}, &block)
16
+ attributes[:cog] = @name unless attributes[:cog]
17
+ @bot.command(name, attributes, &block)
18
+ end
19
+
20
+ def commands; end
15
21
  end
16
22
 
17
23
  # TODO:
@@ -1,3 +1,3 @@
1
1
  module Cogs
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cogsdrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - No-Jons
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-30 00:00:00.000000000 Z
11
+ date: 2020-10-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: