-
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.
update readme and add remove product command
- Loading branch information
1 parent
c9bd5eb
commit a834ab3
Showing
12 changed files
with
159 additions
and
7 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
93 changes: 93 additions & 0 deletions
93
server/ecommerce/src/discord-bot/src/commands/remove-product.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,93 @@ | ||
import { | ||
AutocompleteInteraction, | ||
ChatInputCommandInteraction, | ||
SlashCommandBuilder, | ||
} from 'discord.js'; | ||
import { SlashCommand } from './slash-command'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { ProductsService } from 'src/products/products.service'; | ||
|
||
const PRODUCT_OPTION_NAME = 'product'; | ||
|
||
type Config = { | ||
usersService: UsersService; | ||
productService: ProductsService; | ||
}; | ||
|
||
export class RemoveProduct implements SlashCommand { | ||
readonly config: any = new SlashCommandBuilder() | ||
.setName('remove-product') | ||
.setDescription('Removes one of your listed products') | ||
.addStringOption((option) => | ||
option | ||
.setName(PRODUCT_OPTION_NAME) | ||
.setDescription('Product to remove') | ||
.setRequired(true) | ||
.setAutocomplete(true), | ||
); | ||
|
||
private readonly usersService: UsersService; | ||
private readonly productService: ProductsService; | ||
|
||
constructor(config: Config) { | ||
this.usersService = config.usersService; | ||
this.productService = config.productService; | ||
} | ||
|
||
public autocomplete = async (interaction: AutocompleteInteraction) => { | ||
const userId = interaction.user.id; | ||
const user = await this.getUserWithProducts(userId); | ||
const value = interaction.options.getFocused().toLowerCase(); | ||
const userProducts = user.products; | ||
|
||
const filteredProducts = userProducts | ||
.filter((product) => product.title.toLowerCase().includes(value)) | ||
.slice(0, 25); | ||
|
||
await interaction.respond( | ||
filteredProducts.map((choice) => ({ | ||
name: choice.title, | ||
value: choice.id.toString(), | ||
})), | ||
); | ||
}; | ||
|
||
public execute = async (interaction: ChatInputCommandInteraction) => { | ||
const productArg = interaction.options.getString(PRODUCT_OPTION_NAME); | ||
if (!productArg) { | ||
await interaction.reply({ | ||
content: `Missing "${productArg} option"`, | ||
ephemeral: true, | ||
}); | ||
} | ||
|
||
const productId = parseInt(productArg); | ||
|
||
if (!this.isValidProduct(productId)) { | ||
await interaction.reply({ | ||
content: | ||
'Invalid product ID. Please select a valid product from the autocomplete options.', | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
await this.deleteProductById(productId); | ||
await interaction.reply({ | ||
content: 'Product removed successfully', | ||
ephemeral: true, | ||
}); | ||
}; | ||
|
||
private getUserWithProducts = async (userDiscordId: string) => { | ||
return await this.usersService.findUserByDiscordId(userDiscordId); | ||
}; | ||
|
||
private deleteProductById = async (productId: number) => { | ||
return await this.productService.deleteProduct(productId); | ||
}; | ||
|
||
private isValidProduct = (productArg: number) => { | ||
return !isNaN(productArg); | ||
}; | ||
} |
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
13 changes: 11 additions & 2 deletions
13
server/ecommerce/src/discord-bot/src/commands/slash-command.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,6 +1,15 @@ | ||
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'; | ||
import { | ||
AutocompleteInteraction, | ||
ChatInputCommandInteraction, | ||
SlashCommandBuilder, | ||
} from 'discord.js'; | ||
|
||
export interface SlashCommand { | ||
readonly config: SlashCommandBuilder; | ||
readonly config: Omit< | ||
SlashCommandBuilder, | ||
'addSubcommand' | 'addSubcommandGroup' | ||
>; | ||
|
||
execute(interaction: ChatInputCommandInteraction): Promise<void>; | ||
autocomplete(interaction: AutocompleteInteraction): Promise<void>; | ||
} |
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