Skip to content

Commit

Permalink
More templates & code fixes & ternary vars
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 3, 2023
1 parent aabbfee commit 043f8e5
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 24 deletions.
16 changes: 15 additions & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -64,6 +70,7 @@ const registerSlashcommands = () => {
name: "hook",
type: Discord.ApplicationCommandOptionType.String,
description: "The webhook to delete",
maxLength: 32,
autocomplete: true,
required: true
}]
Expand All @@ -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 => {
Expand Down Expand Up @@ -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})
Expand Down
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions templates/deploy_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}]
Expand All @@ -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")
}]
Expand Down
12 changes: 7 additions & 5 deletions templates/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
}]
}
]
2 changes: 1 addition & 1 deletion templates/deployment_protection_rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}]
Expand Down
4 changes: 2 additions & 2 deletions templates/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}]
}
Expand Down
6 changes: 3 additions & 3 deletions templates/issue_comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}]
Expand All @@ -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")
}]
Expand All @@ -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")
}]
Expand Down
18 changes: 12 additions & 6 deletions templates/org_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
}]
}
]

0 comments on commit 043f8e5

Please sign in to comment.