summary refs log tree commit diff
path: root/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts
blob: 797d348ee4635f64f76d4c9d4567b99cb1633e0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { route } from "@fosscord/api";
import {
	Channel,
	ChannelType,
	DiscordApiErrors,
	emitEvent,
	getPermission,
	OrmUtils,
	VoiceState,
	VoiceStateUpdateEvent,
	VoiceStateUpdateSchema
} from "@fosscord/util";
import { Request, Response, Router } from "express";

const router = Router();
router.patch("/", route({ body: "VoiceStateUpdateSchema" }), async (req: Request, res: Response) => {
	const body = req.body as VoiceStateUpdateSchema;
	let { guild_id, user_id } = req.params;
	if (user_id === "@me") user_id = req.user_id;

	const perms = await getPermission(req.user_id, guild_id, body.channel_id);

	/*
	From https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state
	You must have the MUTE_MEMBERS permission to unsuppress others. You can always suppress yourself.
	You must have the REQUEST_TO_SPEAK permission to request to speak. You can always clear your own request to speak.
	 */
	if (body.suppress && user_id !== req.user_id) {
		perms.hasThrow("MUTE_MEMBERS");
	}
	if (!body.suppress) body.request_to_speak_timestamp = new Date();
	if (body.request_to_speak_timestamp) perms.hasThrow("REQUEST_TO_SPEAK");

	let voice_state = await VoiceState.findOne({
		where: {
			guild_id,
			channel_id: body.channel_id,
			user_id
		}
	});
	if (!voice_state) throw DiscordApiErrors.UNKNOWN_VOICE_STATE;

	voice_state = OrmUtils.mergeDeep(voice_state, body) as VoiceState;
	const channel = await Channel.findOneOrFail({ where: { guild_id, id: body.channel_id } });
	if (channel.type !== ChannelType.GUILD_STAGE_VOICE) {
		throw DiscordApiErrors.CANNOT_EXECUTE_ON_THIS_CHANNEL_TYPE;
	}

	await Promise.all([
		voice_state.save(),
		emitEvent({
			event: "VOICE_STATE_UPDATE",
			data: voice_state,
			guild_id
		} as VoiceStateUpdateEvent)
	]);
	return res.sendStatus(204);
});

export default router;