Skip to content

Commit

Permalink
Merge branch 'dummy-notification-and-service-name' into 'dev'
Browse files Browse the repository at this point in the history
add dummy notification

Closes #9 and #10

See merge request ergo/rosen-bridge/notification!11
  • Loading branch information
zargarzadehm committed Sep 20, 2024
2 parents c33a9f3 + c30e43e commit bebacc7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-pigs-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/abstract-notification': minor
---

Dummy Notification class
5 changes: 5 additions & 0 deletions .changeset/nasty-chicken-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/discord-notification': minor
---

Add service name and update message bosy in discord
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ dist

# Sqlite databases
*.sqlite
.idea/*
2 changes: 1 addition & 1 deletion package-lock.json

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

11 changes: 11 additions & 0 deletions packages/abstract-notification/lib/DummyNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AbstractNotification from './AbstractNotification';

class DummyNotification extends AbstractNotification {
error = () => Promise.resolve();
warning = () => Promise.resolve();
success = () => Promise.resolve();
info = () => Promise.resolve();
raw = () => Promise.resolve();
}

export default DummyNotification;
1 change: 1 addition & 0 deletions packages/abstract-notification/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as AbstractNotification } from './AbstractNotification';
export { default as DummyNotification } from './DummyNotification';
export * from './types';
62 changes: 28 additions & 34 deletions packages/discord/lib/DiscordNotification.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { AbstractNotification } from '@rosen-bridge/abstract-notification';
import { WebhookClient } from 'discord.js';
import { EmbedBuilder, WebhookClient } from 'discord.js';

class DiscordNotification extends AbstractNotification {
private client: WebhookClient;

constructor(webhookURL: string) {
constructor(
webhookURL: string,
private serviceName?: string,
) {
super();
this.client = new WebhookClient({
url: webhookURL,
Expand All @@ -13,11 +16,22 @@ class DiscordNotification extends AbstractNotification {

/**
* send a string to Discord, trimming all lines
* @param title
* @param message
* @param color
*/
private sendMessage = async (message: string) => {
private sendMessage = async (
title: string,
message: string,
color: number,
) => {
const embed = new EmbedBuilder()
.setAuthor({ name: this.serviceName ?? ' ' })
.setTitle(title)
.setDescription(message.replace(/^ +| +$/gm, ''))
.setColor(color);
await this.client.send({
content: message.replace(/^ +| +$/gm, ''), // trim all lines
embeds: [embed],
});
};

Expand All @@ -26,60 +40,40 @@ class DiscordNotification extends AbstractNotification {
* @param title
* @param message
*/
error = async (title: string, message: string) => {
await this.sendMessage(`
## ❌ ${title}
${message}
`);
};
error = async (title: string, message: string) =>
this.sendMessage(`❌ ${title}`, message, 0xd32f2f);

/**
* send a warning notification
* @param title
* @param message
*/
warning = async (title: string, message: string) => {
await this.sendMessage(`
## ⚠️ ${title}
${message}
`);
};
warning = async (title: string, message: string) =>
this.sendMessage(`⚠️ ${title}`, message, 0xed6c02);

/**
* send an info notification
* @param title
* @param message
*/
info = async (title: string, message: string) => {
await this.sendMessage(`
## ℹ️ ${title}
${message}
`);
};
info = async (title: string, message: string) =>
this.sendMessage(`ℹ️ ${title}`, message, 0x0288d1);

/**
* send a success notification
* @param title
* @param message
*/
success = async (title: string, message: string) => {
await this.sendMessage(`
## ✅ ${title}
${message}
`);
};
success = async (title: string, message: string) =>
this.sendMessage(`✅ ${title}`, message, 0x2e7d32);

/**
* send a raw notification
* @param title
* @param message
*/
raw = async (title: string, message: string) => {
await this.sendMessage(`
## ${title}
${message}
`);
};
raw = async (title: string, message: string) =>
this.sendMessage(`${title}`, message, 0x000000);
}

export default DiscordNotification;

0 comments on commit bebacc7

Please sign in to comment.