Discord.js发送一个Webhook [英] Discord.js send a webhook

查看:59
本文介绍了Discord.js发送一个Webhook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在尝试使用webhooks,但我想知道您如何通过带有自定义头像和名称的webhook发送普通消息(未嵌入)

Hi I have been experimenting with webhooks and I'm wondering how do you send a normal message(not embeded) through a webhook with a custom avatar and name


        const user = message.mentions.users.first() || client.users.cache.get(args[0]);
        let announcement = args.slice(1).join(" ");
        if(!announcement) return message.channel.send(`lol say something`)

        const wc = new WebhookClient('id', 'token')
        const embed = new MessageEmbed()
            .setTitle("").setColor('GREEN').setTimestamp().setDescription(announcement)
    wc.send({
        username : user.username,
        avatarURL : user.displayAvatarURL({ dynamic : true }),
        embeds : [embed]
    })
    
    }
    ```

推荐答案

如果您希望发送Discord webhooks,则需要向webhook url发出POST API请求.

If you wish to send Discord webhooks you need to make a POST API request to the webhook url.

为此,您基本上可以使用所需的任何模块,但是在此示例中,我将使用 node-fetch .只需将其安装在您的控制台中

For that you can basically use any module you want however in this example I'll use node-fetch. Simply install it in your console

npm install node-fetch

然后在需要使用它的地方要求它

and then require it where you need to use it

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

现在,我们拥有使它正常工作所需的东西,就可以创建API请求.

Now that we have what we need to make it work lets create the API request.

为此,我们从 params 变量开始.在这里,您可以设置所有使Webhook看起来像您想要的外观的东西.注意:我还介绍了如何发送嵌入内容以防万一.如果要查看所有选项,请在此处.

For that we start with the params variable. Here you set all the things that make the webhook look like you want it to look. Note: I also included how to send embeds just in case. If you want to see all options check here.

var params = {
    username: "Your name",
    avatar_url: "",
    content: "Some message you want to send",
    embeds: [
        {
            "title": "Some title",
            "color": 15258703,
            "thumbnail": {
                "url": "",
            },
            "fields": [
                {
                    "name": "Your fields here",
                    "value": "Whatever you wish to send",
                    "inline": true
                }
            ]
        }
    ]
}

现在有了参数,我们可以创建实际的POST请求.为此,您只需调用 fetch 函数并提供webhook URL.

Now that we have the params we can create the actual POST request. For that you simply call the fetch function and provide the webhook url.

首先,您指定要使用的方法.默认情况下,该方法为 GET .接下来,请确保将标头设置为'Content-type':'application/json',否则会出现错误.最后,在正文中加入 params .我们在这里使用 JSON.stringify()使其正常工作.

First you specify the method you want to use. By default the method is GET. Next make sure to set the headers to 'Content-type': 'application/json', otherwise you'll get an error. Lastly include the params from earlier in the body. We use JSON.stringify() here to make it work.

fetch('URL', {
    method: "POST",
    headers: {
        'Content-type': 'application/json'
    },
    body: JSON.stringify(params)
}).then(res => {
    console.log(res);
}) 

最后,您可以选择捕获可能收到的任何错误.

At the end you have the option to catch any errors you might receive.

这篇关于Discord.js发送一个Webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆