-
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.
- Loading branch information
1 parent
ef7ddee
commit a24b0ec
Showing
15 changed files
with
177 additions
and
20 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
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
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
54 changes: 54 additions & 0 deletions
54
server/ecommerce/src/discord-guild/discord-guild.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,54 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DiscordGuildService } from './discord-guild.service'; | ||
import { DiscordBotService } from 'src/discord-bot/discord-bot.service'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { IProductsService } from 'src/spi/products'; | ||
import { ProductsService } from 'src/products/products.service'; | ||
import { FollowersService } from 'src/followers/followers.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Follow } from 'src/utils/entities/followers.entity'; | ||
import { User } from 'src/utils/entities/user.entity'; | ||
import { Profile } from 'src/utils/entities/profile.entity'; | ||
import { Avatar } from 'src/utils/entities/avatar.entity'; | ||
import { Image } from 'src/utils/entities/image.entity'; | ||
import { ProductNotification } from 'src/utils/entities/product-notification.entity'; | ||
import { Product } from 'src/utils/entities/product.entity'; | ||
import { ProductNotificationService } from 'src/product-notification/product-notification.service'; | ||
import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item-notifier.service'; | ||
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([ | ||
Follow, | ||
User, | ||
Profile, | ||
Avatar, | ||
Image, | ||
ProductNotification, | ||
Product, | ||
]), | ||
], | ||
providers: [ | ||
DiscordBotService, | ||
UsersService, | ||
ProductNotificationService, | ||
ItemNotifierService, | ||
DiscordNotificationsService, | ||
{ | ||
provide: IProductsService, | ||
useClass: ProductsService, | ||
}, | ||
FollowersService, | ||
{ | ||
provide: DiscordGuildService, | ||
useFactory: (botService: DiscordBotService) => | ||
new DiscordGuildService({ | ||
botClient: botService.bot, | ||
}), | ||
inject: [DiscordBotService], | ||
}, | ||
], | ||
exports: [DiscordGuildService], | ||
}) | ||
export class DiscordGuildModule {} |
82 changes: 82 additions & 0 deletions
82
server/ecommerce/src/discord-guild/discord-guild.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,82 @@ | ||
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { Client, MessageCreateOptions } from 'discord.js'; | ||
import 'dotenv/config'; | ||
|
||
type Config = { | ||
botClient: Client; | ||
}; | ||
|
||
@Injectable() | ||
export class DiscordGuildService { | ||
private readonly botClient: Client; | ||
private readonly guildId: string; | ||
private readonly logger: Logger; | ||
|
||
constructor(config: Config) { | ||
this.botClient = config.botClient; | ||
this.logger = new Logger(DiscordGuildService.name); | ||
this.guildId = process.env.GUILD_ID ?? ''; | ||
} | ||
|
||
public assignRoles = async (userId: string, roleIds: string[]) => { | ||
if (roleIds.length === 0) { | ||
return; | ||
} | ||
|
||
roleIds = roleIds.filter((roleId) => !this.hasRole(userId, roleId)); | ||
|
||
const member = await this.getGuildMember(userId); | ||
try { | ||
await member.roles.add(roleIds); | ||
} catch (error) { | ||
this.logger.error( | ||
`Error while trying to assign roles for user ${userId}`, | ||
); | ||
} | ||
}; | ||
|
||
private hasRole = async (userId: string, roleId: string) => { | ||
try { | ||
const member = await this.getGuildMember(userId); | ||
|
||
return !!member && member.roles.cache.some((role) => role.id === roleId); | ||
} catch (error) { | ||
this.logger.error(`Error while getting roles for user ${userId}`); | ||
} | ||
}; | ||
|
||
private getGuildMember = async (userId: string) => { | ||
const guild = await this.getGuild(); | ||
|
||
try { | ||
return ( | ||
guild.members.cache.get(userId) ?? (await guild.members.fetch(userId)) | ||
); | ||
} catch (error) { | ||
this.logger.error('Error when getting guild member'); | ||
} | ||
}; | ||
|
||
private getGuild = async () => { | ||
try { | ||
return ( | ||
this.botClient.guilds.cache.get(this.guildId) ?? | ||
(await this.botClient.guilds.fetch(this.guildId)) | ||
); | ||
} catch (error) { | ||
this.logger.error(`Error when getting guild ${this.guildId}`); | ||
return undefined; | ||
} | ||
}; | ||
|
||
public sendMessageToMember = async ( | ||
userId: string, | ||
message: MessageCreateOptions, | ||
) => { | ||
try { | ||
await this.botClient.users.send(userId, message); | ||
} catch (error) { | ||
this.logger.error(`Error when trying to send message to ${userId}`); | ||
} | ||
}; | ||
} |
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
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
File renamed without changes.
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