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

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

问题描述

您好,我一直在尝试使用 webhook,我想知道您如何通过带有自定义头像和名称的 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 webhook,您需要向 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.接下来确保将 headers 设置为 '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天全站免登陆