Skip to content

Commit

Permalink
Start working on event templates
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 3, 2023
1 parent f1bfdcd commit ed0157b
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"no-unreachable": 1,
"no-unreachable-loop": 2,
"no-unsafe-finally": 2,
"no-unused-vars": [2, { "argsIgnorePattern": "next", "varsIgnorePattern": "_" }],
"no-unused-vars": [2, { "argsIgnorePattern": "next", "varsIgnorePattern": "registerSlashcommands" }],
"no-useless-computed-key": 2,
"no-useless-rename": 2,
"no-useless-escape": 2,
Expand Down Expand Up @@ -95,7 +95,7 @@
"no-constant-binary-expression": 2,
"no-sequences": 2,
"no-irregular-whitespace": [2, { "skipRegExps": true }],
"no-constant-condition": 2,
"no-constant-condition": 1,
"no-undefined": 2,
"no-lone-blocks": 2,
"no-whitespace-before-property": 2,
Expand Down
114 changes: 0 additions & 114 deletions generate.js

This file was deleted.

19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const bot = require("./bot.js")

const { botId, botSecret, userAgent, domain, port, cookieSecret } = require("./config.json")

const fs = require("node:fs")
const path = require("node:path")

const encode = s => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;")

const oauth = require("./util/oauth.js")
Expand Down Expand Up @@ -242,16 +244,23 @@ const hookFunc = async (req, res) => {
if (hook.filterEvent && !JSON.parse(hook.filterEvent).includes(githubEvent)) return res.status(202).send({success: true, info: "Event " + encode(githubEvent) + " is disabled in settings for this hook"})

const data = req.body
console.log(data)
const action = data.action
if (hook.filterAction && !JSON.parse(hook.filterAction).includes(action)) return res.status(202).send({success: true, info: "Action " + encode(action) + " is disabled in settings for this hook"})

let message = hook.message || require(path.join(__dirname, "templates", githubEvent + ".js")).find(msg => msg.action == action) || require(path.join(__dirname, "templates", githubEvent + ".js"))[0]
console.log(message)
const recursiveFunc = (obj, path = "") => {
let message = hook.message
if (!message) {
const githubEventSani = githubEvent.replace(/[^a-z0-9_]/gi, "")
if (!fs.existsSync(path.join(__dirname, "templates", githubEventSani + ".js"))) return res.status(500).send({success: false, error: "No message & no default template for event " + encode(githubEvent)})

message = JSON.stringify(require(path.join(__dirname, "templates", githubEventSani + ".js")).find(msg => msg.action == action) || require(path.join(__dirname, "templates", githubEventSani + ".js"))[0])
}

const recursiveFunc = (obj, currentPath = "") => {
for (const property in obj) {
if (typeof obj[property] == "object") recursiveFunc(obj[property], path + property + ".")
if (typeof obj[property] == "object") recursiveFunc(obj[property], currentPath + property + ".")
// Possible syntax: {sender.login} or {{ sender.login }} or something in between
else message = message.replace(new RegExp("{{? ?" + path + property + " ?}}?", "gi"), obj[property])
else message = message.replace(new RegExp("{{? ?" + currentPath + property + " ?}}?", "gi"), obj[property])
}
}
recursiveFunc(data)
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"cookie-parser": "^1.4.6",
"discord.js": "^14.13.0",
"express": "^4.18.2",
"mysql2": "^3.6.2",
"mysql2": "^3.6.3",
"nodemon": "^3.0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion templates/discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = [
icon_url: "{{ sender.avatar_url }}"
},
title: "`discussion` (`edited`)",
color: color("black")
color: color("cyan")
}]
},{
action: "labeled",
Expand Down
4 changes: 2 additions & 2 deletions templates/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`member` (`edited`)",
color: color("black")
title: "Member permissions edited",
color: color("cyan")
}]
},{
action: "removed",
Expand Down
4 changes: 2 additions & 2 deletions templates/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`meta` (`deleted`)",
color: color("red")
title: "This webhook has been deleted!",
color: color("darkRed")
}]
}
]
16 changes: 8 additions & 8 deletions templates/projects_v2_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`projects_v2_item` (`created`)",
title: "Item created on project board",
color: color("green")
}]
},{
Expand All @@ -38,7 +38,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`projects_v2_item` (`deleted`)",
title: "Item deleted from project board",
color: color("red")
}]
},{
Expand All @@ -48,8 +48,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`projects_v2_item` (`edited`)",
color: color("black")
title: "Item edited on project board",
color: color("cyan")
}]
},{
action: "reordered",
Expand All @@ -58,8 +58,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`projects_v2_item` (`reordered`)",
color: color("black")
title: "Item reordered on project board",
color: color("magenta")
}]
},{
action: "restored",
Expand All @@ -68,8 +68,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`projects_v2_item` (`restored`)",
color: color("black")
title: "Item restored on project board",
color: color("darkGreen")
}]
}
]
2 changes: 1 addition & 1 deletion templates/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "`public`",
title: "[{{ repository }}] Now open source!",
color: color("black")
}]
}
Expand Down
5 changes: 2 additions & 3 deletions util/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ const colorMap = {
cyan: 0x00ffff,
magenta: 0xff00ff,
brown: 0x964b00,
olive: 0x808000,
teal: 0x008080,
navy: 0x000080
darkGreen: 0x006400,
darkRed: 0x8b0000
}

module.exports = (color = "") => {
Expand Down

0 comments on commit ed0157b

Please sign in to comment.