3 lines
2.4 KiB
JavaScript
3 lines
2.4 KiB
JavaScript
(function(h,r,w,p){"use strict";const B=p.findByProps("sendMessage","receiveMessage"),c=p.findByProps("sendBotMessage");let m={},g={},M,y,v;function C(n,s,t){const e=`${n}:${t}`,o=t-Date.now();return g[e]={content:s,at:t},m[e]=setTimeout(function(){const a=s,i=Date.now().toString();B.sendMessage(n,{content:a},void 0,{nonce:i}),delete m[e],delete g[e],w.showToast("Scheduled message sent.")},o),e}function D(){Object.values(m).forEach(clearTimeout),m={},g={}}function I(n){const s=new Date;n=n.toLowerCase();const t=/(\d+)(h|m|s)/g;let e=0,o;for(;(o=t.exec(n))!==null;){const d=parseInt(o[1]),u=o[2];u==="h"?e+=d*3600*1e3:u==="m"?e+=d*60*1e3:u==="s"&&(e+=d*1e3)}if(e>0)return Date.now()+e;const a=/^(\d{1,2}):(\d{2})(am|pm)?$/,i=n.match(a);if(i){let d=parseInt(i[1]);const u=parseInt(i[2]),f=i[3];f&&(f==="pm"&&d<12?d+=12:f==="am"&&d===12&&(d=0));const l=new Date(s);return l.setHours(d,u,0,0),l.getTime()<=s.getTime()&&l.setDate(l.getDate()+1),l.getTime()}return 0}function $(){M=r.registerCommand({name:"schedule",displayName:"schedule",description:"Schedule a message after a delay or at a time",options:[{name:"time",description:"Delay (e.g. 10s, 1h30m) or time (3:30pm, 15:30)",type:3,required:!0},{name:"message",description:"Message to send",type:3,required:!0}],execute:function(n,s){const t=s.channel.id,e=n[0]?.value,o=n[1]?.value;if(typeof e!="string"||e.trim()==="")return c.sendBotMessage(t,"Invalid time value!");if(typeof o!="string"||o.trim()==="")return c.sendBotMessage(t,"Message cannot be empty!");const a=I(e);if(a===0||a<=Date.now())return c.sendBotMessage(t,"Invalid or past time specified!");C(t,o,a);const i=Math.round((a-Date.now())/1e3);c.sendBotMessage(t,`Scheduled message in ${i}s.`)}}),y=r.registerCommand({name:"scheduled",displayName:"scheduled",description:"List scheduled messages",execute:function(n,s){const t=s.channel.id,e=Object.entries(g).map(function([o,a]){return`\u2022 **In ${Math.round((a.at-Date.now())/1e3)}s** \u2192 ${a.content}`});e.length===0?c.sendBotMessage(t,"No scheduled messages."):c.sendBotMessage(t,`Scheduled messages:
|
|
${e.join(`
|
|
`)}`)}}),v=r.registerCommand({name:"cancel-scheduled",displayName:"cancel-scheduled",description:"Cancel all scheduled messages",execute:function(n,s){const t=s.channel.id;D(),c.sendBotMessage(t,"All scheduled messages cancelled.")}})}function T(){D(),r.unregisterCommand(M),r.unregisterCommand(y),r.unregisterCommand(v)}return h.onLoad=$,h.onUnload=T,h})({},vendetta.commands,vendetta.ui.toasts,vendetta.metro);
|