seyfert/src/structures/VoiceState.ts
Marcos Susaña d6ff9767a1
feat: http voice states (#235)
* feat: http voice states

* fix: ups

* fix: typo

* fix: structure
2024-08-07 20:26:47 -04:00

82 lines
2.6 KiB
TypeScript

import type { UsingClient } from '../';
import type { VoiceStateResource } from '../cache/resources/voice-states';
import { type GuildMemberStructure, Transformers } from '../client/transformers';
import type { ObjectToLower } from '../common';
import type { APIVoiceState } from '../types';
import { Base } from './extra/Base';
export interface VoiceState extends Base, ObjectToLower<Omit<VoiceStateResource, 'member'>> {}
export class VoiceState extends Base {
protected withMember?: GuildMemberStructure;
constructor(client: UsingClient, data: APIVoiceState) {
super(client);
const { member, ...rest } = data;
this.__patchThis(rest);
if (member?.user && data.guild_id)
this.withMember = Transformers.GuildMember(client, member, member.user, data.guild_id);
}
get isMuted() {
return this.mute || this.selfMute;
}
async member(force?: boolean) {
return (this.withMember = await this.client.members.fetch(this.guildId, this.userId, force));
}
user(force?: boolean) {
return this.client.users.fetch(this.userId, force);
}
async channel(force?: boolean) {
if (!this.channelId) return;
return this.client.channels.fetch(this.channelId, force);
}
async setMute(mute = !this.mute, reason?: string) {
const member = await this.client.members.edit(this.guildId, this.userId, { mute }, reason);
this.mute = mute;
return member;
}
async setDeaf(deaf = !this.deaf, reason?: string) {
const member = await this.client.members.edit(this.guildId, this.userId, { deaf }, reason);
this.deaf = deaf;
return member;
}
async setSuppress(suppress = !this.suppress) {
await this.client.proxy.guilds(this.guildId)['voice-states']['@me'].patch({
body: { suppress },
});
this.suppress = suppress;
}
async requestSpeak(date: string | Date = new Date()) {
if (typeof date === 'string') date = new Date(date);
if (Number.isNaN(date)) return Promise.reject('Invalid date');
date = date.toISOString();
await this.client.proxy.guilds(this.guildId)['voice-states']['@me'].patch({
body: { request_to_speak_timestamp: date },
});
this.requestToSpeakTimestamp = date;
}
disconnect(reason?: string) {
return this.setChannel(null, reason);
}
async fetch(force = false) {
const member = this.withMember ?? (await this.member(force));
return this.client.members.voice(this.guildId, member.id, force);
}
async setChannel(channel_id: null | string, reason?: string) {
const member = await this.client.members.edit(this.guildId, this.userId, { channel_id }, reason);
this.channelId = channel_id;
return member;
}
}