Class: Cosgrove::Bot

Inherits:
Discordrb::Commands::CommandBot
  • Object
show all
Includes:
SnarkCommands, Support
Defined in:
lib/cosgrove/bot.rb

Constant Summary

Constants included from SnarkCommands

SnarkCommands::WITTY

Instance Method Summary collapse

Methods included from SnarkCommands

add_all_snark_commands

Methods included from Support

#append_link_details, #cannot_find_input, #find_account, #last_irreversible_block, #muted, #page_views, #send_url, #skip_channel, #skipped_channel?, #skipped_channels, #start_typing, #suggest_account_name, #unknown_account

Methods included from Utils

#api, #chain_options, #core_asset, #cycle_api_at, #cycle_stream_at, #debt_asset, #find_author, #find_author_name_permlink, #find_comment, #find_comment_by_slug, #find_transfer, #follow_api, #head_block_number, #hive_engine, #hive_engine_blockchain, #hive_engine_contracts, #hive_engine_shutdown, #last_irreversible_block_num, #new_tx, #parse_slug, #ping_api, #properties, #reset_api, #reset_stream, #steem_engine, #steem_engine_blockchain, #steem_engine_contracts, #steem_engine_shutdown, #stream, #to_rep

Methods included from Config

#channel_disable_comment_voting, #channel_upvote_weight, #cosgrove_allow_pm_commands, #cosgrove_client_id, #cosgrove_disable_comment_voting, #cosgrove_operators, #cosgrove_secure, #cosgrove_token, #cosgrove_upvote_weight, #discord_channels, #discord_fancy_log, #discord_log_mode, #hive_account, #hive_api_failover_urls, #hive_api_url, #hive_engine_api_url, #hive_posting_wif, #meeseeker_url, #steem_account, #steem_api_failover_urls, #steem_api_url, #steem_engine_api_url, #steem_posting_wif, #test_api_failover_urls, #test_api_url, #test_posting_wif

Constructor Details

#initialize(options = {}) ⇒ Bot

Returns a new instance of Bot.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cosgrove/bot.rb', line 14

def initialize(options = {})
  options[:token] ||= cosgrove_token
  options[:client_id] ||= cosgrove_client_id
  super(options)
  
  @on_success_upvote_job = options[:on_success_upvote_job]
  @on_success_register_job = options[:on_success_register_job]
  
  self.bucket :voting, limit: 4, time_span: 8640, delay: 10

  add_all_commands
  add_all_messages
  SnarkCommands::add_all_snark_commands(self)
end

Instance Method Details

#add_all_commandsObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cosgrove/bot.rb', line 38

def add_all_commands
  self.command :help do |_event, *args|
    help = []
    help << "`$slap [target]` - does a slap on the `target`"
    help << "`$verify <account> [chain]` - check `account` association with Discord users (`chain` default `steem`)"
    help << "`$register <account> [chain]` - associate `account` with your Discord user (`chain` default `steem`)"
    help << "`$upvote [url]` - upvote from #{}; empty or `^` to upvote last steemit link"
    help.join("\n")
  end
  
  self.command :version do |_|
    "cosgrove: #{Cosgrove::VERSION} :: https://github.com/steem-third-party/cosgrove"
  end
  
  self.command :verify do |event, key, chain = :hive|
    return if event.channel.pm? && !cosgrove_allow_pm_commands
     = nil
    
    if key.nil?
      event.respond "To create an account: https://hive.blog/enter_email?r=#{}"
      return
    end
    
     = (key, event)
    
    if !! && .respond_to?(:name)
       = Cosgrove::Account.new(.name, chain)
    else
      discord_id = key.split('@').last.split('>').first.to_i
       = Cosgrove::Account.find_by_discord_id(discord_id, chain)
    end
    
     = . if .nil? && !!
    
    if !! && !! && .discord_ids.any?
      if .hidden?
        "#{chain.to_s.upcase} account `#{.name}` has been registered."
      else
        discord_ids = .discord_ids.map { |id| "<@#{id}>" }
        
        "#{chain.to_s.upcase} account `#{.name}` has been registered with #{discord_ids.to_sentence}."
      end
    elsif !!
      "#{chain.to_s.upcase} account `#{.name}` has not been registered with any Discord account.  To register:\n`$register #{.name}`"
    elsif discord_id.to_i > 0
      "<@#{discord_id}> has not been associated with a #{chain.to_s.upcase} account.  To register:\n`$register <account>`"
    else
      "No association found.  To register:\n`$register <account>`"
    end
  end
  
  self.command :register do |event, , chain = :hive|
    return if event.channel.pm? && !cosgrove_allow_pm_commands
    
    discord_id = event.author.id
    
    if discord_id.to_i == 0
      event.respond 'Problem with discord id.'
      return
    end
    
     = (, event, chain)
    
    if .nil?
      event.respond 'Try again later.'
      return
    end
    
     = Cosgrove::Account.new(.name, chain)
    
    if .discord_ids.include? discord_id
      event.respond "Already registered `#{.name}` on `#{chain.upcase}` with <@#{discord_id}>"
      return
    end
    
    memo_key = .memo_key(discord_id)
    op = find_transfer(chain: chain, account: , from: .name, to: , memo_key: memo_key)
      
    if !!op
      .add_discord_id(discord_id)
      
      if !!@on_success_register_job
        begin
          @on_success_register_job.call(event, )
        rescue => e
          ap e
          ap e.backtrace
        end
      end
      
      "Ok.  #{chain.to_s.upcase} account #{.name} has been registered with <@#{discord_id}>."
    else
      "To register `#{.name}` with <@#{discord_id}>, send `0.001 #{core_asset}` or `0.001 #{debt_asset}` to `#{}` with memo: `#{memo_key}`\n\nThen type `$register #{.name}` again."
    end
  end
  
  self.command(:upvote, bucket: :voting, rate_limit_message: 'Sorry, you are in cool-down.  Please wait %time% more seconds.') do |event, slug = nil|
    return if event.channel.pm? && !cosgrove_allow_pm_commands
    
    slug = Cosgrove::latest_steemit_link[event.channel.name] if slug.nil? || slug.empty? || slug == '^'
    options = {
      on_success: @on_success_upvote_job
    }

    Cosgrove::UpvoteJob.new(options).perform(event, slug)
  end
end

#add_all_messagesObject


29
30
31
32
33
34
35
36
# File 'lib/cosgrove/bot.rb', line 29

def add_all_messages
  # A user typed a link to steemit.com (or some condenser site)
  self.message(content: /http[s]*:\/\/.*\/(.*\/)[email protected]+\/.+/, ignore_bots: false) do |event|
    link = event.content.split(' ').first
    Cosgrove::latest_steemit_link[event.channel.name] = link
    append_link_details(event, link)
  end
end