send-discord-message
Version:
Send message to a Discord channel using channel webhooks.
2 lines (1 loc) • 1.04 kB
JavaScript
;var n=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var h=(r,o)=>{for(var t in o)n(r,t,{get:o[t],enumerable:!0})},w=(r,o,t,s)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of c(o))!d.call(r,e)&&e!==t&&n(r,e,{get:()=>o[e],enumerable:!(s=a(o,e))||s.enumerable});return r};var p=r=>w(n({},"__esModule",{value:!0}),r);var f={};h(f,{default:()=>i});module.exports=p(f);var i=class{constructor(o){this.webhookUrl=o;if(!o||!o.startsWith("https://discord.com/api/webhooks/"))throw new Error("Invalid Discord webhook URL")}async send(o){if(!o||typeof o!="string")throw new Error("Message must be a non-empty string");try{let t=await fetch(this.webhookUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:o})});if(!t.ok){let s=await t.text();throw new Error(`Discord webhook failed: ${t.status} ${s}`)}}catch(t){throw console.error("Failed to send to Discord:",t instanceof Error?t.message:t),t}}};