-
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.
feat/add items list in discord message
- Loading branch information
Showing
2 changed files
with
44 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import axios from 'axios'; | ||
import Order from '../models/order'; | ||
import log from '../utils/log'; | ||
Check failure on line 3 in src/utils/sendDiscordMessage.ts GitHub Actions / lint (18)
|
||
import PlaceAndDiscord from "../models/placeAndDiscord"; | ||
|
||
const sendOrderToDiscordApi = async (order: Order) => { | ||
const token = process.env.DISCORD_API_PRIVATE_KEY; | ||
log.info('Sending order to discord...'); | ||
|
||
const items = '- ' + order.orderItems | ||
Check failure on line 10 in src/utils/sendDiscordMessage.ts GitHub Actions / lint (18)
|
||
.map(orderItem => orderItem.item.name) | ||
.join('\n- '); | ||
|
||
try { | ||
const discordId: string | null = (await PlaceAndDiscord.findByPk(order.place))?.discordId; | ||
if (!discordId) { | ||
log.info('No discord id found for this place, aborting'); | ||
return; | ||
} | ||
log.info(`Discord id found : ${discordId}`); | ||
const res = await axios.post( | ||
`https://discord.com/api/users/@me/channels`, | ||
{ recipient_id: discordId }, | ||
{ | ||
headers: { | ||
Authorization: `Bot ${token}`, | ||
}, | ||
}, | ||
); | ||
const { id: dmId }: { id: string } = res.data; | ||
const content = `Ta commande est prête, viens la chercher !\n\nDétail de la commande :\n${items}\n\nNuméro de commande : **${order.place}**`; | ||
await axios.post( | ||
`https://discord.com/api/channels/${dmId}/messages`, | ||
{ content: content}, | ||
Check failure on line 34 in src/utils/sendDiscordMessage.ts GitHub Actions / lint (18)
|
||
{ headers: { Authorization: `Bot ${token}` } } | ||
); | ||
log.info(`Discord message sent to : ${discordId}`); | ||
} catch (error) { | ||
log.warn(`Discord message error : ${error}`); | ||
} | ||
}; | ||
|
||
export default sendOrderToDiscordApi; |