Skip to content

Instantly share code, notes, and snippets.

@littletsu
Created September 18, 2018 05:52
Show Gist options
  • Save littletsu/b06cce533359f6cb7ac68b4901ae6317 to your computer and use it in GitHub Desktop.
Save littletsu/b06cce533359f6cb7ac68b4901ae6317 to your computer and use it in GitHub Desktop.
Mini wrapper around discord API. Dependencies: snekfetch, ws. Heavily based in rylib by ry00001 (https://github.com/ry00001)
const fetch = require('snekfetch')
const ws = require("ws")
var logHeartbeat = false
// Weebsocket
get('gateway').then(gateway => {
let weebsocket = new ws(gateway.url)
weebsocket.onmessage = (message) => {
let data = JSON.parse(message.data)
let seq = data.s
let parsed = data.d
function send(opcode, d) {
weebsocket.send(JSON.stringify({
op: opcode,
d: d
}))
}
// Hello
if(data.op == 10) {
let interval = parsed.heartbeat_interval
console.log('Hello received, heartbeat interval is: ' + interval + 'ms')
setInterval(() => {
send(1, seq)
logHeartbeat ? console.log('Heartbeat sent.')
: null
}, interval)
send(2, {
token: process.env.TOKEN,
"properties": {
"$os": "linux",
"$browser": "etzyy-wrapper",
"$device": "etzyy-wrapper"
},
compress: false,
large_threshold: 250,
presence: {
game: {
name: 'oof:hi',
type: 0
},
status: 'online',
since: new Date().valueOf(),
afk: false
}
})
console.log('Identified')
}
// Heartbeat
if(data.op == 1) {
send(1, seq)
}
// Events
if(data.op == 0) {
let event = data.t
// Ready
if(event == "READY") {
let self = parsed
console.log(`Ready as ` + self.user.username + ` (${self.user.id})`)
}
// Message
if(event == "MESSAGE_CREATE") {
let msg = parsed
if(msg.content == "oof:hi") {
send_message(msg.channel_id, 'Hoi!')
react_message(msg.channel_id, msg.id, '👋')
}
}
}
}
weebsocket.onclose = (why) => {
console.log(`Disconnected to websocket: ${why.reason}. Terminating process...`)
process.exit()
}
weebsocket.onopen = () => {
console.log('Connected to websocket')
}
})
// 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
headers.Authorization = 'Bot ' + process.env.TOKEN
let req = await fetch.post(url, {headers: headers}).send(data)
return req.body
}
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}).send(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 send_message(channel_id, content) {
return await post(`channels/${channel_id}/messages`, {}, {content: content})
}
async function react_message(channel_id, message_id, unicode_emoji) {
return await put(`/channels/${channel_id}/messages/${message_id}/reactions/${unicode_emoji}/@me`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment