Fix issue referencing quirks (#5)

master
Lars Müller 2020-03-28 19:22:16 +01:00 committed by GitHub
parent 1d3b384750
commit b807fc86a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 13 deletions

View File

@ -1,3 +1,8 @@
const {valid_issue_states} = require("./config.json");
const valid_issue_states_lookup = {}
for (const state of valid_issue_states) {
valid_issue_states_lookup[state] = true;
}
const {color} = require("./config.js");
const max_length = 256;
const {MessageEmbed} = require("discord.js");
@ -5,6 +10,9 @@ const request = require("request");
function sendGitHubEmbedReply(message, issue) {
let embed = new MessageEmbed();
if (!valid_issue_states_lookup[issue.state]) {
return;
}
embed.setURL(issue.html_url);
embed.setTitle(`**${issue.title.trim()}**`);
embed.setAuthor(issue.user.login, issue.user.avatar_url, issue.user.html_url);

View File

@ -1,5 +1,7 @@
{
"prefix": "!",
"token": "Bot ABCDEFGHIJKLMNOPQRSTUVWXYZ.1234567890",
"color": "#FF0000"
"color": "#FF0000",
"reference_min_number": 99,
"valid_issue_states": ["open"]
}

View File

@ -1,6 +1,7 @@
const fs = require("fs");
const Discord = require("discord.js");
const {prefix, token} = require('./config.json');
const {prefix, token, reference_min_num} = require('./config.json');
const reference_min_number = reference_min_num || 0
const request = require("request");
const {sendGitHubEmbedReply} = require("./common.js");
@ -74,19 +75,32 @@ client.on("message", async message => {
}
}
// No valid command, look for #d+, referencing pulls or issues
for (const match of message.content.matchAll(/#(\d+)/g)) {
const number = match[1];
request({
url: "https://api.github.com/repos/minetest/minetest/issues/" + number,
json: true,
headers: {
"User-Agent": "Minetest Bot"
for (const match of message.content.matchAll(/(^|\s+)#(\d+)/g)) {
let count = 0;
for (let i = 0; i < match.index; i++) {
if (message.content.charAt(i) === "`") {
count++;
}
}, function(err, res, pkg) {
if (pkg.url) {
sendGitHubEmbedReply(message, pkg);
}
// even number of code block starters => all code blocks have been closed
// no code block terminator after #id => code block containing id hasn't been closed
let not_inside_codeblock = (count % 2 === 0) || (message.content.indexOf("`", match.index + match[0].length) < 0);
if (not_inside_codeblock) {
const number = parseInt(match[2]);
if (number >= reference_min_number) {
request({
url: "https://api.github.com/repos/minetest/minetest/issues/" + number,
json: true,
headers: {
"User-Agent": "Minetest Bot"
}
}, function(err, res, pkg) {
if (pkg.url) {
sendGitHubEmbedReply(message, pkg);
}
});
}
});
}
}
} catch (error) {
console.error(error);