0% found this document useful (0 votes)
32 views3 pages

Index Js

Uploaded by

furkantkaya26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Index Js

Uploaded by

furkantkaya26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import fetch from 'node-fetch';

import WebSocket from 'ws';


import tls from 'tls';

// Configuration
const config = {
token:
"MTI2NDQ3NjY1NzE2Nzc2MTQ2NQ.G_qUBt.YORMgagSpsG94G_1kvJ9WbWRMtuh0EdCMvJPjw", //
User token
guilds: [
{ id: "1242935750698467368", specialUrl: "19977" },
{ id: "1242935750698467368", specialUrl: "19977" },
{ id: "1242935750698467368", specialUrl: "19977" }
],
notificationChannelId: "1242935848597717073"
};

const { token, guilds, notificationChannelId } = config;

const tlsSocket = tls.connect({ host: 'canary.discord.com', port: 443 });

tlsSocket.on('secureConnect', () => {
console.log("Connected to Discord via TLS.");

const websocket = new WebSocket('wss://gateway.discord.gg/?v=9&encoding=json');

websocket.onopen = () => {
console.log("WebSocket connection opened.");
websocket.send(JSON.stringify({
op: 2,
d: {
token,
properties: {
$os: 'linux',
$browser: 'node.js',
$device: 'node.js'
}
}
}));
};

websocket.onclose = (event) => {


console.error("WebSocket connection closed:", event.reason);
process.exit();
};

websocket.onerror = (error) => {


console.error("WebSocket error:", error.message);
};

websocket.onmessage = async (message) => {


const { d, t } = JSON.parse(message.data);

if (t === 'READY') {
console.log("Ready event received.");
console.log("Connected to the following guilds:");
d.guilds.forEach(guild => console.log(`Guild ID: ${guild.id}`));
}
if (t === 'GUILD_UPDATE') {
console.log("GUILD_UPDATE event received.");

const processedGuilds = new Set();

const requestPromises = guilds.map(guild => {


if (!processedGuilds.has(guild.id)) {
processedGuilds.add(guild.id);
console.log(`Fetching data for guild ID: ${guild.id}`);
return fetch(`https://discord.com/api/v9/guilds/${guild.id}`, {
headers: { 'Authorization': token }
}).then(response => ({ response, guildId: guild.id, specialUrl:
guild.specialUrl }));
}
return null;
}).filter(promise => promise !== null);

try {
const responses = await Promise.all(requestPromises);

for (let { response, guildId, specialUrl } of responses) {


if (!response.ok) {
console.error(`Failed to fetch data for guild ID: ${guildId}`);
continue;
}

const data = await response.json();


console.log(`Data received for guild ID: ${guildId}`);

if (data.vanity_url_code === specialUrl) {


const logMessage = `Special URL (${specialUrl}) is now available in
guild ${data.name}`;
console.log(logMessage);

await fetch(`https://discord.com/api/v9/channels/$
{notificationChannelId}/messages`, {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify({ content: logMessage })
});
} else {
console.log(`Special URL (${specialUrl}) is not available in guild $
{data.name}`);

// Try to set the vanity URL to the special URL


const setVanityUrlResponse = await
fetch(`https://discord.com/api/v9/guilds/${guildId}/vanity-url`, {
method: 'PATCH',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify({ code: specialUrl })
});

if (setVanityUrlResponse.ok) {
console.log(`Successfully set the vanity URL for guild ID: $
{guildId}`);
} else {
console.error(`Failed to set the vanity URL for guild ID: $
{guildId}`);
}
}
}
} catch (error) {
console.error("Error processing guild update:", error.message);
}
}
};
});

tlsSocket.on('error', (error) => {


console.error("TLS connection error:", error.message);
});

You might also like