mlg
mlg

Reputation: 1511

Sending message on Discord via Puppeteer (no bot, no application)

I'm writing a Puppeteer script that opens Discord Web, logs in (manual step for captcha & authorize), goes to a channel and enters a message into the message form, and I'm struggling to send it.

This is different to all existing Q&As, because it's not via a Discord Webhook or Application.

The complication seems to be that the element where the message is entered in is a contenteditable div.

Another possibility is that simulating the Enter key is working, but it's not sending anything because setting the innerText on the div does not make it register that that's the message to be sent. I noticed that the placeholder text doesn't disappear.

Check the screenshot to see the exact element. Discords message element

The code that does the message sending looks like this:

const selector = 'div[contenteditable=true]:nth-child(2)';
page.goto('https://discord.com/channels/2358929835296835/39057203957203');
await page.waitForSelector(selector);
await page.focus(selector);
await page.$eval(selector, el => el.innerText = 'Hello, world!');
await page.keyboard.press((String.fromCharCode(13) as any));

Would you give me ideas on things to try to send that message? The code works up to putting 'Hello, world' onto the message form.

UPDATE: looks like Discord is using this library called Slate for the message form: https://github.com/ianstormtaylor/slate, which builds a nested tree of spans as you type. I managed to edit the right span with some text, but I still can't trigger it to send.

I've asked the creator this same question but no results yet.

Upvotes: 4

Views: 2364

Answers (1)

karl_kfoury
karl_kfoury

Reputation: 56

i remember making something similar, and i think the text is contained in a span deep under the DIV ure referencing. also changing the innerText can be unreliable so try clicking and typing instead

const selector = '#app-mount > div.app-1q1i1E > div > div.layers-3iHuyZ.layers-3q14ss > div > div > div > div.content-98HsJk > div.chat-3bRxxu > div.content-yTz4x3 > main > form > div:nth-child(1) > div > div > div.scrollableContainer-2NUZem.webkit-HjD9Er > div > div.textArea-12jD-V.textAreaSlate-1ZzRVj.slateContainer-3Qkn2x > div.markup-2BOw-j.slateTextArea-1Mkdgw.fontSize16Padding-3Wk7zP'
page.goto('https://discord.com/channels/2358929835296835/39057203957203') 
await page.waitForSelector(selector, {timeout: 0})
var text = await page.$(selector)
await text.click()
await text.type("hello world")
await page.keyboard.press("Enter")

Upvotes: 4

Related Questions