discord-rb
Version:
Discord.rb is a library for interacting with the Discord API.
87 lines (62 loc) • 2.58 kB
JavaScript
const fetch = require("node-fetch")
const Errors = require("../errors/Errors.js")
const req = require("request")
//represents a discord channel.
const Channel = async (channelID, token) => {
var options = {
"method": "GET",
"headers": {
"Authorization": `Bot ${token}`,
"Cookie": "__cfduid=d62bef48329670a370c27df97f1a81eba1587580440; __cfruid=bf4b45e1a6e5ef27d99bc35b0eab7581881c3b8c-1587580440"
}
}
const request = await fetch(`https://discordapp.com/api/channels/${channelID}`, options)
const channel = await request.json()
if (!channel.id) return undefined
const data = {
//the id of this channel.
ID: channel.id,
//the type of this channel.
Type: {
0: "GuildTextChannel",
1: "UserDMChannel",
4: "GuildCategoryChannel"
}[channel.type],
//the name of this channel.
Name: channel.name,
//the id of this guild (if the channel belongs to a guild)
GuildID: channel.guild_id,
//wether if the channel is nsfw or not.
NSFW: channel.nsfw,
//the topic of this channel.
Topic: channel.topic,
//the position of this channel.
Position: channel.position,
//sends a normal message to this channel (if the bot is on this guild).
SendMessage: async (content) => {
if (typeof content !== "string") throw new TypeError(Errors.STRING)
if (content === "") throw new TypeError(Errors.EMPTY_MESSAGE)
let msg = {
"content": content
}
var options = {
"method": "POST",
"url": `https://discordapp.com/api/channels/${channel.id}/messages`,
"headers": {
"Authorization": `Bot ${token}`,
"content-type": "application/json",
"Cookie": "__cfduid=d691ce9608d2f2417dea1a984b9cb46aa1587146073; __cfruid=1c5bf67d2aa9e7bc76ced134a29a051b862dc121-1587146073"
},
body: JSON.stringify(msg)
}
req(options, (error, response) => {
let res = JSON.parse(response.body)
if (res.message === "Unknown Channel") throw new TypeError(Errors.UNKNOWN)
if (res.message === "401: Unauthorized") throw new TypeError(Errors.UNAUTHORIZED)
else return res
})
}
}
return data
}
module.exports = Channel