-
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.
Merge pull request #40 from ungdev/fix/discord-message
Fix/discord-message
- Loading branch information
Showing
6 changed files
with
48 additions
and
64 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
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 './log'; | ||
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 | ||
.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 }, | ||
{ headers: { Authorization: `Bot ${token}` } } | ||
); | ||
log.info(`Discord message sent to : ${discordId}`); | ||
} catch (error) { | ||
log.warn(`Discord message error : ${error}`); | ||
} | ||
}; | ||
|
||
export default sendOrderToDiscordApi; |
This file was deleted.
Oops, something went wrong.