admin管理员组文章数量:1401849
I want to send a message in my Discord server using a Webhook. Is there any way to do that using JavaScript?
I tried this code, but it didn't work:
var url = // Webhook URL
var text = '{"content":"Hi"}' // Text to post
$.ajax({
data: 'payload=' + JSON.stringify({
"text": text
}),
dataType: 'json',
processData: false,
type: 'POST',
url: url
});
Souce.
I want to send a message in my Discord server using a Webhook. Is there any way to do that using JavaScript?
I tried this code, but it didn't work:
var url = // Webhook URL
var text = '{"content":"Hi"}' // Text to post
$.ajax({
data: 'payload=' + JSON.stringify({
"text": text
}),
dataType: 'json',
processData: false,
type: 'POST',
url: url
});
Souce.
Share Improve this question edited Oct 31, 2023 at 21:04 NebularNerd 1022 silver badges10 bronze badges asked May 5, 2018 at 8:09 Adit LuhadiaAdit Luhadia 4121 gold badge8 silver badges18 bronze badges 2- @Boy With Silver Wings Thanks a lot! Hope it works :) – Adit Luhadia Commented May 5, 2018 at 8:29
-
2
if you want to post a json you should specify ` contentType: "application/json"` in your request. Also
'payload={"text": "..."}'
is not a valid json payload. – nutic Commented May 5, 2018 at 8:34
2 Answers
Reset to default 3<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Discord Webhook Tutorial</title>
</head>
<body>
<button onclick="sendMessage()">Send</button>
</body>
<script>
function sendMessage() {
var request = new XMLHttpRequest();
request.open("POST", "https://discordapp./api/webhooks/676118118082281513/ZS5YcWhurzokBrKX9NgexqtxrJA5Pu2Bo4i7_JsIxC-JIbPBVhSZkcVVukGOro52rnQA");
request.setRequestHeader('Content-type', 'application/json');
var params = {
username: "My Webhook Name",
avatar_url: "",
content: "The message to send"
}
request.send(JSON.stringify(params));
}
</script>
</html>
A WebHook url is still a Universal Resource Locator is it not? They are typically defined on Http so if you want to call a Webhook you would do so over Http. This means a typical fetch
request can make a WebHook call.
In your case, you are posting a “Hi”
message to the WebHook so it should look something like this:
var url = // Webhook URL
var text = {content: "Hi"}
fetch(url,
{
method: “POST”,
headers: {“Content-Type”: “application/json”},
body: JSON.stringify(text),
}
)
.then(res => •••); /*Do whatever you want with the response when it arrives.*/
The assumption is that the WebHook expects a payload with Content-Type”: “application/json”
via a Post
request at that endpoint.
AFAIK, WebHooks are a way servers municate with each other not client-server munication but that’s disputable.
本文标签: discordIs there any way to post a webhook using JavaScriptStack Overflow
版权声明:本文标题:discord - Is there any way to post a webhook using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744266562a2597974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论