Skip to content

Commit

Permalink
Merge pull request #42 from dogboydog/embeds
Browse files Browse the repository at this point in the history
Support 'embeds' parameter to discord API
  • Loading branch information
Ilshidur committed Sep 2, 2020
2 parents ba393b9 + 778ea88 commit 8135f7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ e.g.: `Action called: {{ GITHUB_ACTION }} as {{ EVENT_PAYLOAD.pull_request.id }}
* ***IMPORTANT !!* You MUST NOT append `/github` at the end of the webhook.**
* **`DISCORD_USERNAME`** (*optional*): overrides the bot nickname.
* **`DISCORD_AVATAR`** (*optional*): overrides the avatar URL.
* **`DISCORD_EMBEDS`** (*optional*): This should be a valid JSON string of an array of Discord `embed` objects. See the [documentation on Discord WebHook Embeds](https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html) for more information. You can use set it to `${{ toJson(my_value) }}` using [`toJson()`](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#tojson) if your input is an object value.
* That's all.

## Alternatives
Expand Down
17 changes: 14 additions & 3 deletions entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
let url;
let payload;

if (argv._.length === 0) {
// If argument NOT provided, let Discord show the event informations.
if (argv._.length === 0 && !process.env.DISCORD_EMBEDS) {
// If argument and embeds NOT provided, let Discord show the event informations.
url = `${process.env.DISCORD_WEBHOOK}/github`;
payload = JSON.stringify(JSON.parse(eventContent));
} else {
// Otherwise, if the argument is provided, let Discord override the message.
// Otherwise, if the argument or embeds are provided, let Discord override the message.
const args = argv._.join(' ');
const message = _.template(args)({ ...process.env, EVENT_PAYLOAD: JSON.parse(eventContent) });

let embedsObject;
if (process.env.DISCORD_EMBEDS) {
try {
embedsObject = JSON.parse(process.env.DISCORD_EMBEDS);
} catch (parseErr) {
console.error('Error parsing DISCORD_EMBEDS :' + parseErr);
process.exit(1);
}
}

url = process.env.DISCORD_WEBHOOK;
payload = JSON.stringify({
content: message,
...process.env.DISCORD_EMBEDS && { embeds: embedsObject },
...process.env.DISCORD_USERNAME && { username: process.env.DISCORD_USERNAME },
...process.env.DISCORD_AVATAR && { avatar_url: process.env.DISCORD_AVATAR },
});
Expand Down

0 comments on commit 8135f7f

Please sign in to comment.