Skip to content

Commit 2b2653e

Browse files
authored
Add guild notifications (#13)
* Create server notification helper * Create guildCreate event * Create guild delete event
1 parent 2401e15 commit 2b2653e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/events/guildCreate/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Guild, WebhookClient } from 'discord.js';
2+
import { isEmpty } from 'lodash';
3+
import { GUILD_NOTIFICATION_WEBHOOK_URL, USE_DATABASE } from '../../config/environment';
4+
import { insertNewGuild } from '../../services/database';
5+
import { serverNotificationEmbed } from '../../utils/helpers';
6+
import { EventModule } from '../events';
7+
8+
export default function ({ app }: EventModule) {
9+
app.on('guildCreate', async (guild: Guild) => {
10+
try {
11+
USE_DATABASE && (await insertNewGuild(guild));
12+
if (GUILD_NOTIFICATION_WEBHOOK_URL && !isEmpty(GUILD_NOTIFICATION_WEBHOOK_URL)) {
13+
const embed = await serverNotificationEmbed({ app, guild, type: 'join' });
14+
const notificationWebhook = new WebhookClient({ url: GUILD_NOTIFICATION_WEBHOOK_URL });
15+
await notificationWebhook.send({
16+
embeds: [embed],
17+
username: 'My App Server Notification',
18+
avatarURL: '',
19+
});
20+
}
21+
} catch (error) {
22+
//TODO: Add error handling
23+
console.log(error);
24+
}
25+
});
26+
}

src/events/guildDelete/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Guild, WebhookClient } from 'discord.js';
2+
import { isEmpty } from 'lodash';
3+
import { GUILD_NOTIFICATION_WEBHOOK_URL, USE_DATABASE } from '../../config/environment';
4+
import { deleteGuild } from '../../services/database';
5+
import { serverNotificationEmbed } from '../../utils/helpers';
6+
import { EventModule } from '../events';
7+
8+
export default function ({ app }: EventModule) {
9+
app.on('guildDelete', async (guild: Guild) => {
10+
try {
11+
USE_DATABASE && (await deleteGuild(guild));
12+
if (GUILD_NOTIFICATION_WEBHOOK_URL && !isEmpty(GUILD_NOTIFICATION_WEBHOOK_URL)) {
13+
const embed = await serverNotificationEmbed({ app, guild, type: 'leave' });
14+
const notificationWebhook = new WebhookClient({ url: GUILD_NOTIFICATION_WEBHOOK_URL });
15+
await notificationWebhook.send({
16+
embeds: [embed],
17+
username: 'My App Server Notification',
18+
avatarURL: '',
19+
});
20+
}
21+
} catch (error) {
22+
//TODO: Add error handling
23+
console.log(error);
24+
}
25+
});
26+
}

src/utils/helpers.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { APIEmbed, Client, Guild } from 'discord.js';
2+
3+
export const serverNotificationEmbed = async ({
4+
app,
5+
guild,
6+
type,
7+
}: {
8+
app: Client;
9+
guild: Guild;
10+
type: 'join' | 'leave';
11+
}): Promise<APIEmbed> => {
12+
const defaultIcon =
13+
'https://cdn.discordapp.com/attachments/248430185463021569/614789995596742656/Wallpaper2.png';
14+
const guildIcon = guild.icon && guild.iconURL();
15+
const guildOwner =
16+
type === 'join'
17+
? await guild.members.fetch(guild.ownerId).then((guildMember) => guildMember.user.tag)
18+
: '-';
19+
20+
const embed = {
21+
title: type === 'join' ? 'Joined a new server' : 'Left a server',
22+
description: `I'm now in **${app.guilds.cache.size}** servers!`,
23+
color: type === 'join' ? 55296 : 16711680,
24+
thumbnail: {
25+
url: guildIcon ? guildIcon.replace(/jpeg|jpg/gi, 'png') : defaultIcon,
26+
},
27+
fields: [
28+
{
29+
name: 'Name',
30+
value: guild.name,
31+
inline: true,
32+
},
33+
{
34+
name: 'Owner',
35+
value: guildOwner,
36+
inline: true,
37+
},
38+
{
39+
name: 'Members',
40+
value: guild.memberCount.toString(),
41+
inline: true,
42+
},
43+
],
44+
};
45+
return embed;
46+
};

0 commit comments

Comments
 (0)