/ clis / discord-app / servers.js
servers.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  export const serversCommand = cli({
 3      site: 'discord-app',
 4      name: 'servers',
 5      description: 'List all Discord servers (guilds) in the sidebar',
 6      domain: 'localhost',
 7      strategy: Strategy.UI,
 8      browser: true,
 9      args: [],
10      columns: ['Index', 'Server'],
11      func: async (page) => {
12          const servers = await page.evaluate(`
13        (function() {
14          const results = [];
15          // Discord guild icons in the sidebar
16          const items = document.querySelectorAll('[data-list-item-id*="guildsnav___"], [class*="listItem_"]');
17          
18          items.forEach((item, i) => {
19            const nameAttr = item.querySelector('[data-dnd-name]');
20            const ariaLabel = item.getAttribute('aria-label') || (item.querySelector('[aria-label]') || {}).getAttribute?.('aria-label');
21            const name = nameAttr ? nameAttr.getAttribute('data-dnd-name') : (ariaLabel || (item.textContent || '').trim());
22            
23            if (name && name.length > 0) {
24              results.push({ Index: i + 1, Server: name.substring(0, 80) });
25            }
26          });
27          
28          return results;
29        })()
30      `);
31          if (servers.length === 0) {
32              return [{ Index: 0, Server: 'No servers found' }];
33          }
34          return servers;
35      },
36  });