Skip to content

Instantly share code, notes, and snippets.

@z3ro0k
Created October 31, 2020 07:34
Show Gist options
  • Save z3ro0k/f03360347c805ebe3ff87ee09fc0a84b to your computer and use it in GitHub Desktop.
Save z3ro0k/f03360347c805ebe3ff87ee09fc0a84b to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ToDo List //
// //
// Code in beta, not working in this version, migration the methods to axios //
// //
// //
// //
// //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
var request = require('request-promise-native')
const fetch = require('axios')
const ws = require("ws")
const dotenv = require('dotenv');
dotenv.config();
fetch.defaults.headers.post['Content-Type'] = 'application/json';
fetch.defaults.headers.common['Authorization'] = 'Bot ' +process.env.TOKEN;
getGateway(process.env.TOKEN).then(async gateway => {
var socket = new ws(gateway)
socket.onmessage = (msg) => {
var data = JSON.parse(msg.data)
var seq = data.s
var parsed = data.d
// Heartbeat
if(data.op == 10) {
var interval = parsed.heartbeat_interval
console.log('op 10: ')
console.log('Hay que mandar ping cada: ' + interval + 'ms')
setInterval(() => {
socket.send(JSON.stringify({
op: 1,
d: seq
}))
console.log('Ping enviado: ' + interval + 'ms')
}, interval)
socket.send(JSON.stringify({
op: 2,
d: {
token: process.env.TOKEN,
properties: {
$os: 'windows',
$browser: 'Android',
$device: 'Android'
},
presence: {
status: 'online',
game: {
name: interval+ ' ms',
type: 0
},
since: new Date().valueOf(),
afk: false
}
}
}))
console.log('Presence y datos enviados: ' + `{
token: process.env.TOKEN,
properties: {
$os: 'windows',
$browser: 'Android',
$device: 'Android'
},
presence: {
status: 'online',
game: {
name: interval+ ' ms',
type: 0
},
since: new Date().valueOf(),
afk: false
}
}}`)
}
if(data.op == 0) {
// Ready
if (data.t === 'READY') {
let self = parsed
console.log(`conectado correctamente (${self.user.username})`)
} else if(data.t == 'MESSAGE_CREATE') { // Message
let msg = parsed
if(msg.content == "z:hola") {
console.log(msg)
send_message(msg.channel_id, 'hola '+ msg.author.username)
// react_message(msg.channel_id, msg.id, '👋')
}
}
}
if(data.op == 1) {
console.log('Se pidio ping, enviando...')
socket.send(JSON.stringify({
op: 1,
d: seq
}))
}
}
socket.onclose = () => {
console.log('(socket cerrado)')
}
socket.onopen = () => {
console.log('(socket abierto)')
}
})
async function getGateway(token) {
var req = await request({url: "https://discordapp.com/api/v6/gateway", headers: {'Authorization': 'Bot ' + token}})
return JSON.parse(req).url
}
// REST
async function get(endpoint, headers = {}) {
let url = 'https://discordapp.com/api/v6/' + endpoint
headers.Authorization = 'Bot ' + process.env.TOKEN
let req = await fetch.get(url, {
headers: headers
})
return req.body
}
async function post(endpoint, headers = {}, data) {
let url = 'https://discordapp.com/api/v6/' + endpoint
console.log(url);
let req = await fetch.post(url, {
data: data
}).catch(e => console.log(e))
return req
}
async function put(endpoint, headers = {}, data = {}) {
let url = 'https://discordapp.com/api/v6/' + endpoint
headers.Authorization = 'Bot ' + process.env.TOKEN
let req = await fetch.put(url, {
headers: headers,
data
})
return req.body
}
async function del(endpoint, headers = {}) {
let url = 'https://discordapp.com/api/v6/' + endpoint
headers.Authorization = 'Bot ' + process.env.TOKEN
let req = await fetch.delete(url, {
headers: headers
})
return req.body
}
async function patch(endpoint, headers = {}, data = {}) {
let url = 'https://discordapp.com/api/v6/' + endpoint
headers.Authorization = 'Bot ' + process.env.TOKEN
let req = await fetch.patch(url, {
headers: headers
}).send(data)
return req.body
}
// Messagay Methods (rej is res, and res is rej)
async function send_message(channel_id, content) {
return new Promise(async (rej, res) => {
rej(await post(`channels/${channel_id}/messages`, {}, content))
})
}
async function react_message(channel_id, message_id, unicode_emoji) {
return new Promise(async (rej, res) => {
rej(await put(`channels/${channel_id}/messages/${message_id}/reactions/${unicode_emoji}/@me`))
})
}
async function edit_message(channel_id, message_id, new_content) {
return new Promise(async (rej, res) => {
rej(await patch(`channels/${channel_id}/messages/${message_id}`, {}, {
content: new_content
}))
})
}
async function send_embed_message(channel_id, embed) {
return new Promise(async (rej, res) => {
rej(await post(`channels/${channel_id}/messages`, {}, {
embed: embed
}))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment