disclient
Version:
A powerfull discord api wrapper.
160 lines (159 loc) • 6.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = __importDefault(require("node-fetch"));
const Constants_1 = require("./Constants");
class RestApiHandler {
token;
constructor(token) {
this.token = token;
this.token = token;
}
async fetchAllChannels(guildid) {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/guilds/${guildid}/channels`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return res.json();
}
async fetchOneChannel(channelid) {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/channels/${channelid}`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return res.json();
}
async fetchGuilds() {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/guilds`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return res.json();
}
async fetchOneGuild(guildId) {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/guilds/${guildId}`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return await res.json();
}
async fetchGuildMembers(guildId) {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/guilds/${guildId}/members?limit=1000`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return res.json();
}
async fetchOneGuildMember(memberId, guildId) {
const res = await node_fetch_1.default(`${Constants_1.URI.API}/guilds/${guildId}/members/${memberId}`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` }
});
return res.json;
}
async PostMessage(channel, content) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const data = {
"content": content,
"tts": false,
};
const body = JSON.stringify(data);
const res = await node_fetch_1.default(`${Constants_1.URI.API}/channels/${channel}/messages`, {
method: "POST",
headers,
body,
});
return res.json();
}
async createReaction(channel, message, emoji) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const uri = `${Constants_1.URI.API}/channels/${channel}/messages/${message}/reactions/${emoji}/@me`;
const encodeduri = encodeURI(uri);
await node_fetch_1.default(encodeduri, {
method: "PUT",
headers,
});
}
async deleteMessage(channel, message, reason) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}`, "X-Audit-Log-Reason": reason };
await node_fetch_1.default(`${Constants_1.URI.API}/channels/${channel}/messages/${message}`, {
method: "DELETE",
headers,
});
}
async replyMessage(channel, message, content) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const data = {
"content": content,
"message_reference": {
"message_id": message
},
"tts": false,
};
const body = JSON.stringify(data);
const res = await node_fetch_1.default(`${Constants_1.URI.API}/channels/${channel}/messages`, {
method: "POST",
headers,
body,
});
return res.json();
}
async sendMessagewithEmbedandButton(channel, content, embeds, components) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const data = {
"content": content,
"embeds": embeds,
"components": [
{
"type": 1,
"components": components
}
],
"tts": false,
};
const body = JSON.stringify(data);
const res = await node_fetch_1.default(`${Constants_1.URI.API}/channels/${channel}/messages`, {
method: "POST",
headers,
body,
});
return res.json();
}
async createInteractionResponse(message, interaction_token, interaction_id, emphemeral, intertype) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const data = {
"data": {
"tts": false,
"content": message,
"flags": emphemeral ? 1 << 6 : null,
},
"type": 4
};
const body = JSON.stringify(data);
await node_fetch_1.default(`${Constants_1.URI.API}/interactions/${interaction_id}/${interaction_token}/callback`, {
method: "POST",
headers,
body,
});
}
async editInteractionResponse(message, interaction_token, interaction_id, emphemeral, intertype) {
const headers = { "Content-Type": "application/json", "Authorization": `Bot ${this.token}` };
const data = {
"data": {
"tts": false,
"content": message,
"flags": emphemeral ? 1 << 6 : null,
},
"type": 7
};
const body = JSON.stringify(data);
await node_fetch_1.default(`${Constants_1.URI.API}/interactions/${interaction_id}/${interaction_token}/callback`, {
method: "PATCH",
headers,
body,
});
}
}
exports.default = RestApiHandler;