/ clis / discord-app / send.js
send.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  export const sendCommand = cli({
 3      site: 'discord-app',
 4      name: 'send',
 5      description: 'Send a message in the active Discord channel',
 6      domain: 'localhost',
 7      strategy: Strategy.UI,
 8      browser: true,
 9      args: [{ name: 'text', required: true, positional: true, help: 'Message to send' }],
10      columns: ['Status'],
11      func: async (page, kwargs) => {
12          const text = kwargs.text;
13          await page.evaluate(`
14        (function(text) {
15          // Discord uses a Slate-based editor with [data-slate-editor="true"] or role="textbox"
16          const editor = document.querySelector('[role="textbox"][data-slate-editor="true"], [class*="slateTextArea"]');
17          if (!editor) throw new Error('Could not find Discord message input. Make sure a channel is open.');
18          
19          editor.focus();
20          document.execCommand('insertText', false, text);
21        })(${JSON.stringify(text)})
22      `);
23          await page.wait(0.3);
24          await page.pressKey('Enter');
25          return [{ Status: 'Success' }];
26      },
27  });