Class: Elephrame::Bots::BaseBot
- Inherits:
-
Object
- Object
- Elephrame::Bots::BaseBot
- Defined in:
- lib/elephrame/bot.rb
Overview
a superclass for other bots holds common methods and variables
Direct Known Subclasses
Command, GenerativeBot, Interact, PeriodInteract, Periodic, Reply, TraceryBot, Watcher
Constant Summary collapse
- NoBotRegex =
/#?NoBot/i
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#failed ⇒ Object
readonly
Returns the value of attribute failed.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#strip_html ⇒ Object
Returns the value of attribute strip_html.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.backup_method(method, new_name) ⇒ Object
A helper method that is a wrapper around alias_method (just to make some code easier to read).
Instance Method Summary collapse
-
#fetch_account_id(account_name) ⇒ String
Gets the ID of an account given the user’s handle or username.
-
#fetch_list_id(name) ⇒ Integer
Gets the ID of a list given the name.
-
#find_ancestor(id, depth = 10, stop_at = 1) ⇒ Mastodon::Status
Finds most recent post by bot in the ancestors of a provided post.
-
#initialize ⇒ Elephrame::Bots::BaseBot
constructor
Sets up our REST
client
, gets and saves ourusername
, sets default value forstrip_html
(true), andmax_retries
(5),failed
. -
#no_bot?(account_id) ⇒ Bool
Checks to see if a user has some form of “#NoBot” in their bio or in their profile fields (so we can make making friendly bots easier!).
-
#post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) ⇒ Object
Creates a post, uploading media if need be.
Constructor Details
#initialize ⇒ Elephrame::Bots::BaseBot
Sets up our REST client
, gets and saves our username
, sets default value for strip_html
(true), and max_retries
(5), failed
22 23 24 25 26 27 28 29 |
# File 'lib/elephrame/bot.rb', line 22 def initialize @client = Mastodon::REST::Client.new(base_url: ENV['INSTANCE'], bearer_token: ENV['TOKEN']) @username = @client.verify_credentials().acct @strip_html = true @max_retries = 5 @failed = { media: false, post: false } end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/elephrame/bot.rb', line 11 def client @client end |
#failed ⇒ Object (readonly)
Returns the value of attribute failed.
11 12 13 |
# File 'lib/elephrame/bot.rb', line 11 def failed @failed end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
12 13 14 |
# File 'lib/elephrame/bot.rb', line 12 def max_retries @max_retries end |
#strip_html ⇒ Object
Returns the value of attribute strip_html.
12 13 14 |
# File 'lib/elephrame/bot.rb', line 12 def strip_html @strip_html end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
11 12 13 |
# File 'lib/elephrame/bot.rb', line 11 def username @username end |
Class Method Details
.backup_method(method, new_name) ⇒ Object
A helper method that is a wrapper around alias_method (just to make some code easier to read)
145 146 147 |
# File 'lib/elephrame/bot.rb', line 145 def self.backup_method(method, new_name) alias_method new_name, method end |
Instance Method Details
#fetch_account_id(account_name) ⇒ String
Gets the ID of an account given the user’s handle or username
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/elephrame/bot.rb', line 125 def fetch_account_id(account_name) name = account_name.reverse.chomp('@').reverse search = @client.search("@#{name}") accounts = {} search.accounts.each do |acct| accounts[acct.acct] = acct.id accounts[acct.username] = acct.id end accounts[name] end |
#fetch_list_id(name) ⇒ Integer
Gets the ID of a list given the name
110 111 112 113 114 115 116 |
# File 'lib/elephrame/bot.rb', line 110 def fetch_list_id(name) lists = {} @client.lists.collect do |l| lists[l.title] = l.id end lists[name] end |
#find_ancestor(id, depth = 10, stop_at = 1) ⇒ Mastodon::Status
Finds most recent post by bot in the ancestors of a provided post
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/elephrame/bot.rb', line 76 def find_ancestor(id, depth = 10, stop_at = 1) depth.times { post = @client.status(id) unless id.nil? id = post.in_reply_to_id stop_at -= 1 if post.account.acct == @username return post if stop_at.zero? } return nil end |
#no_bot?(account_id) ⇒ Bool
Checks to see if a user has some form of “#NoBot” in their bio or in their profile fields (so we can make making friendly bots easier!)
97 98 99 100 101 |
# File 'lib/elephrame/bot.rb', line 97 def no_bot? account_id acct = @client.account(account_id) acct.note =~ NoBotRegex || acct.fields.any? {|f| f.name =~ NoBotRegex || f.value =~ NoBotRegex} end |
#post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) ⇒ Object
Creates a post, uploading media if need be
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/elephrame/bot.rb', line 41 def post(text, visibility: 'unlisted', spoiler: '', reply_id: '', hide_media: false, media: []) uploaded_ids = [] unless media.size.zero? @failed[:media] = retry_if_needed { uploaded_ids = media.collect {|m| @client.upload_media(m).id } } end = { visibility: visibility, spoiler_text: spoiler, in_reply_to_id: reply_id, media_ids: @failed[:media] ? [] : uploaded_ids, sensitive: hide_media, } @failed[:post] = retry_if_needed { @client.create_status text, } end |