Class: Lolcommits::Plugin::Discord

Inherits:
Base
  • Object
show all
Defined in:
lib/lolcommits/plugin/discord.rb

Constant Summary collapse

RETRY_COUNT =

Number of times to retry if RestClient.post fails

1

Instance Method Summary collapse

Instance Method Details

#configure_options!Hash

Prompts the user to configure integration with Discord

Prompts user for a Discord webhook URL

Returns:

  • (Hash)

    a hash of configured plugin options



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lolcommits/plugin/discord.rb', line 50

def configure_options!
  options = super

  if options[:enabled]
    print "enter your Discord webhook URL below, then press enter: (e.g. https://discord.com/api/webhooks/1234/xxxx-xx-xxxxx) \n"
    webhook_url = parse_user_input(gets.strip)

    options.merge!(
      webhook: webhook_url,
    )
  end

  options
end

#run_capture_readyObject

Capture ready hook, runs after lolcommits captures a snapshot.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lolcommits/plugin/discord.rb', line 16

def run_capture_ready
  retries = RETRY_COUNT
  begin
    print "Posting to Discord ... "

    client = Discordrb::Webhooks::Client.new(url: configuration[:webhook])
    response = client.execute do |builder|
      builder.content = "[#{runner.vcs_info.repo}]\n> ```#{runner.message}```"
      builder.file = File.new(runner.lolcommit_path)
    end

    debug response
    print "done!\n"
  rescue => e
    print "failed! #{e.message}"
    if retries > 0
      retries -= 1
      print " - retrying ...\n"
      retry
    else
      print " - giving up ...\n"
      puts 'Try running config again:'
      puts "\tlolcommits --config -p discord"
    end
  end
end