UNPKG

send-discord-message

Version:

Send message to a Discord channel using channel webhooks.

2 lines (1 loc) 585 B
var r=class{constructor(t){this.webhookUrl=t;if(!t||!t.startsWith("https://discord.com/api/webhooks/"))throw new Error("Invalid Discord webhook URL")}async send(t){if(!t||typeof t!="string")throw new Error("Message must be a non-empty string");try{let o=await fetch(this.webhookUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:t})});if(!o.ok){let e=await o.text();throw new Error(`Discord webhook failed: ${o.status} ${e}`)}}catch(o){throw console.error("Failed to send to Discord:",o instanceof Error?o.message:o),o}}};export{r as default};