Skip to content

Commit

Permalink
Added embed message support
Browse files Browse the repository at this point in the history
  • Loading branch information
chamorin committed Jun 1, 2023
1 parent 95f4776 commit c3d50f3
Show file tree
Hide file tree
Showing 6 changed files with 654 additions and 414 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ Supports `JSON` and `YAML` format for configuration file. Example of `config.jso
"message": "Hello user! This **{user_id}** made a __transaction__ with id `{trx_id}`"
},
{
"entity": "Grants",
"entity": "Grant",
"chat_ids": [
"1098279427617603636"
],
"message": "This ||{grant}||"
"message": "This ||{grant}||",
"embed": {
"title": "Embed message title {user_id}",
"description": "Embed message description {user_id}"
}
}
]
```
Expand All @@ -81,4 +85,5 @@ Text between `{}` are field names and are used as labels for message templating.

### Discord
- [x] Handle rate limit
- [x] Markdown message parsing
- [x] Markdown message parsing
- [x] Embed message support
8 changes: 6 additions & 2 deletions examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
"message": "Hello user! This **{user_id}** made a __transaction__ with id `{trx_id}`"
},
{
"entity": "Grants",
"entity": "Grant",
"chat_ids": [
"1098279427617603636"
],
"message": "This ||{grant}||"
"message": "This ||{grant}||",
"embed": {
"title": "Embed message title {user_id}",
"description": "Embed message description {user_id}"
}
}
]
28 changes: 26 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,32 @@ export async function action(manifest: string, moduleName: string, options: Acti
const substreams = run(spkg, moduleName, options);

substreams.on("anyMessage", async (messages: EntityChanges) => {
await social.distributeMessages(messages, (chatId, message, config) => {
discordBot.sendMessage(chatId, message, config);
messages.entityChanges.forEach(async (entityChange) => {
social.configs.forEach(async (conf: any) => {
if (entityChange.entity === conf.entity) {
const formattedMessage = social.formatMessage(entityChange, conf.message);
let formattedEmbed: any;

if (conf.embed) {
formattedEmbed = JSON.stringify(conf.embed);

entityChange.fields.forEach(async (field) => {
formattedEmbed = String(formattedEmbed).replaceAll(`{${field.name}}`, field.newValue?.typed.case === "array" ? field.newValue!.typed.value.value.map(v => (v.typed.value)).join() : field.newValue?.typed.value as string);
});

formattedEmbed = JSON.parse(formattedEmbed);
}

conf.chat_ids.forEach(async (chatId: string) => {
if (conf.embed) {
await social.queue.add(() => discordBot.sendEmbedMessage(chatId, formattedMessage, formattedEmbed, conf));
} else {
await social.queue.add(() => discordBot.sendTextMessage(chatId, formattedMessage, conf));
}

});
}
});
});
});

Expand Down
Loading

0 comments on commit c3d50f3

Please sign in to comment.