Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

How can you make a mimic command using discord.js v12?

I was wondering how to make my bot send a message with a profile picture and nickname of someone else. Many bots have this feature, and it can be quite useful for example for suggestions logs where you could immediately see, who thought of the suggestion. I'm not sure if I explained it well enough, so I will provide you with image examples here.

Mimic command

Suggestions logging

I couldn't find any tutorials/articles regarding this, so if it is too complicated to explain, link to a tutorial would be definetely enough.

question from:https://stackoverflow.com/questions/65944681/how-can-you-make-a-mimic-command-using-discord-js-v12

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

They use webhooks, which can be created like:

const webhook = await channel.createWebhook('Some-username', {
    avatar: 'https://i.imgur.com/wSTFkRM.png',
})

So you'd want to set the username and avatar to mimic the target. Then, to fire the webhook, you need to install node-fetch (npm i node-fetch), and:

const fetch = require('node-fetch');

const webhookURL = webhook.url;
await fetch(webhookURL, {
    headers: {'Content-Type': 'application/json' },
    method: 'POST',
    body: JSON.stringify({
        content: 'your message here'
    })
});

and that will fire the webhook. Then, delete the webhook:

await webhook.delete()

That's the basics of webhooks, to find out more try this video. Hope that helped!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...