Skip to content

Commit

Permalink
feat: refactor v9api, update types and endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkaroid committed May 14, 2022
1 parent 96c3e30 commit f883ace
Showing 1 changed file with 343 additions and 0 deletions.
343 changes: 343 additions & 0 deletions index.ts
@@ -0,0 +1,343 @@
import { get, post, put, loop, remove, reply, create } from "./src/utils";
import c from "./src/const";

class Jalter {

private base: string
private auth: object

static default: typeof Jalter;

constructor(token: string) {
if (!token) throw new Error(c.error.missingToken);
this.base = c.endpoint.baseurl;
this.auth = { "authorization": token };

get(`${this.base}/users/@me`, this.auth).then(res => {
if (res.statusCode !== 200) throw new Error(c.error.invalidToken);
});
}

/**
* Returns the user object of the requester's account.
* @returns the current user object
* @example
* ```js
* jalter.getMe().then((res) => { console.log("getMe", res); });
* ```
* https://discord.com/developers/docs/resources/user#get-current-user
*/
async getMe(): Promise<object> {
const endpoint = `${this.base}/users/@me`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Returns a user object for a given user ID.
* @param {string} userId the user id
* @returns the user object
* @example
* ```js
* jalter.getUser("317255788324454400").then((res) => { console.log("getUser", res); });
* ```
* https://discord.com/developers/docs/resources/user#get-user
*/
async getUser(userId: string): Promise<object> {
const endpoint = `${this.base}/users/${userId}`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Post a message to a guild text or DM channel. Returns a message object
* @param {string} channelId the channel id
* @param {string} text the message text
* @returns the message object send
* @example
* ```js
* jalter.sendMessage("974918359500075041", "hey").then((res) => { console.log("sendMessage", res); });
* ```
* https://discord.com/developers/docs/resources/channel#create-message
*/
async sendMessage(channelId: string, text: string): Promise<object> {
const endpoint = `${this.base}/channels/${channelId}/messages`;
return await post(endpoint, text, this.auth).then(res => {
return res.body as object;
});
}


/**
* Returns the messages for a channel.
* @param {string} channelId
* @param {number} limit
* @return the messages that were received
* @example
* ```js
* jalter.getMessages("974918359500075041", 10).then((res) => { console.log("getMessages", res); });
* ```
* https://discord.com/developers/docs/resources/channel#get-channel-messages
*/
async getMessage(channelId: string, limit: number): Promise<object> {
const endpoint = `${this.base}/channels/${channelId}/messages?limit=${limit}`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Returns an audit log object for the guild
* @param {string} guildId the guild id
* return the audit logs from related guild
* @example
* ```js
* jalter.getAuditLogs("974918359500075041").then((res) => { console.log("getAuditLogs", res); });
* ```
* https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log
*/
async getAuditLogs(guildId: string): Promise<object> {
const endpoint = `${this.base}/guilds/${guildId}/audit-logs`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Returns a list of role objects for the guild.
* @param {string} guildId the guild id
* @returns the list of roles from related guild
* @example
* ```js
* jalter.getRoles("974918359500075041").then((res) => { console.log("getRoles", res); });
* ```
* https://discord.com/developers/docs/resources/guild#get-guild-roles
*/
async getRoles(guildId: string): Promise<object> {
const endpoint = `${this.base}/guilds/${guildId}/roles`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Kick a member from a guild
* @param {string} guildId the guild id
* @param {string} userId the user id
* @returns the member object that was removed
* @example
* ```js
* jalter.kickUser("974918359500075041", "317255788324454400").then((res) => { console.log("kickUser", res); });
* ```
* https://discord.com/developers/docs/resources/guild#remove-guild-member
*/
async kickUser(guildId: string, userId: string): Promise<object> {
const endpoint = `${this.base}/guilds/${guildId}/members/${userId}`;
return await remove(endpoint, this.auth).then(res => {
return res.body as object;
});
}


/**
* Ban a member from a guild
* @param {string} guildId the guild id
* @param {string} userId the user id
* @param {string} reason the reason for banning
* @returns the user object that was banned
* @example
* ```js
* jalter.banUser("974918359500075041", "974918359500075041", "I don't like you").then((res) => { console.log("banUser", res); });
* ```
* https://discord.com/developers/docs/resources/guild#remove-guild-member
*/
async banUser(guildId: string, userId: string, reason = ""): Promise<object> {
const endpoint = `${this.base}/guilds/${guildId}/bans/${userId}`;
return await put(endpoint, reason, this.auth).then(res => {
return res.body as object;
});
}

/**
* Unban a member from a guild
* @param {string} guildId the guild id
* @param {string} userId the user id
* @returns the user object that was unbanned
* @example
* ```js
* jalter.unBanUser("974918359500075041", "974918359500075041").then((res) => { console.log("unBanUser", res); });
* ```
* https://discord.com/developers/docs/resources/guild#remove-guild-member
*/
async unBanUser(guildId: string, userId: string): Promise<void> {
const endpoint = `${this.base}/guilds/${guildId}/bans/${userId}`;
await remove(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Join a guild
* @param {string} inviteUrl the code invites
* @returns the guild object that was joined
* @example
* ```js
* jalter.joinGuild("ApHu666E").then((res) => { console.log("joinGuild", res); });
* ```
* https://discord.com/developers/docs/resources/invite#get-invite
*/
async joinGuild(inviteUrl: string): Promise<object> {
if (inviteUrl.startsWith("https")) throw new Error(c.error.notValidCode);
const endpoint = `${this.base}/invites/${inviteUrl}`;
return await post(endpoint, "", this.auth).then(res => {
return res.body as object;
});
}

/**
* Leave a guild. Returns a 204 empty response on success.
* @param {string} guildId the guild id
* @returns the guild object that was left or null
* @example
* ```js
* jalter.leaveGuild("974918359500075041").then((res) => { console.log("leaveGuild", res); });
* ```
* https://discord.com/developers/docs/resources/user#leave-guild
*/
async leaveGuild(guildId: string): Promise<object> {
const endpoint = `${this.base}/users/@me/guilds/${guildId}`;
return await remove(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Reply to a message
* @param {string} channelId the channel id
* @param {string} messageId the message id
* @param {string} text the desired reply text
* @returns the message object that was replied
* @example
* ```js
* jalter.replyMessage("974918359500075041", "974963134144069662", "uh?").then((res) => { console.log("replyMessage", res); });
* ```
* https://discord.com/developers/docs/resources/channel#create-message
*/
async replyMessage(channelId: string, messageId: string, text: string): Promise<object> {
const endpoint = `${this.base}/channels/${channelId}/messages`;
return await reply(endpoint, messageId, text, this.auth).then(res => {
return res.body as object;
});
}

/**
* Delete a message
* @param {string} channelId the channel id
* @param {string} messageId the message id
* @returns the message object that was deleted or null
* @example
* ```js
* jalter.deleteMessage("974918359500075041", "974963134144069662").then((res) => { console.log("deleteMessage", res); });
* ```
* https://discord.com/developers/docs/resources/channel#get-channel-message
*/
async deleteMessage(channelId: string, messageId: string): Promise<object> {
const endpoint = `${this.base}/channels/${channelId}/messages/${messageId}`;
return await remove(endpoint, this.auth).then(res => {
return res.body as object;
});
}

/**
* Create a new channel object for the guild
* @param {string} guildId the guild id
* @param {string} name the desired name
* @param {string} type the channel TYPE
* @returns the channel object that was created
* @example
* ```js
* jalter.createChannel("974918359500075041", "general", 0).then((res) => { console.log("createChannel", res); });
* ```
* https://discord.com/developers/docs/resources/channel#create-guild-channel
*/
async createChannel(guildId: string, name: string, type: number): Promise<object> {
if (!c.channelType.includes(type)) throw Error("Invalid channel type, please use one of the following: " + c.channelType.join(", "));
const endpoint = `${this.base}/guilds/${guildId}/channels`;
return await create(endpoint, name, type, this.auth).then(res => {
return res.body as object;
});
}

/**
* Returns a list of guild channel objects. Does not include threads.
* @param {string} guildId the guild id
* @returns the list of channels from related guild
* @example
* ```js
* jalter.getChannels("974918359500075041").then((res) => { console.log("getChannels", res); });
* ```
* https://discord.com/developers/docs/resources/guild#get-guild-channels
*/
async getChannels(guildId: string): Promise<object> {
const endpoint = `${this.base}/guilds/${guildId}/channels`;
return await get(endpoint, this.auth).then(res => {
return res.body as object;
});
}


/**
* For farming stuff, fill the array with your custom message, just dont greedy
* @param {number} channelId The destination channel ID
* @param {number} interval The interval time per ms
* @param {string[]} textList The array of messages
* @returns the message object send
* @example
* send random hey, ho, or let's go
* ```js
* jalter.sendInterval("974918359500075041", 2000, ["hey", "ho", "let's go"]).then((res) => { console.log("sendInterval", res); });
* ```
*
* @example
* will send a random hello world in different 72 languages if array is not specified
* ```js
* jalter.sendInterval("974918359500075041", 2000).then((res) => { console.log("sendInterval", res); });
* ```
* Non standard stuff, there is no external links except [channel#create-message](https://discord.com/developers/docs/resources/channel#create-message)
*/
async sendInterval(channelId: string, interval: number, textList: string[] = c.endpoint.hello): Promise<number> {
if (!Array.isArray(textList)) throw new Error(c.error.notArray);
if (interval < 1000) throw new Error(c.error.invalidDelay);

return loop(async () => {
this.sendMessage(channelId, textList[Math.floor(Math.random() * textList.length)]).then(console.log);
}, interval);
}

/**
* Send a message on the channel and will stop once the count is reached
* @param {string} channelId the channel id
* @param {string} text the message text
* @param {number} interval the interval delay time
* @param {number} count the number of messages to send
* @returns the message object send
* @example
* send a message with a delay of 2 seconds and will stop after reaching 5 messages
* ```js
* jalter.sendCount("974918359500075041", 2000, "hey", 5).then((res) => { console.log("sendCount", res); });
* ```
* Non standard stuff, there is no external links except [channel#create-message](https://discord.com/developers/docs/resources/channel#create-message)
*/
async sendCount(channelId: string, interval: number, text: string, count: number): Promise<void> {
if (interval < 1000) throw new Error(c.error.invalidDelay);
for (let i = 0; i < count; i++) {
return this.sendMessage(channelId, text).then(console.log);
}
}

}

Jalter.default = Jalter;
export = Jalter;

0 comments on commit f883ace

Please sign in to comment.