chore: deno fmt

This commit is contained in:
Skillz4Killz
2021-04-04 00:34:40 +00:00
committed by GitHub
parent e7d9b58c81
commit b81bf8d484
24 changed files with 140 additions and 155 deletions
+4 -5
View File
@@ -9,11 +9,10 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/
export async function getChannel(channelId: string, addToCache = true) {
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_BASE(channelId),
)) as DiscordChannel;
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_BASE(channelId),
)) as DiscordChannel;
const channelStruct = await structures.createChannelStruct(
result,
+4 -5
View File
@@ -9,11 +9,10 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/
export async function getChannels(guildId: string, addToCache = true) {
const result =
(await rest.runMethod(
"get",
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
const result = (await rest.runMethod(
"get",
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
return Promise.all(result.map(async (res) => {
const channelStruct = await structures.createChannelStruct(res, guildId);
+4 -5
View File
@@ -5,11 +5,10 @@ import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */
export async function getPins(channelId: string) {
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_PINS(channelId),
)) as DiscordMessage[];
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_PINS(channelId),
)) as DiscordMessage[];
return Promise.all(
result.map((res) => structures.createMessageStruct(res)),
+6 -7
View File
@@ -5,13 +5,12 @@ import { endpoints } from "../../util/constants.ts";
/** Fetch all of the global commands for your application. */
export async function getSlashCommands(guildId?: string) {
const result =
(await rest.runMethod(
"get",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
)) as SlashCommand[];
const result = (await rest.runMethod(
"get",
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
)) as SlashCommand[];
return new Collection(result.map((command) => [command.name, command]));
}
+5 -6
View File
@@ -4,12 +4,11 @@ import { endpoints } from "../../util/constants.ts";
/** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */
export async function createGuild(options: CreateServerOptions) {
const guild =
(await rest.runMethod(
"post",
endpoints.GUILDS,
options,
)) as CreateGuildPayload;
const guild = (await rest.runMethod(
"post",
endpoints.GUILDS,
options,
)) as CreateGuildPayload;
return structures.createGuildStruct(guild, 0);
}
+2 -1
View File
@@ -18,7 +18,8 @@ export async function getAuditLogs(
? AuditLogs[options.action_type]
: undefined,
limit: options.limit && options.limit >= 1 && options.limit <= 100
? options.limit : 50,
? options.limit
: 50,
},
);
+4 -5
View File
@@ -7,11 +7,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getBans(guildId: string) {
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
const results =
(await rest.runMethod(
"get",
endpoints.GUILD_BANS(guildId),
)) as BannedUser[];
const results = (await rest.runMethod(
"get",
endpoints.GUILD_BANS(guildId),
)) as BannedUser[];
return new Collection<string, BannedUser>(
results.map((res) => [res.user.id, res]),
+5 -3
View File
@@ -15,9 +15,11 @@ export async function getMember(
const guild = await cacheHandlers.get("guilds", guildId);
if (!guild && !options?.force) return;
const data = (await rest.runMethod("get",
endpoints.GUILD_MEMBER(guildId, id),
)) as MemberCreatePayload;
const data =
(await rest.runMethod(
"get",
endpoints.GUILD_MEMBER(guildId, id),
)) as MemberCreatePayload;
const memberStruct = await structures.createMemberStruct(data, guildId);
await cacheHandlers.set("members", memberStruct.id, memberStruct);
+6 -13
View File
@@ -42,19 +42,12 @@ export async function getMembers(guildId: string, options?: GetMemberOptions) {
);
}
const result =
(await rest.runMethod(
"get",
`${endpoints.GUILD_MEMBERS(guildId)}?limit=${
membersLeft > 1000
? 1000
: membersLeft
}${
options?.after
? `&after=${options.after}`
: ""
}`,
)) as DiscordGuildMember[];
const result = (await rest.runMethod(
"get",
`${endpoints.GUILD_MEMBERS(guildId)}?limit=${
membersLeft > 1000 ? 1000 : membersLeft
}${options?.after ? `&after=${options.after}` : ""}`,
)) as DiscordGuildMember[];
const memberStructures = await Promise.all(
result.map(async (member) => {
+4 -5
View File
@@ -10,11 +10,10 @@ export async function getMessage(channelId: string, id: string) {
"READ_MESSAGE_HISTORY",
]);
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE(channelId, id),
)) as MessageCreateOptions;
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE(channelId, id),
)) as MessageCreateOptions;
return structures.createMessageStruct(result);
}
+5 -6
View File
@@ -19,12 +19,11 @@ export async function getMessages(
if (options?.limit && options.limit > 100) return;
const result =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGES(channelId),
options,
)) as MessageCreateOptions[];
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGES(channelId),
options,
)) as MessageCreateOptions[];
return Promise.all(
result.map((res) => structures.createMessageStruct(res)),
+5 -6
View File
@@ -9,12 +9,11 @@ export async function getReactions(
reaction: string,
options?: DiscordGetReactionsParams,
) {
const users =
(await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
options,
)) as UserPayload[];
const users = (await rest.runMethod(
"get",
endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction),
options,
)) as UserPayload[];
return new Collection(users.map((user) => [user.id, user]));
}
+4 -5
View File
@@ -4,11 +4,10 @@ import { endpoints } from "../../util/constants.ts";
/** Crosspost a message in a News Channel to following channels. */
export async function publishMessage(channelId: string, messageId: string) {
const data =
(await rest.runMethod(
"post",
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId),
)) as MessageCreateOptions;
const data = (await rest.runMethod(
"post",
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId),
)) as MessageCreateOptions;
return structures.createMessageStruct(data);
}
@@ -23,12 +23,11 @@ export async function createGuildTemplate(
throw new Error("The description can only be in between 0-120 characters.");
}
const template =
(await rest.runMethod(
"post",
endpoints.GUILD_TEMPLATES(guildId),
data,
)) as GuildTemplate;
const template = (await rest.runMethod(
"post",
endpoints.GUILD_TEMPLATES(guildId),
data,
)) as GuildTemplate;
return structures.createTemplateStruct(template);
}
@@ -13,11 +13,10 @@ export async function deleteGuildTemplate(
) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const deletedTemplate =
(await rest.runMethod(
"delete",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
)) as GuildTemplate;
const deletedTemplate = (await rest.runMethod(
"delete",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
)) as GuildTemplate;
return structures.createTemplateStruct(deletedTemplate);
}
+5 -6
View File
@@ -22,12 +22,11 @@ export async function editGuildTemplate(
throw new Error("The description can only be in between 0-120 characters.");
}
const template =
(await rest.runMethod(
"patch",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
data,
)) as GuildTemplate;
const template = (await rest.runMethod(
"patch",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
data,
)) as GuildTemplate;
return structures.createTemplateStruct(template);
}
+4 -5
View File
@@ -10,11 +10,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getGuildTemplates(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const templates =
(await rest.runMethod(
"get",
endpoints.GUILD_TEMPLATES(guildId),
)) as GuildTemplate[];
const templates = (await rest.runMethod(
"get",
endpoints.GUILD_TEMPLATES(guildId),
)) as GuildTemplate[];
return templates.map((template) => structures.createTemplateStruct(template));
}
+4 -5
View File
@@ -4,11 +4,10 @@ import { endpoints } from "../../util/constants.ts";
/** Returns the guild template if it exists */
export async function getTemplate(templateCode: string) {
const result =
(await rest.runMethod(
"get",
endpoints.GUILD_TEMPLATE(templateCode),
) as GuildTemplate);
const result = (await rest.runMethod(
"get",
endpoints.GUILD_TEMPLATE(templateCode),
) as GuildTemplate);
const template = await structures.createTemplateStruct(result);
return template;
+4 -5
View File
@@ -10,11 +10,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function syncGuildTemplate(guildId: string, templateCode: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const template =
(await rest.runMethod(
"put",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
)) as GuildTemplate;
const template = (await rest.runMethod(
"put",
`${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`,
)) as GuildTemplate;
return structures.createTemplateStruct(template);
}
+5 -1
View File
@@ -90,7 +90,11 @@ async function handleApplicationCommand(
}
/** Internal function to verify security. Discord will send bad and good data and this function is important to verify it. If it is not verified properly, Discord will kill your bot. */
export function verifySecurity(buffer: Uint8Array, signature: string, time: string) {
export function verifySecurity(
buffer: Uint8Array,
signature: string,
time: string,
) {
const sig = new Uint8Array(64);
const timestamp = new TextEncoder().encode(time);
+1 -1
View File
@@ -22,7 +22,7 @@ export const rest = {
console.error(error);
},
// PLACEHOLDERS TO ALLOW USERS TO CUSTOMIZE
debug: function(_type, error) {},
debug: function (_type, error) {},
fetching() {},
fetched() {},
fetchSuccess() {},
+16 -15
View File
@@ -9,7 +9,7 @@ export function runMethod(
url: string,
body?: unknown,
retryCount = 0,
bucketId?: string | null
bucketId?: string | null,
) {
rest.eventHandlers.debug?.("requestCreate", {
method,
@@ -55,18 +55,19 @@ export function runMethod(
return { rateLimited: rateLimitResetIn, beforeFetch: true, bucketId };
}
const query =
method === "get" && body
? // deno-lint-ignore no-explicit-any
Object.entries(body as any)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
value as string | number | boolean
)}`
)
.join("&")
: "";
const query = method === "get" && body
? // deno-lint-ignore no-explicit-any
Object.entries(body as any)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${
encodeURIComponent(
value as string | number | boolean,
)
}`,
)
.join("&")
: "";
const urlToUse = method === "get" && query ? `${url}?${query}` : url;
rest.eventHandlers.debug?.("requestFetch", {
@@ -78,7 +79,7 @@ export function runMethod(
});
const response = await fetch(
urlToUse,
rest.createRequestBody(body, method)
rest.createRequestBody(body, method),
);
rest.eventHandlers.debug?.("requestFetched", {
method,
@@ -90,7 +91,7 @@ export function runMethod(
});
const bucketIdFromHeaders = rest.processRequestHeaders(
url,
response.headers
response.headers,
);
await rest.handleStatusCode(response, errorStack);
-1
View File
@@ -1,4 +1,3 @@
export interface ListGuildMembers {
/** Max number of members to return (1-1000). Default: 1 */
limit?: number;
+34 -33
View File
@@ -8,24 +8,23 @@ import { PermissionStrings } from "../types/permissions/permission_strings.ts";
async function getCached(table: "guilds", key: string | Guild): Promise<Guild>;
async function getCached(
table: "channels",
key: string | Channel
key: string | Channel,
): Promise<Channel>;
async function getCached(
table: "members",
key: string | Member
key: string | Member,
): Promise<Member>;
async function getCached(
table: "guilds" | "channels" | "members",
key: string | Guild | Channel | Member
key: string | Guild | Channel | Member,
) {
const cached =
typeof key === "string"
? // @ts-ignore TS is wrong here
await cacheHandlers.get(table, key)
: key;
const cached = typeof key === "string"
? // @ts-ignore TS is wrong here
await cacheHandlers.get(table, key)
: key;
if (!cached || typeof cached === "string") {
throw new Error(
Errors[`${table.slice(0, -1).toUpperCase()}_NOT_FOUND` as Errors]
Errors[`${table.slice(0, -1).toUpperCase()}_NOT_FOUND` as Errors],
);
}
@@ -35,7 +34,7 @@ async function getCached(
/** Calculates the permissions this member has in the given guild */
export async function calculateBasePermissions(
guild: string | Guild,
member: string | Member
member: string | Member,
) {
guild = await getCached("guilds", guild);
member = await getCached("members", member);
@@ -60,7 +59,7 @@ export async function calculateBasePermissions(
/** Calculates the permissions this member has for the given Channel */
export async function calculateChannelOverwrites(
channel: string | Channel,
member: string | Member
member: string | Member,
) {
channel = await getCached("channels", channel);
@@ -71,12 +70,12 @@ export async function calculateChannelOverwrites(
// Get all the role permissions this member already has
let permissions = BigInt(
await calculateBasePermissions(channel.guildId, member)
await calculateBasePermissions(channel.guildId, member),
);
// First calculate @everyone overwrites since these have the lowest priority
const overwriteEveryone = channel?.permissionOverwrites.find(
(overwrite) => overwrite.id === (channel as Channel).guildId
(overwrite) => overwrite.id === (channel as Channel).guildId,
);
if (overwriteEveryone) {
// First remove denied permissions since denied < allowed
@@ -103,7 +102,7 @@ export async function calculateChannelOverwrites(
// Third calculate member specific overwrites since these have the highest priority
const overwriteMember = overwrites.find(
(overwrite) => overwrite.id === (member as Member).id
(overwrite) => overwrite.id === (member as Member).id,
);
if (overwriteMember) {
permissions &= ~BigInt(overwriteMember.deny);
@@ -116,14 +115,15 @@ export async function calculateChannelOverwrites(
/** Checks if the given permission bits are matching the given permissions. `ADMINISTRATOR` always returns `true` */
export function validatePermissions(
permissionBits: string,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
if (BigInt(permissionBits) & 8n) return true;
return permissions.every(
(permission) =>
// Check if permission is in permissionBits
BigInt(permissionBits) & BigInt(DiscordBitwisePermissionFlags[permission])
BigInt(permissionBits) &
BigInt(DiscordBitwisePermissionFlags[permission]),
);
}
@@ -131,7 +131,7 @@ export function validatePermissions(
export async function hasGuildPermissions(
guild: string | Guild,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// First we need the role permission bits this member has
const basePermissions = await calculateBasePermissions(guild, member);
@@ -142,7 +142,7 @@ export async function hasGuildPermissions(
/** Checks if the bot has these permissions in the given guild */
export function botHasGuildPermissions(
guild: string | Guild,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// Since Bot is a normal member we can use the hasRolePermissions() function
return hasGuildPermissions(guild, botId, permissions);
@@ -152,7 +152,7 @@ export function botHasGuildPermissions(
export async function hasChannelPermissions(
channel: string | Channel,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// First we need the overwrite bits this member has
const channelOverwrites = await calculateChannelOverwrites(channel, member);
@@ -163,7 +163,7 @@ export async function hasChannelPermissions(
/** Checks if the bot has these permissions f0r the given channel */
export function botHasChannelPermissions(
channel: string | Channel,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// Since Bot is a normal member we can use the hasRolePermissions() function
return hasChannelPermissions(channel, botId, permissions);
@@ -172,7 +172,7 @@ export function botHasChannelPermissions(
/** Returns the permissions that are not in the given permissionBits */
export function missingPermissions(
permissionBits: string,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
if (BigInt(permissionBits) & 8n) return [];
@@ -181,7 +181,7 @@ export function missingPermissions(
!(
BigInt(permissionBits) &
BigInt(DiscordBitwisePermissionFlags[permission])
)
),
);
}
@@ -189,7 +189,7 @@ export function missingPermissions(
export async function getMissingGuildPermissions(
guild: string | Guild,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// First we need the role permissino bits this member has
const permissionBits = await calculateBasePermissions(guild, member);
@@ -201,7 +201,7 @@ export async function getMissingGuildPermissions(
export async function getMissingChannelPermissions(
channel: string | Channel,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// First we need the role permissino bits this member has
const permissionBits = await calculateChannelOverwrites(channel, member);
@@ -213,7 +213,7 @@ export async function getMissingChannelPermissions(
export async function requireGuildPermissions(
guild: string | Guild,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
const missing = await getMissingGuildPermissions(guild, member, permissions);
if (missing.length) {
@@ -225,7 +225,7 @@ export async function requireGuildPermissions(
/** Throws an error if the bot does not have all permissions */
export function requireBotGuildPermissions(
guild: string | Guild,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// Since Bot is a normal member we can use the throwOnMissingGuildPermission() function
return requireGuildPermissions(guild, botId, permissions);
@@ -235,12 +235,12 @@ export function requireBotGuildPermissions(
export async function requireChannelPermissions(
channel: string | Channel,
member: string | Member,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
const missing = await getMissingChannelPermissions(
channel,
member,
permissions
permissions,
);
if (missing.length) {
// If the member is missing a permission throw an Error
@@ -251,7 +251,7 @@ export async function requireChannelPermissions(
/** Throws an error if the bot has not all of the given channel permissions */
export function requireBotChannelPermissions(
channel: string | Channel,
permissions: PermissionStrings[]
permissions: PermissionStrings[],
) {
// Since Bot is a normal member we can use the throwOnMissingChannelPermission() function
return requireChannelPermissions(channel, botId, permissions);
@@ -263,7 +263,8 @@ export function calculatePermissions(permissionBits: bigint) {
// Since Object.keys() not only returns the permission names but also the bit values we need to return false if it is a Number
if (Number(permission)) return false;
// Check if permissionBits has this permission
return permissionBits & BigInt(DiscordBitwisePermissionFlags[permission as PermissionStrings]);
return permissionBits &
BigInt(DiscordBitwisePermissionFlags[permission as PermissionStrings]);
}) as PermissionStrings[];
}
@@ -280,7 +281,7 @@ export function calculateBits(permissions: PermissionStrings[]) {
/** Gets the highest role from the member in this guild */
export async function highestRole(
guild: string | Guild,
member: string | Member
member: string | Member,
) {
guild = await getCached("guilds", guild);
@@ -316,7 +317,7 @@ export async function highestRole(
export async function higherRolePosition(
guild: string | Guild,
roleId: string,
otherRoleId: string
otherRoleId: string,
) {
guild = await getCached("guilds", guild);
@@ -336,7 +337,7 @@ export async function higherRolePosition(
export async function isHigherPosition(
guild: string | Guild,
memberId: string,
compareRoleId: string
compareRoleId: string,
) {
guild = await getCached("guilds", guild);