diff options
Diffstat (limited to 'src/api/routes/guilds/#guild_id')
20 files changed, 73 insertions, 84 deletions
diff --git a/src/api/routes/guilds/#guild_id/bans.ts b/src/api/routes/guilds/#guild_id/bans.ts index efb06fa0..b044689f 100644 --- a/src/api/routes/guilds/#guild_id/bans.ts +++ b/src/api/routes/guilds/#guild_id/bans.ts @@ -41,8 +41,8 @@ router.get( async (req: Request, res: Response) => { const { guild_id } = req.params; - let bans = await Ban.find({ where: { guild_id: guild_id } }); - let promisesToAwait: object[] = []; + const bans = await Ban.find({ where: { guild_id: guild_id } }); + const promisesToAwait: object[] = []; const bansObj: object[] = []; bans.filter((ban) => ban.user_id !== ban.executor_id); // pretend self-bans don't exist to prevent victim chasing @@ -104,14 +104,14 @@ router.put( if ( req.user_id === banned_user_id && - banned_user_id === req.permission!.cache.guild?.owner_id + banned_user_id === req.permission?.cache.guild?.owner_id ) throw new HTTPError( "You are the guild owner, hence can't ban yourself", 403, ); - if (req.permission!.cache.guild?.owner_id === banned_user_id) + if (req.permission?.cache.guild?.owner_id === banned_user_id) throw new HTTPError("You can't ban the owner", 400); const banned_user = await User.getPublicUser(banned_user_id); @@ -149,7 +149,7 @@ router.put( const banned_user = await User.getPublicUser(req.params.user_id); - if (req.permission!.cache.guild?.owner_id === req.params.user_id) + if (req.permission?.cache.guild?.owner_id === req.params.user_id) throw new HTTPError( "You are the guild owner, hence can't ban yourself", 403, @@ -186,7 +186,7 @@ router.delete( async (req: Request, res: Response) => { const { guild_id, user_id } = req.params; - let ban = await Ban.findOneOrFail({ + const ban = await Ban.findOneOrFail({ where: { guild_id: guild_id, user_id: user_id }, }); diff --git a/src/api/routes/guilds/#guild_id/channels.ts b/src/api/routes/guilds/#guild_id/channels.ts index b72f5ddb..acdb5f19 100644 --- a/src/api/routes/guilds/#guild_id/channels.ts +++ b/src/api/routes/guilds/#guild_id/channels.ts @@ -68,7 +68,7 @@ router.patch( 400, ); - const opts: any = {}; + const opts: Partial<Channel> = {}; if (x.position != null) opts.position = x.position; if (x.parent_id) { diff --git a/src/api/routes/guilds/#guild_id/delete.ts b/src/api/routes/guilds/#guild_id/delete.ts index 551c6829..9a13c9b4 100644 --- a/src/api/routes/guilds/#guild_id/delete.ts +++ b/src/api/routes/guilds/#guild_id/delete.ts @@ -16,17 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { - Channel, - emitEvent, - GuildDeleteEvent, - Guild, - Member, - Message, - Role, - Invite, - Emoji, -} from "@fosscord/util"; +import { emitEvent, GuildDeleteEvent, Guild } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; @@ -36,7 +26,7 @@ const router = Router(); // discord prefixes this route with /delete instead of using the delete method // docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild router.post("/", route({}), async (req: Request, res: Response) => { - var { guild_id } = req.params; + const { guild_id } = req.params; const guild = await Guild.findOneOrFail({ where: { id: guild_id }, diff --git a/src/api/routes/guilds/#guild_id/discovery-requirements.ts b/src/api/routes/guilds/#guild_id/discovery-requirements.ts index 11dcc33e..de2da6ee 100644 --- a/src/api/routes/guilds/#guild_id/discovery-requirements.ts +++ b/src/api/routes/guilds/#guild_id/discovery-requirements.ts @@ -16,8 +16,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { Guild, Config } from "@fosscord/util"; - import { Router, Request, Response } from "express"; import { route } from "@fosscord/api"; diff --git a/src/api/routes/guilds/#guild_id/index.ts b/src/api/routes/guilds/#guild_id/index.ts index 0df90f56..c262a088 100644 --- a/src/api/routes/guilds/#guild_id/index.ts +++ b/src/api/routes/guilds/#guild_id/index.ts @@ -47,10 +47,10 @@ router.get("/", route({}), async (req: Request, res: Response) => { 401, ); - // @ts-ignore - guild.joined_at = member?.joined_at; - - return res.send(guild); + return res.send({ + ...guild, + joined_at: member?.joined_at, + }); }); router.patch( @@ -68,7 +68,7 @@ router.patch( "MANAGE_GUILDS", ); - var guild = await Guild.findOneOrFail({ + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["emojis", "roles", "stickers"], }); @@ -110,7 +110,7 @@ router.patch( "DISCOVERABLE", ]; - for (var feature of diff) { + for (const feature of diff) { if (MUTABLE_FEATURES.includes(feature)) continue; throw FosscordApiErrors.FEATURE_IS_IMMUTABLE.withParams( diff --git a/src/api/routes/guilds/#guild_id/invites.ts b/src/api/routes/guilds/#guild_id/invites.ts index 6e9cc3e6..dd099992 100644 --- a/src/api/routes/guilds/#guild_id/invites.ts +++ b/src/api/routes/guilds/#guild_id/invites.ts @@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { getPermission, Invite, PublicInviteRelation } from "@fosscord/util"; +import { Invite, PublicInviteRelation } from "@fosscord/util"; import { route } from "@fosscord/api"; import { Request, Response, Router } from "express"; diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts index 2cf7c08b..2daa7d9b 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts @@ -49,11 +49,12 @@ router.patch( "/", route({ body: "MemberChangeSchema" }), async (req: Request, res: Response) => { - let { guild_id, member_id } = req.params; - if (member_id === "@me") member_id = req.user_id; + const { guild_id } = req.params; + const member_id = + req.params.member_id === "@me" ? req.user_id : req.params.member_id; const body = req.body as MemberChangeSchema; - let member = await Member.findOneOrFail({ + const member = await Member.findOneOrFail({ where: { id: member_id, guild_id }, relations: ["roles", "user"], }); @@ -101,7 +102,8 @@ router.put("/", route({}), async (req: Request, res: Response) => { const rights = await getRights(req.user_id); - let { guild_id, member_id } = req.params; + const { guild_id } = req.params; + let { member_id } = req.params; if (member_id === "@me") { member_id = req.user_id; rights.hasThrow("JOIN_GUILDS"); @@ -109,19 +111,19 @@ router.put("/", route({}), async (req: Request, res: Response) => { // TODO: join others by controller } - var guild = await Guild.findOneOrFail({ + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, }); - var emoji = await Emoji.find({ + const emoji = await Emoji.find({ where: { guild_id: guild_id }, }); - var roles = await Role.find({ + const roles = await Role.find({ where: { guild_id: guild_id }, }); - var stickers = await Sticker.find({ + const stickers = await Sticker.find({ where: { guild_id: guild_id }, }); diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts b/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts index 619b66f7..c93eab08 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts @@ -26,12 +26,12 @@ router.patch( "/", route({ body: "MemberNickChangeSchema" }), async (req: Request, res: Response) => { - var { guild_id, member_id } = req.params; - var permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; - if (member_id === "@me") { - member_id = req.user_id; - permissionString = "CHANGE_NICKNAME"; - } + const { guild_id } = req.params; + let permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; + const member_id = + req.params.member_id === "@me" + ? ((permissionString = "CHANGE_NICKNAME"), req.user_id) + : req.params.member_id; const perms = await getPermission(req.user_id, guild_id); perms.hasThrow(permissionString); diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts index e64893b7..16c5e789 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts @@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { getPermission, Member } from "@fosscord/util"; +import { Member } from "@fosscord/util"; import { route } from "@fosscord/api"; import { Request, Response, Router } from "express"; diff --git a/src/api/routes/guilds/#guild_id/members/index.ts b/src/api/routes/guilds/#guild_id/members/index.ts index b96210f3..51e9eb1f 100644 --- a/src/api/routes/guilds/#guild_id/members/index.ts +++ b/src/api/routes/guilds/#guild_id/members/index.ts @@ -17,7 +17,7 @@ */ import { Request, Response, Router } from "express"; -import { Guild, Member, PublicMemberProjection } from "@fosscord/util"; +import { Member, PublicMemberProjection } from "@fosscord/util"; import { route } from "@fosscord/api"; import { MoreThan } from "typeorm"; import { HTTPError } from "lambert-server"; diff --git a/src/api/routes/guilds/#guild_id/messages/search.ts b/src/api/routes/guilds/#guild_id/messages/search.ts index 7061b5f0..601167ee 100644 --- a/src/api/routes/guilds/#guild_id/messages/search.ts +++ b/src/api/routes/guilds/#guild_id/messages/search.ts @@ -16,6 +16,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/* eslint-disable @typescript-eslint/ban-ts-comment */ + import { Request, Response, Router } from "express"; import { route } from "@fosscord/api"; import { getPermission, FieldErrors, Message, Channel } from "@fosscord/util"; @@ -28,10 +30,10 @@ router.get("/", route({}), async (req: Request, res: Response) => { const { channel_id, content, - include_nsfw, // TODO + // include_nsfw, // TODO offset, sort_order, - sort_by, // TODO: Handle 'relevance' + // sort_by, // TODO: Handle 'relevance' limit, author_id, } = req.query; @@ -62,7 +64,7 @@ router.get("/", route({}), async (req: Request, res: Response) => { if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json({ messages: [], total_results: 0 }); - var query: FindManyOptions<Message> = { + const query: FindManyOptions<Message> = { order: { timestamp: sort_order ? (sort_order.toUpperCase() as "ASC" | "DESC") @@ -87,7 +89,7 @@ router.get("/", route({}), async (req: Request, res: Response) => { skip: offset ? Number(offset) : 0, }; //@ts-ignore - if (channel_id) query.where!.channel = { id: channel_id }; + if (channel_id) query.where.channel = { id: channel_id }; else { // get all channel IDs that this user can access const channels = await Channel.find({ @@ -96,7 +98,7 @@ router.get("/", route({}), async (req: Request, res: Response) => { }); const ids = []; - for (var channel of channels) { + for (const channel of channels) { const perm = await getPermission( req.user_id, req.params.guild_id, @@ -108,12 +110,12 @@ router.get("/", route({}), async (req: Request, res: Response) => { } //@ts-ignore - query.where!.channel = { id: In(ids) }; + query.where.channel = { id: In(ids) }; } //@ts-ignore - if (author_id) query.where!.author = { id: author_id }; + if (author_id) query.where.author = { id: author_id }; //@ts-ignore - if (content) query.where!.content = Like(`%${content}%`); + if (content) query.where.content = Like(`%${content}%`); const messages: Message[] = await Message.find(query); diff --git a/src/api/routes/guilds/#guild_id/profile/index.ts b/src/api/routes/guilds/#guild_id/profile/index.ts index 5771fbf1..cbf0ff6a 100644 --- a/src/api/routes/guilds/#guild_id/profile/index.ts +++ b/src/api/routes/guilds/#guild_id/profile/index.ts @@ -33,8 +33,9 @@ router.patch( "/:member_id", route({ body: "MemberChangeProfileSchema" }), async (req: Request, res: Response) => { - let { guild_id, member_id } = req.params; - if (member_id === "@me") member_id = req.user_id; + const { guild_id } = req.params; + // const member_id = + // req.params.member_id === "@me" ? req.user_id : req.params.member_id; const body = req.body as MemberChangeProfileSchema; let member = await Member.findOneOrFail({ diff --git a/src/api/routes/guilds/#guild_id/prune.ts b/src/api/routes/guilds/#guild_id/prune.ts index 1199df54..37b70f63 100644 --- a/src/api/routes/guilds/#guild_id/prune.ts +++ b/src/api/routes/guilds/#guild_id/prune.ts @@ -29,16 +29,16 @@ export const inactiveMembers = async ( days: number, roles: string[] = [], ) => { - var date = new Date(); + const date = new Date(); date.setDate(date.getDate() - days); //Snowflake should have `generateFromTime` method? Or similar? - var minId = BigInt(date.valueOf() - Snowflake.EPOCH) << BigInt(22); + const minId = BigInt(date.valueOf() - Snowflake.EPOCH) << BigInt(22); /** idea: ability to customise the cutoff variable possible candidates: public read receipt, last presence, last VC leave **/ - var members = await Member.find({ + let members = await Member.find({ where: [ { guild_id, @@ -83,7 +83,7 @@ export const inactiveMembers = async ( router.get("/", route({}), async (req: Request, res: Response) => { const days = parseInt(req.query.days as string); - var roles = req.query.include_roles; + let roles = req.query.include_roles; if (typeof roles === "string") roles = [roles]; //express will return array otherwise const members = await inactiveMembers( @@ -102,7 +102,7 @@ router.post( async (req: Request, res: Response) => { const days = parseInt(req.body.days); - var roles = req.query.include_roles; + let roles = req.query.include_roles; if (typeof roles === "string") roles = [roles]; const { guild_id } = req.params; diff --git a/src/api/routes/guilds/#guild_id/regions.ts b/src/api/routes/guilds/#guild_id/regions.ts index a9c04a39..61ba00bf 100644 --- a/src/api/routes/guilds/#guild_id/regions.ts +++ b/src/api/routes/guilds/#guild_id/regions.ts @@ -16,10 +16,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { Config, Guild, Member } from "@fosscord/util"; +import { Guild } from "@fosscord/util"; import { Request, Response, Router } from "express"; -import { getVoiceRegions, route } from "@fosscord/api"; -import { getIpAdress } from "@fosscord/api"; +import { getVoiceRegions, route, getIpAdress } from "@fosscord/api"; const router = Router(); diff --git a/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts b/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts index 22eb439a..48e77897 100644 --- a/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts +++ b/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts @@ -87,7 +87,8 @@ router.patch( role.assign({ ...body, permissions: String( - req.permission!.bitfield & BigInt(body.permissions || "0"), + (req.permission?.bitfield || 0n) & + BigInt(body.permissions || "0"), ), }); diff --git a/src/api/routes/guilds/#guild_id/roles/index.ts b/src/api/routes/guilds/#guild_id/roles/index.ts index feab84ef..54d4b12c 100644 --- a/src/api/routes/guilds/#guild_id/roles/index.ts +++ b/src/api/routes/guilds/#guild_id/roles/index.ts @@ -68,7 +68,8 @@ router.post( guild_id: guild_id, managed: false, permissions: String( - req.permission!.bitfield & BigInt(body.permissions || "0"), + (req.permission?.bitfield || 0n) & + BigInt(body.permissions || "0"), ), tags: undefined, icon: undefined, diff --git a/src/api/routes/guilds/#guild_id/templates.ts b/src/api/routes/guilds/#guild_id/templates.ts index f5244313..284bbccf 100644 --- a/src/api/routes/guilds/#guild_id/templates.ts +++ b/src/api/routes/guilds/#guild_id/templates.ts @@ -44,7 +44,7 @@ const TemplateGuildProjection: (keyof Guild)[] = [ router.get("/", route({}), async (req: Request, res: Response) => { const { guild_id } = req.params; - var templates = await Template.find({ + const templates = await Template.find({ where: { source_guild_id: guild_id }, }); @@ -60,9 +60,9 @@ router.post( where: { id: guild_id }, select: TemplateGuildProjection, }); - const exists = await Template.findOneOrFail({ + const exists = await Template.findOne({ where: { id: guild_id }, - }).catch((e) => {}); + }); if (exists) throw new HTTPError("Template already exists", 400); const template = await Template.create({ diff --git a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts index 9883ef0d..3577df17 100644 --- a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts +++ b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts @@ -37,8 +37,9 @@ router.patch( route({ body: "VoiceStateUpdateSchema" }), async (req: Request, res: Response) => { const body = req.body as VoiceStateUpdateSchema; - var { guild_id, user_id } = req.params; - if (user_id === "@me") user_id = req.user_id; + const { guild_id } = req.params; + const user_id = + req.params.user_id === "@me" ? req.user_id : req.params.user_id; const perms = await getPermission( req.user_id, diff --git a/src/api/routes/guilds/#guild_id/widget.json.ts b/src/api/routes/guilds/#guild_id/widget.json.ts index 46b8aa8b..9319d058 100644 --- a/src/api/routes/guilds/#guild_id/widget.json.ts +++ b/src/api/routes/guilds/#guild_id/widget.json.ts @@ -17,14 +17,7 @@ */ import { Request, Response, Router } from "express"; -import { - Config, - Permissions, - Guild, - Invite, - Channel, - Member, -} from "@fosscord/util"; +import { Permissions, Guild, Invite, Channel, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { random, route } from "@fosscord/api"; @@ -46,7 +39,7 @@ router.get("/", route({}), async (req: Request, res: Response) => { if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404); // Fetch existing widget invite for widget channel - var invite = await Invite.findOne({ + let invite = await Invite.findOne({ where: { channel_id: guild.widget_channel_id }, }); @@ -70,7 +63,7 @@ router.get("/", route({}), async (req: Request, res: Response) => { } // Fetch voice channels, and the @everyone permissions object - const channels = [] as any[]; + const channels: { id: string; name: string; position: number }[] = []; ( await Channel.find({ @@ -88,15 +81,15 @@ router.get("/", route({}), async (req: Request, res: Response) => { ) { channels.push({ id: doc.id, - name: doc.name, - position: doc.position, + name: doc.name ?? "Unknown channel", + position: doc.position ?? 0, }); } }); // Fetch members // TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file) - let members = await Member.find({ where: { guild_id: guild_id } }); + const members = await Member.find({ where: { guild_id: guild_id } }); // Construct object to respond with const data = { diff --git a/src/api/routes/guilds/#guild_id/widget.png.ts b/src/api/routes/guilds/#guild_id/widget.png.ts index 1c6a62ac..65de8978 100644 --- a/src/api/routes/guilds/#guild_id/widget.png.ts +++ b/src/api/routes/guilds/#guild_id/widget.png.ts @@ -16,6 +16,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + import { Request, Response, Router } from "express"; import { Guild } from "@fosscord/util"; import { HTTPError } from "lambert-server"; @@ -161,8 +163,7 @@ async function drawIcon( scale: number, icon: string, ) { - // @ts-ignore - const img = new require("canvas").Image(); + const img = new (require("canvas").Image)(); img.src = icon; // Do some canvas clipping magic! |