[英]Discord Oauth2: How do I get & list guilds?
一些機器人有儀表板,它們有一個顯示“管理服務器”的頁面。 如何獲取我的機器人所在的公會並列出它們? 以下是一些關於我如何獲得授權用戶名、頭像、鑒別器等的代碼片段。
window.onload = () => {
const fragment = new URLSearchParams(window.location.hash.slice(1));
const [accessToken, tokenType] = [fragment.get('access_token'), fragment.get('token_type')];
if (!accessToken) {
window.location.href('/')
}
fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `${tokenType} ${accessToken}`,
},
})
.then(result => result.json())
.then(response => {
console.log(response);
const { username, discriminator, avatar, id} = response;
//set the welcome username string
document.getElementById('h1').innerText = ` ${username}#${discriminator}`;
//set the avatar image by constructing a url to access discord's cdn
document.getElementById("avatar").src = `https://cdn.discordapp.com/avatars/${id}/${avatar}.jpg`;
})
.catch(console.error);
};
</script>```
Thank you! :)
根據我在大多數具有此類功能的機器人中看到的情況,可能的方法如下:
express
服務器,然后在與他們的機器人相同的目錄中使用 ejs。 示例: https://github.com/Tomato6966/Musicium更具體地說,他們如何做到這一點: https://github.com/Tomato6966/Musicium/blob/main/dashboard/index.js如果您真的不介意泄露您的機器人令牌,您可以在客戶端向 discord api 發出 api 請求,如下所示:
fetch("https://discord.com/api/v6/users/@me/guilds", {
headers: {
"Authorization": "Bot <TOKEN_HERE>"
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.