Files

217 lines
5.9 KiB
JavaScript
Raw Permalink Normal View History

import axios from 'axios';
2023-04-02 09:49:47 -04:00
import author from '../messages/User.js';
import { message } from '../messages/message.js';
import { Channel } from '../guilds/Channel.js';
import {Embed} from '../messages/embed.js';
2023-04-02 09:49:47 -04:00
import Guild from '../guilds/Guild.js';
2023-04-07 20:35:03 -04:00
import { DataManager } from '../DataManager.js';
2023-04-25 17:34:01 -04:00
import { Modal, ModalComponent } from './Modal.js';
import { MessageActionRow } from '../messages/MessageActionRow.js';
2023-03-20 12:45:20 -04:00
2023-04-07 19:13:15 -04:00
2023-04-07 20:12:16 -04:00
class interactionOptions {
/** @type {String} */
name;
/** @type {Number} */
type;
/** @type {[{value: any, type: Number, name: String}]} */
options;
/** @type {Boolean} */
focused;
constructor(o) {
for (const k in this) {
if (o[k]) this[k] = o[k];
}
}
}
2023-03-20 12:45:20 -04:00
2023-04-07 20:35:03 -04:00
export class Interaction extends DataManager {
2023-03-20 12:45:20 -04:00
/** @type {author} */
user;
/** @type {Number} */
type;
/** @type {String} */
#token;
/** @type {String} */
id;
/** @type {String} */
channel_id;
/** @type {Channel} */
channel;
/** @type {String} */
application_id;
2023-04-02 09:49:47 -04:00
/** @type {Guild} */
guild;
/** @type {String} */
guild_id;
/** @type {interactionOptions} */
data;
async deferReply() {
try {
await this.client.axiosCustom.post(`/interactions/${this.id}/${this.#token}/callback`, {
type: 5
});
} catch (err) {
console.log(err);
}
}
2023-04-25 17:34:01 -04:00
/**
* @param {Modal} m
*/
async #sendModal(m) {
const response = await this.client.axiosCustom.post(`/interactions/${this.id}/${this.#token}/callback`, {type: 9, data: m.toObj()});
return response.data;
}
2023-03-20 12:45:20 -04:00
/**
* @param {{content: String, ephemeral?: Boolean, embeds: [Embed]} | String} inp
* @returns {Promise<message>}
*/
async reply(inp) {
2023-04-25 17:34:01 -04:00
//Check for action row
if (inp instanceof Modal) return await this.#sendModal(inp);
2023-03-20 12:45:20 -04:00
return new Promise(async (resolve, reject) => {
const toSend = (typeof inp == 'string') ? inp : inp.content;
var embds = undefined;
if (inp.embeds) {
embds = [];
for (const i of inp.embeds) {
embds.push(i.toJSON());
}
}
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.post(`/interactions/${this.id}/${this.#token}/callback`, {
2023-03-20 12:45:20 -04:00
type: 4,
data: {
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
}
2023-04-07 20:12:16 -04:00
});
2023-03-20 12:45:20 -04:00
2023-04-07 20:12:16 -04:00
resolve(new message(response.data, this.client));
2023-03-20 12:45:20 -04:00
});
}
async update(inp) {
return new Promise(async (resolve, reject) => {
try {
const toSend = (typeof inp == 'string') ? inp : inp.content;
var embds = undefined;
if (inp.embeds) {
embds = [];
for (const i of inp.embeds) {
embds.push(i.toJSON());
}
}
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.patch(`/webhooks/${this.client.id}/${this.#token}/messages/@original`, {
2023-03-20 12:45:20 -04:00
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
2023-04-07 20:12:16 -04:00
});
2023-03-20 12:45:20 -04:00
// const response = await axios.get(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`, config);
2023-04-07 20:12:16 -04:00
resolve(new message(response.data, this.client));
2023-03-20 12:45:20 -04:00
} catch(err) {
reject(err);
}
});
}
/**
* @param {*} inp
* @returns {Promise<message>}
*/
async followUp(inp) {
return new Promise(async (resolve, reject) => {
try {
const toSend = (typeof inp == 'string') ? inp : inp.content;
var embds = undefined;
if (inp.embeds) {
embds = [];
for (const i of inp.embeds) {
embds.push(i.toJSON());
}
}
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.post(`/webhooks/${this.client.id}/${this.#token}`, {
2023-03-20 12:45:20 -04:00
content: toSend,
embeds: embds,
flags: (inp.ephemeral) ? (1 << 6) : undefined
2023-04-07 20:12:16 -04:00
});
2023-03-20 12:45:20 -04:00
// const response = await axios.get(`https://discord.com/api/webhooks/${this.#application.id}/${this.#token}/messages/@original`, config);
2023-04-07 20:12:16 -04:00
resolve(new message(response.data, this.client));
2023-03-20 12:45:20 -04:00
} catch(err) {
reject(err);
}
});
}
/**
* @returns {Promise<Boolean>} Returns true if successful
*/
async delete() {
return new Promise(async (resolve, reject) => {
try {
2023-04-07 20:12:16 -04:00
const response = await this.client.axiosCustom.delete(`/webhooks/${this.client.id}/${this.#token}/messages/@original`);
2023-03-20 12:45:20 -04:00
resolve(response.status == 204);
} catch (err) {
reject(err);
}
});
}
/**
* @param {Object} intRaw
*/
2023-04-07 20:12:16 -04:00
constructor(intRaw, client) {
super(client);
2023-04-25 17:34:01 -04:00
Object.defineProperty(this, 'guild', { enumerable: false });
2023-04-07 20:12:16 -04:00
2023-04-25 17:34:01 -04:00
if (!intRaw) return;
2023-03-20 12:45:20 -04:00
this.#token = intRaw["token"];
for (const k in this) {
if (intRaw[k] != undefined) {
if (k == "user") this[k] = new author(intRaw[k]);
else if (k == 'data') {
this.data = new interactionOptions(intRaw[k]);
} else {
2023-03-20 12:45:20 -04:00
if (k == 'channel_id') {
2023-04-07 20:12:16 -04:00
this.channel = new Channel(intRaw[k], this.guild, client);
2023-03-20 12:45:20 -04:00
}
this[k] = intRaw[k];
}
}
}
}
}