elephrame 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a102472e567d49830d7440751ed69fe4eeffc08dbb155d39b792c56660653a91
4
- data.tar.gz: 36c68839714a2edce6d370a005c6fddd33e85d3cf9ce7b736c7fbf16a142018b
3
+ metadata.gz: 041bac1a80ecb71d3a0da5c2af2f24ec9302457c8663efe0acad6cfaabbb57f4
4
+ data.tar.gz: fdf9594ab59e48986b3e8c92f22c3da7c8f696e2b014197b42ea7897bc368f7d
5
5
  SHA512:
6
- metadata.gz: 64ca939df0ef20b7727bfcbe799dee62f4614c7e955e8830035b4829640f80d6c12169e66038739deab3faa0c6c4fbaf9556790d7c5d4f6af1b3cabb66f4e732
7
- data.tar.gz: 60971b59d7df12a7318ae2c03021b5cef2f4421af0d70678cb9696ecc3fc84beba0a7879e60fbf3333f9911b748063550b58d81549ce25d46c841334fe13d83c
6
+ metadata.gz: 55ea90d7f5b18997aa31d5662b2261728a2a6a407ff2d5ebec9f78796d3df0a7f45fff54a7686adde3768db011f89425dbaae81da5e9e0e3180887732fd16611
7
+ data.tar.gz: 4ebfbcdbfafddfd91b1da93bf71f265d6b499a2f43d58abe3af4a45cad948f2e7063b463da74a47048d5fc810bb34f9f6efbc77f49c93667628c2655c5bdb988
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elephrame (0.3.7)
4
+ elephrame (0.4.0)
5
5
  moostodon (~> 0.2.0)
6
6
  rufus-scheduler
7
7
 
data/README.md CHANGED
@@ -51,6 +51,7 @@ So far the framework support 4 bot types: Periodic, Interact, PeroidInteract, Re
51
51
  - `Interact` supports callbacks for each type of interaction (favorites, boosts, replies, follows)
52
52
  - `PeriodInteract` supports both of the above (I know, this isn't a good name)
53
53
  - `Reply` only supports replying to mentions
54
+ - `Command` supports running code when mentioned with commands it recognizes
54
55
 
55
56
  The string passed to `Periodic` and `PeroidInteract` must be either a 'Duration' string or a 'Cron' string, as parsable by [fugit](https://github.com/floraison/fugit)
56
57
 
@@ -74,6 +75,13 @@ Exposed methods from bot object:
74
75
 
75
76
  (See RubyDocs for source code documentation)
76
77
 
78
+ ## In the Wild!
79
+
80
+ Here's a list of bots that are currently using this framework. If you are using it, add yourself to this list and submit a pull request!
81
+
82
+ - [GameBot](https://github.com/theZacAttacks/GameBot)
83
+ - [EnhanceBot](https://github.com/theZacAttacks/EnhanceBot)
84
+
77
85
  ## Development
78
86
 
79
87
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,28 @@
1
+ require 'elephrame'
2
+
3
+ Candy = [ 'M&Ms', 'Skittles', 'Twix', 'Candycorn' ]
4
+ Genders = [ 'sweet', 'sour', 'bitter', 'creamy', 'umami' ]
5
+
6
+ # set the prefix and usage string for our bot
7
+ cmd_bot = Elephrame::Bots::Command.new '!', 'mention me with !candy to get candy, or !gender to get a gender'
8
+
9
+ # add in the candy command
10
+ cmd_bot.add_command 'candy' do |bot, data, status|
11
+ bot.reply("@#{status.account.acct} here's some candy!
12
+ *gives you a #{Candy.sample}*",
13
+ spoiler: 'candy')
14
+ end
15
+
16
+ # add in the gender command
17
+ cmd_bot.add_command 'gender' do |bot, data, status|
18
+ bot.reply("@#{status.account.acct} here's a spare gender!
19
+ *gives you a #{Genders.sample} gender*",
20
+ spoiler: 'gender shitpost')
21
+ end
22
+
23
+ # if the command is not found
24
+ cmd_bot.if_not_found do |bot, status|
25
+ bot.reply("@#{status.account.acct} I didn't recognize that! Respond with !help to get usage info")
26
+ end
27
+
28
+ cmd_bot.run
@@ -39,5 +39,37 @@ module Elephrame
39
39
  end
40
40
  end
41
41
 
42
+
43
+ ##
44
+ # A bot that responds to commands,
45
+ #
46
+ # after creation make sure to call add_command or else your bot won't
47
+ # know what to respond to! (See [Elephrame::Command] for more details
48
+
49
+ class Command < BaseBot
50
+ include Elephrame::Streaming
51
+ include Elephrame::Command
52
+
53
+ ##
54
+ # Create a new Command bot, sets +commands+ and +cmd_hash+ to empty
55
+ # defaults
56
+ #
57
+ # @param prefix [String] sets the command prefix, defaults to '!'
58
+ # @param usage [String] the response to the help command
59
+ #
60
+ # @return [Elephrame::Bots::Command]
61
+
62
+ def initialize prefix = '!', usage = nil
63
+ super()
64
+
65
+ @commands = []
66
+ @cmd_hash = {}
67
+
68
+ setup_streaming
69
+ set_prefix prefix
70
+ set_help usage unless usage.nil?
71
+ end
72
+ end
73
+
42
74
  end
43
75
  end
@@ -141,4 +141,87 @@ module Elephrame
141
141
 
142
142
  alias_method :run, :run_interact
143
143
  end
144
+
145
+
146
+ module Command
147
+ include Elephrame::Reply
148
+
149
+ attr_reader :commands, :prefix
150
+ attr :cmd_hash, :cmd_regex, :not_found
151
+
152
+ ##
153
+ # sets the prefix for commands
154
+ #
155
+ # @param pf [String] the prefix
156
+
157
+ def set_prefix pf
158
+ @prefix = pf
159
+ end
160
+
161
+ ##
162
+ # Shortcut method to provide usage docs in response to help command
163
+ #
164
+ # @param usage [String]
165
+
166
+ def set_help usage
167
+ add_command 'help' do |bot, content, status|
168
+ bot.reply("@#{status.account.acct} #{usage}")
169
+ end
170
+ end
171
+
172
+ ##
173
+ # Adds the command and block into the bot to process later
174
+ # also sets up the command regex
175
+ #
176
+ # @param cmd [String] a command to add
177
+ # @param block [Proc] the code to execute when +cmd+ is recieved
178
+
179
+ def add_command cmd, &block
180
+ @commands.append cmd unless @commands.include? cmd
181
+ @cmd_hash[cmd.to_sym] = block
182
+
183
+ # build up our regex (this regex should be fine, i guess :shrug:)
184
+ @cmd_regex = /\A#{@prefix}(?<cmd>#{@commands.join('|')})\b(?<data>.*)/m
185
+ end
186
+
187
+ ##
188
+ # What to do if we don't match anything
189
+ #
190
+ # @param block [Proc] a block to run when we don't match a command
191
+
192
+ def if_not_found &block
193
+ @not_found = block
194
+ end
195
+
196
+ ##
197
+ # Starts loop to process any mentions, running command procs set up earlier
198
+
199
+ def run_commands
200
+ @streamer.user do |update|
201
+ next unless update.kind_of? Mastodon::Notification and update.type == 'mention'
202
+
203
+ # set up the status to strip html, if needed
204
+ update.status.class
205
+ .module_eval { alias_method :content, :strip } if @strip_html
206
+ store_mention_data update.status
207
+
208
+ # strip our username out of the status
209
+ post = update.status.content.gsub(/@#{@username} /, '')
210
+
211
+ # see if the post matches our regex, running the stored proc if it does
212
+ matches = @cmd_regex.match(post)
213
+
214
+ unless matches.nil?
215
+ @cmd_hash[matches[:cmd].to_sym]
216
+ .call(self,
217
+ matches[:data].strip,
218
+ update.status)
219
+ else
220
+ @not_found.call(self, update.status)
221
+ end
222
+ end
223
+ end
224
+
225
+ alias_method :run, :run_commands
226
+ end
144
227
  end
@@ -1,3 +1,3 @@
1
1
  module Elephrame
2
- VERSION = "0.3.7"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elephrame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-06 00:00:00.000000000 Z
11
+ date: 2018-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,6 +100,7 @@ files:
100
100
  - elephrame.gemspec
101
101
  - examples/README.md
102
102
  - examples/combined.rb
103
+ - examples/command.rb
103
104
  - examples/interact.rb
104
105
  - examples/periodic.rb
105
106
  - examples/reply.rb