Browse Source

Make it a little less ugly

master
parent
commit
d03aa5109f
No known key found for this signature in database GPG Key ID: DA34C790D267C164
5 changed files with 90 additions and 25 deletions
  1. +3
    -1
      README.md
  2. +3
    -3
      shard.yml
  3. +30
    -21
      src/TildesAutolinkerBot.cr
  4. +27
    -0
      src/issue.cr
  5. +27
    -0
      src/mr.cr

+ 3
- 1
README.md View File

@@ -6,7 +6,9 @@ In Discord, replies to certain formats with links to the Tildes Gitlab repositor

Add a .env file containing
```
token = your-bot-token
discord_token = your-bot-token
gl_token = your-personal-access-token
gl_project = your-project-id
```
After that, plain old `shards build` and run.



+ 3
- 3
shard.yml View File

@@ -1,5 +1,5 @@
name: TildesAutolinkerBot
version: 0.1.0
version: 0.2.0

authors:
- deing <admin@15318.de>
@@ -10,11 +10,11 @@ targets:

dependencies:
discordcr:
github: meew0/discordcr
github: z64/discordcr
branch: master
dotenv:
github: gdotdesign/cr-dotenv

crystal: 0.27.2
crystal: 0.29.0

license: MIT

+ 30
- 21
src/TildesAutolinkerBot.cr View File

@@ -1,8 +1,11 @@
require "dotenv"
require "discordcr"

require "./issue"
require "./mr"

Dotenv.load!
client = Discord::Client.new(token: "Bot #{ENV["token"]}")
client = Discord::Client.new(token: "Bot #{ENV["discord_token"]}")

bot_id = client.get_current_user.id
bot_name = client.get_current_user.username
@@ -10,41 +13,47 @@ bot_name = client.get_current_user.username
issue_regex = /#([0-9]+)/
mr_regex = /!([0-9]+)/
help_regex = Regex.new("<@!?#{bot_id}> help")
utw_regex = Regex.new("<@!?#{bot_id}> utw")
help_string = <<-STR
**__TildesAutolinker__**
Use `@#{bot_name} help` to see this message.
`@#{bot_name} UTW` links to the Unofficial Tildes Wiki.
To get other links (see below), mention me anywhere in your message.
`#123` is turned into <https://gitlab.com/tildes/tildes/issues/123>.
Hacked together by `deing#7141`. Code here: https://git.dingenskirchen.systems/deing/TildesAutolinkerBot.
Accepted formats:
`#1234` Link Tildes Issue.
`!1234` Link Tildes Merge Request.

Hacked together by <@344166495317655562> — [Source](https://git.dingenskirchen.systems/deing/TildesAutolinkerBot) — `@#{bot_name} help` to see this message
STR

client.on_message_create do |message|
next if message.author.bot
next unless message.mentions.any? { |user| user.id == bot_id }

if message.content.downcase =~ help_regex
client.create_message(message.channel_id, help_string)
client.create_message(message.channel_id, "", Discord::Embed.new(description: help_string, title: "TildesAutolinker"))
next
end
if message.content.downcase =~ utw_regex
client.create_message(message.channel_id, "<https://unofficial-tildes-wiki.gitlab.io>")
next
end
issues = message.content.scan issue_regex

output = ""

issues = message.content.scan issue_regex
issues.each do |issue|
output += "**Issue ##{issue[1]}:** <https://gitlab.com/tildes/tildes/issues/#{issue[1]}>\n"
begin
output += get_issue(issue[1]).formatify + "\n"
rescue e
output += "##{issue[1]} `#{e}`\n"
end
end

merges = message.content.scan mr_regex
merges.each do |mr|
output += "**Merge Request !#{mr[1]}:** <https://gitlab.com/tildes/tildes/merge_requests/#{mr[1]}>\n"
end
if output.size == 0
client.create_message(message.channel_id, ":exclamation: No matches found.")
else
client.create_message(message.channel_id, output)
begin
output += get_mr(mr[1]).formatify + "\n"
rescue e
output += "!#{mr[1]} `#{e}`\n"
end
end

output = ":exclamation: No matches found." if output.size == 0

client.create_message(message.channel_id, "", Discord::Embed.new(description: output))
end

client.run

+ 27
- 0
src/issue.cr View File

@@ -0,0 +1,27 @@
require "http/client"
require "json"
require "discordcr"

struct Issue
JSON.mapping(
title: String,
web_url: String,
iid: Int32,
state: String,
author: NamedTuple(username: String, web_url: String)
)

def formatify
output = "[##{self.iid}](#{self.web_url}) "
output += "[@#{self.author[:username]}](#{self.author[:web_url]}): "
output += self.title
output += " **[\\✔]**" if self.state == "closed"
output
end
end

def get_issue(issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{ENV["gl_project"]}/issues/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
raise "Negative API Response: #{resp.body}" unless resp.success?
Issue.from_json(resp.body)
end

+ 27
- 0
src/mr.cr View File

@@ -0,0 +1,27 @@
require "http/client"
require "json"
require "discordcr"

struct MergeRequest
JSON.mapping(
title: String,
web_url: String,
iid: Int32,
state: String,
author: NamedTuple(username: String, web_url: String)
)

def formatify
output = "[!#{self.iid}](#{self.web_url}) "
output += "[@#{self.author[:username]}](#{self.author[:web_url]}): "
output += self.title
output += " **[\\✔]**" if self.state == "closed"
output
end
end

def get_mr(issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{ENV["gl_project"]}/issues/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
raise "Negative API Response: #{resp.body}" unless resp.success?
MergeRequest.from_json(resp.body)
end

Loading…
Cancel
Save