-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add receiving discord notifications when tracked user lists a new item
- Loading branch information
1 parent
50795a2
commit 5b57812
Showing
10 changed files
with
237 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
server/ecommerce/src/discord-bot/src/commands/notifiers/item-notifier.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ItemNotifierService } from './item-notifier.service'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Follow } from 'src/utils/entities/followers.entity'; | ||
import { User } from 'discord.js'; | ||
import { Profile } from 'src/utils/entities/profile.entity'; | ||
import { Avatar } from 'src/utils/entities/avatar.entity'; | ||
import { Product } from 'src/utils/entities/product.entity'; | ||
import { ProductNotification } from 'src/utils/entities/product-notification.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([ | ||
Follow, | ||
User, | ||
Profile, | ||
Avatar, | ||
Product, | ||
ProductNotification, | ||
]), | ||
], | ||
providers: [ItemNotifierService, UsersService, DiscordNotificationsService], | ||
exports: [ItemNotifierService], | ||
}) | ||
export class ItemNotifierModule {} |
24 changes: 24 additions & 0 deletions
24
server/ecommerce/src/discord-bot/src/commands/notifiers/item-notifier.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ItemNotifier } from './item-notifier'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service'; | ||
import { Product } from 'src/utils/entities/product.entity'; | ||
|
||
@Injectable() | ||
export class ItemNotifierService { | ||
private readonly itemNotifier: ItemNotifier; | ||
|
||
constructor( | ||
private usersService: UsersService, | ||
private discordNotificationsService: DiscordNotificationsService, | ||
) { | ||
this.itemNotifier = new ItemNotifier({ | ||
bot: this.discordNotificationsService.discordNotificationsBot, | ||
usersService: this.usersService, | ||
}); | ||
} | ||
|
||
public notifyUsers = async (userId: number, product: Product) => { | ||
return await this.itemNotifier.notifyUsers(userId, product); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
server/ecommerce/src/discord-notifications/discord-notifications-bot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { | ||
AutocompleteInteraction, | ||
ChatInputCommandInteraction, | ||
Client, | ||
Events, | ||
MessageCreateOptions, | ||
REST, | ||
Routes, | ||
} from 'discord.js'; | ||
import { SlashCommand } from 'src/discord-bot/src/commands/slash-command'; | ||
import { InteractionError } from 'src/discord-bot/src/errors/interactionError'; | ||
|
||
type Config = { | ||
bot: Client; | ||
botToken: string; | ||
commands: SlashCommand[]; | ||
botApplicationId: string; | ||
}; | ||
|
||
export class DiscordNotificationsBot { | ||
private readonly logger: Logger; | ||
private readonly bot: Client; | ||
private readonly botToken: string; | ||
private readonly botApplicationId: string; | ||
private readonly commands = new Map<string, SlashCommand>(); | ||
|
||
constructor(config: Config) { | ||
this.logger = new Logger(DiscordNotificationsBot.name); | ||
this.bot = config.bot; | ||
this.botToken = config.botToken; | ||
this.botApplicationId = config.botApplicationId; | ||
config.commands.forEach((c) => this.commands.set(c.config.name, c)); | ||
} | ||
|
||
public start = async () => { | ||
await this.bot.login(this.botToken); | ||
this.bot.on('ready', this.onReady); | ||
}; | ||
|
||
private onReady = async () => { | ||
await this.registerCommands(); | ||
this.listenToInteractions(); | ||
this.logger.log('DiscordNotificationsBot has been started'); | ||
}; | ||
|
||
private registerCommands = async () => { | ||
const rest = new REST().setToken(this.botToken); | ||
const url = Routes.applicationCommands(this.botApplicationId); | ||
await rest.put(url, { | ||
body: Array.from(this.commands.values()).map((c) => c.config.toJSON()), | ||
}); | ||
}; | ||
|
||
private handleCommand = async (interaction: ChatInputCommandInteraction) => { | ||
const command = this.commands.get(interaction.commandName); | ||
if (!command) return; | ||
|
||
try { | ||
await command.execute(interaction); | ||
} catch (error) { | ||
const isInteractionError = error instanceof InteractionError; | ||
const msg = isInteractionError | ||
? error.message | ||
: 'There was an error while executing this command!'; | ||
|
||
if (!isInteractionError) { | ||
this.logger.error( | ||
`Error when executing ${interaction.commandName} command`, | ||
); | ||
} | ||
if (interaction.replied || interaction.deferred) { | ||
this.logger.error( | ||
`Error when executing ${interaction.commandName} command`, | ||
); | ||
await interaction.followUp({ | ||
content: msg, | ||
ephemeral: true, | ||
}); | ||
} else { | ||
await interaction.reply({ | ||
content: msg, | ||
ephemeral: true, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
private listenToInteractions = () => { | ||
this.bot.on(Events.InteractionCreate, async (interaction) => { | ||
if (interaction.isChatInputCommand()) { | ||
await this.handleCommand(interaction); | ||
} else if (interaction.isAutocomplete()) { | ||
await this.handleAutocomplete(interaction); | ||
} | ||
}); | ||
}; | ||
|
||
private handleAutocomplete = async (interaction: AutocompleteInteraction) => { | ||
const command = this.commands.get(interaction.commandName); | ||
if (!command) return; | ||
|
||
try { | ||
await command.autocomplete(interaction); | ||
} catch (error) { | ||
this.logger.error( | ||
{ error }, | ||
`Error when executing the ${interaction.commandName} autocomplete`, | ||
); | ||
} | ||
}; | ||
|
||
public sendDM = async ( | ||
userDiscordId: string, | ||
message: MessageCreateOptions, | ||
) => { | ||
try { | ||
await this.bot.users.send(userDiscordId, message); | ||
} catch (error) {} | ||
}; | ||
} |
40 changes: 33 additions & 7 deletions
40
server/ecommerce/src/discord-notifications/discord-notifications.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DiscordBotService } from 'src/discord-bot/discord-bot.service'; | ||
import { ProductsService } from 'src/products/products.service'; | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { Product } from 'src/utils/entities/product.entity'; | ||
import { DiscordNotificationsBot } from './discord-notifications-bot'; | ||
import { Client, IntentsBitField } from 'discord.js'; | ||
|
||
@Injectable() | ||
export class DiscordNotificationsService { | ||
public notify = async (userId: number, product: Product) => { | ||
return; | ||
}; | ||
export class DiscordNotificationsService implements OnModuleInit { | ||
public discordNotificationsBot: DiscordNotificationsBot; | ||
private readonly bot: Client; | ||
private readonly botToken: string; | ||
private readonly botApplicationId: string; | ||
|
||
constructor() { | ||
this.botToken = process.env.DISCORD_BOT_TOKEN; | ||
this.botApplicationId = process.env.DISCORD_CLIENT_ID; | ||
this.bot = new Client({ | ||
intents: [ | ||
IntentsBitField.Flags.Guilds, | ||
IntentsBitField.Flags.GuildMessages, | ||
], | ||
allowedMentions: { | ||
parse: ['everyone', 'roles', 'users'], | ||
}, | ||
}); | ||
|
||
this.discordNotificationsBot = new DiscordNotificationsBot({ | ||
commands: [], | ||
bot: this.bot, | ||
botToken: this.botToken, | ||
botApplicationId: this.botApplicationId, | ||
}); | ||
} | ||
|
||
async onModuleInit() { | ||
await this.discordNotificationsBot.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters