You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

77 lines
2.0 KiB

  1. require "dotenv"
  2. require "discordcr"
  3. require "./issue"
  4. require "./mr"
  5. DOTENV = Dotenv.load!
  6. client = Discord::Client.new(token: "Bot #{DOTENV["discord_token"]}")
  7. bot_id = client.get_current_user.id
  8. bot_name = client.get_current_user.username
  9. issue_regex = /([\w\/]*)#([0-9]+)/
  10. mr_regex = /([\w\/]*)!([0-9]+)/
  11. help_regex = Regex.new("<@!?#{bot_id}> help")
  12. help_string = <<-STR
  13. Accepted formats:
  14. `#1234` Link Tildes Issue.
  15. `!1234` Link Tildes Merge Request.
  16. Prefix with a repo name to retrieve items from it instead of the default one.
  17. Enabled Repositories: `#{DOTENV.keys.reject("discord_token").reject("gl_token")}`
  18. Hacked together by <@344166495317655562> — [Source](https://git.dingenskirchen.systems/deing/TildesAutolinkerBot) — `@#{bot_name} help` to see this message
  19. STR
  20. client.on_message_create do |message|
  21. next if message.author.bot
  22. next unless message.mentions.any? { |user| user.id == bot_id }
  23. if message.content.downcase =~ help_regex
  24. client.create_message(message.channel_id, "", Discord::Embed.new(description: help_string, title: "TildesAutolinker"))
  25. next
  26. end
  27. output = ""
  28. issues = message.content.scan issue_regex
  29. issues.each do |issue|
  30. begin
  31. repo = if issue[1].size == 0
  32. DOTENV["defaultrepo"]
  33. else
  34. DOTENV[issue[1]]
  35. end
  36. output += get_issue(repo, issue[2]).formatify + "\n"
  37. rescue e
  38. pp e
  39. output += "##{issue[2]} `#{e}`\n"
  40. end
  41. end
  42. merges = message.content.scan mr_regex
  43. merges.each do |mr|
  44. # Avoid reacting to pings with ! in them
  45. next if mr[2].size > 5
  46. pp mr[0]
  47. begin
  48. repo = if mr[1].size == 0
  49. DOTENV["defaultrepo"]
  50. else
  51. DOTENV[mr[1]]
  52. end
  53. output += get_mr(repo, mr[2]).formatify + "\n"
  54. rescue e
  55. pp e
  56. output += "!#{mr[2]} `#{e}`\n"
  57. end
  58. end
  59. output = ":exclamation: No matches found." if output.size == 0
  60. client.create_message(message.channel_id, "", Discord::Embed.new(description: output))
  61. end
  62. client.run