From 043f8e5e3eb36b867edd292e31b78fcb69286fb9 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:15:17 +0100 Subject: [PATCH] More templates & code fixes & ternary vars --- bot.js | 16 +++++++++++++++- index.js | 21 +++++++++++++++++---- templates/deploy_key.js | 4 ++-- templates/deployment.js | 12 +++++++----- templates/deployment_protection_rule.js | 2 +- templates/fork.js | 4 ++-- templates/issue_comment.js | 6 +++--- templates/org_block.js | 18 ++++++++++++------ 8 files changed, 59 insertions(+), 24 deletions(-) diff --git a/bot.js b/bot.js index 16fff46..8e91b1f 100644 --- a/bot.js +++ b/bot.js @@ -53,8 +53,14 @@ const registerSlashcommands = () => { name: "hook", type: Discord.ApplicationCommandOptionType.String, description: "The webhook to edit", + maxLength: 32, autocomplete: true, required: true + },{ + name: "message", + type: Discord.ApplicationCommandOptionType.String, + description: "The new message as template name, JSON object or array", + required: true }] },{ name: "delete", @@ -64,6 +70,7 @@ const registerSlashcommands = () => { name: "hook", type: Discord.ApplicationCommandOptionType.String, description: "The webhook to delete", + maxLength: 32, autocomplete: true, required: true }] @@ -73,7 +80,8 @@ const registerSlashcommands = () => { bot.on("ready", () => { bot.user.setPresence({activities: [{name: "Custom Status", state: "Customizable GitHub hooks!", type: Discord.ActivityType.Custom}], status: "online"}) - //registerSlashcommands() + + if (Math.random() > 0.95) registerSlashcommands() }) bot.on("guildCreate", guild => { @@ -141,6 +149,12 @@ bot.on("interactionCreate", async interaction => { ephemeral: true }) }) + } else if (subcommand == "edit-message") { + const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ? AND `server` = ?", [interaction.options.getString("hook"), interaction.guild.id]) + if (rows.length == 0) return interaction.reply({content: "The hook `" + interaction.options.getString("hook") + "` doesn't exist or doesn't belong to this server.", ephemeral: true}) + + await pool.query("UPDATE `hook` SET `message` = ? WHERE `id` = ?", [interaction.options.getString("message"), interaction.options.getString("hook")]) + interaction.reply({content: "Successfully updated the hook message of **" + rows[0].name + "**.", ephemeral: true}) } else if (subcommand == "delete") { const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ? AND `server` = ?", [interaction.options.getString("hook"), interaction.guild.id]) if (rows.length == 0) return interaction.reply({content: "The hook `" + interaction.options.getString("hook") + "` doesn't exist or doesn't belong to this server.", ephemeral: true}) diff --git a/index.js b/index.js index f44da70..c66045e 100644 --- a/index.js +++ b/index.js @@ -32,10 +32,10 @@ app.use((req, res, next) => { if (ratelimit5m[ip] && ratelimit5m[ip] >= 130) return res.status(429).send("Too many requests in the last 5 minutes") if (ratelimitGlobal5m >= 800) return res.status(429).send("Too many requests in the last 5 minutes") - if (req.path.startsWith("/hook/")) return next() - - if (ratelimit30s[ip]) ratelimit30s[ip]++ - else ratelimit30s[ip] = 1 + if (!req.path.startsWith("/hook/")) { + if (ratelimit30s[ip]) ratelimit30s[ip]++ + else ratelimit30s[ip] = 1 + } if (ratelimit5m[ip]) ratelimit5m[ip]++ else ratelimit5m[ip] = 1 ratelimitGlobal5m++ @@ -265,6 +265,19 @@ const hookFunc = async (req, res) => { } recursiveFunc(data) + // Handling ternary + message = message.replace(/{{ ?[^}]+ ?\? ?[^}]+ ?: ?[^}]+ ?}}/gi, match => { + const parts = match.replace(/{{ ?| ?}}/gi, "").split("?") + return parts[0] && parts[0] != "false" ? parts[1] : parts[2] + }) + // Handling OR + message = message.replace(/{{ ?[^}]+ ?(\|\| ?[^}]+ ?){1,5}}}/gi, match => { + const parts = match.replace(/{{ ?| ?}}/gi, "").split("||") + return parts.find(part => part && part != "false") + }) + // Removing empty variables + message = message.replace(/{{ ?[^}]+ ?}}/gi, "") + let parsed = {} try { parsed = JSON.parse(message) diff --git a/templates/deploy_key.js b/templates/deploy_key.js index 7c7b12a..c501622 100644 --- a/templates/deploy_key.js +++ b/templates/deploy_key.js @@ -8,7 +8,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `deploy_key` (`created`)", + title: "[{{ repository.full_name }}] Deploy key **{{ key.title }}** created", url: "{{ repository.html_url }}", color: color("green") }] @@ -19,7 +19,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `deploy_key` (`deleted`)", + title: "[{{ repository.full_name }}] Deploy key **{{ key.title }}** deleted", url: "{{ repository.html_url }}", color: color("red") }] diff --git a/templates/deployment.js b/templates/deployment.js index 19defa2..1d9371e 100644 --- a/templates/deployment.js +++ b/templates/deployment.js @@ -5,12 +5,14 @@ module.exports = [ action: "created", embeds: [{ author: { - name: "{{ sender.login }}", - icon_url: "{{ sender.avatar_url }}" + name: "{{ deployment.creator.login }}", + icon_url: "{{ deployment.creator.avatar_url || sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `deployment` (`created`)", - url: "{{ repository.html_url }}", - color: color("green") + title: "[{{ repository.name }}:{{ repository.default_branch }}] Deployment created", + url: "{{ deployment.url }}", + description: "{{ deployment.description }}\n\n{{ deployment.environment }}", + color: color("green"), + timestamp: "{{ deployment.created_at }}" }] } ] diff --git a/templates/deployment_protection_rule.js b/templates/deployment_protection_rule.js index 61b02c2..156560b 100644 --- a/templates/deployment_protection_rule.js +++ b/templates/deployment_protection_rule.js @@ -8,7 +8,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `deployment_protection_rule` (`requested`)", + title: "[{{ repository.name }}:{{ repository.default_branch }}] Deployment protection rule requested", url: "{{ repository.html_url }}", color: color("black") }] diff --git a/templates/fork.js b/templates/fork.js index 34f5ea5..864cf2c 100644 --- a/templates/fork.js +++ b/templates/fork.js @@ -7,8 +7,8 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `fork`", - url: "{{ repository.html_url }}", + title: "[{{ forkee.full_name }}] Fork created", + url: "{{ forkee.html_url }}", color: color("black") }] } diff --git a/templates/issue_comment.js b/templates/issue_comment.js index 2372c94..8da3af0 100644 --- a/templates/issue_comment.js +++ b/templates/issue_comment.js @@ -8,7 +8,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `issue_comment` (`created`)", + title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment created", url: "{{ repository.html_url }}", color: color("green") }] @@ -19,7 +19,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `issue_comment` (`deleted`)", + title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment deleted", url: "{{ repository.html_url }}", color: color("red") }] @@ -30,7 +30,7 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `issue_comment` (`edited`)", + title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment edited", url: "{{ repository.html_url }}", color: color("cyan") }] diff --git a/templates/org_block.js b/templates/org_block.js index 5b535ed..9704bbc 100644 --- a/templates/org_block.js +++ b/templates/org_block.js @@ -8,9 +8,12 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `org_block` (`blocked`)", - url: "{{ repository.html_url }}", - color: color("black") + title: "[{{ organization.name }}] User **{{ blocked_user.login }}** blocked", + url: "{{ organization.html_url }}", + thumbnail: { + url: "{{ blocked_user.avatar_url }}" + }, + color: color("red") }] },{ action: "unblocked", @@ -19,9 +22,12 @@ module.exports = [ name: "{{ sender.login }}", icon_url: "{{ sender.avatar_url }}" }, - title: "[{{ repository.name }}:{{ repository.default_branch }}] `org_block` (`unblocked`)", - url: "{{ repository.html_url }}", - color: color("black") + title: "[{{ organization.name }}] User **{{ blocked_user.login }}** unblocked", + url: "{{ organization.html_url }}", + thumbnail: { + url: "{{ blocked_user.avatar_url }}" + }, + color: color("green") }] } ]