Lin*_*ips 1 node.js discord discord.js
您好,我一直在尝试使用 Webhook,我想知道如何通过具有自定义头像和名称的 Webhook 发送普通消息(未嵌入)
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]
})
}
```
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您希望发送 Discord webhook,您需要向 webhook url 发出 POST API 请求。
为此,您基本上可以使用您想要的任何模块,但在本例中我将使用node-fetch. 只需将其安装在您的控制台中即可
npm install node-fetch
Run Code Online (Sandbox Code Playgroud)
然后在需要使用的地方需要它
const fetch = require('node-fetch');
Run Code Online (Sandbox Code Playgroud)
现在我们已经拥有了让它工作所需的一切,让我们创建 API 请求。
为此,我们从params变量开始。您可以在此处设置使 Webhook 看起来像您想要的样子的所有内容。注意:我还介绍了如何发送嵌入内容,以防万一。如果您想查看所有选项,请选中此处。
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
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我们有了参数,我们可以创建实际的 POST 请求。为此,您只需调用该fetch函数并提供 webhook url 即可。
首先,您指定要使用的方法。默认情况下该方法是GET. 接下来确保将标头设置为'Content-type': 'application/json',否则您会收到错误。最后包括params身体前面的内容。我们用JSON.stringify()这里来让它工作。
fetch('URL', {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(params)
}).then(res => {
console.log(res);
})
Run Code Online (Sandbox Code Playgroud)
最后,您可以选择捕获可能收到的任何错误。
| 归档时间: |
|
| 查看次数: |
13225 次 |
| 最近记录: |