directus-extension-discord-operation
Version:
Send text or rich embed messages to Discord channels/threads
2 lines (1 loc) • 1.27 kB
JavaScript
const e={basic:{color:3447003},success:{color:5763719,footer:{text:"Success"}},error:{color:15548997,footer:{text:"Error"}},info:{color:3426654,footer:{text:"Info"}}};var t={id:"discord",handler:async({channelId:t,messageType:o,message:r,embedTemplate:s,embedTitle:a,embedDescription:n,embedAuthorName:i,embedAuthorIcon:c,embedAuthorUrl:d,embedThumbnail:m,embedImage:l,embedFooterText:h,embedFooterIcon:b,embedTimestamp:u,embedFields:p,token:f})=>{try{let g;if("simple"===o)g={content:r};else{const t={...e[s]||e.basic,title:a,description:n};if(i&&(t.author={name:i,...c&&{icon_url:c},...d&&{url:d}}),m&&(t.thumbnail={url:m}),l&&(t.image={url:l}),h&&(t.footer={text:h,...b&&{icon_url:b}}),u&&(t.timestamp=(new Date).toISOString()),p)try{const e=JSON.parse(p);Array.isArray(e)&&(t.fields=e)}catch(e){}g={embeds:[t]}}const y=await fetch(`https://discord.com/api/v10/channels/${t}/messages`,{method:"POST",headers:{Authorization:`Bot ${f}`,"Content-Type":"application/json"},body:JSON.stringify(g)});if(!y.ok){const e=await y.json();throw new Error(`Discord API Error: ${e.message}`)}const I=await y.json();return{status:"success",messageId:I.id,channelId:I.channel_id,type:o}}catch(e){throw new Error(`Failed to send Discord message: ${e.message}`)}}};export{t as default};