Calling support (1)

This commit is contained in:
March 7th
2022-05-21 20:58:33 +07:00
parent cc9c4f3145
commit f9ec41c9b6
11 changed files with 303 additions and 39 deletions

View File

@@ -1,8 +1,10 @@
'use strict';
const { joinVoiceChannel, entersState, VoiceConnectionStatus } = require('@discordjs/voice');
const { Channel } = require('./Channel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const MessageManager = require('../managers/MessageManager');
const { Status } = require('../util/Constants');
/**
* Represents a direct message channel between two users.
@@ -97,18 +99,58 @@ class DMChannel extends Channel {
// Testing feature: Call
// URL: https://discord.com/api/v9/channels/DMchannelId/call/ring
/**
* Call this DMChannel. [TEST only]
* @returns {Promise<void>}
* Call this DMChannel. Return discordjs/voice VoiceConnection
* @param {Object} options Options for the call (selfDeaf, selfMute: Boolean)
* @returns {Promise<VoiceConnection>}
*/
call() {
console.log('TEST only, and not working !');
return this.client.api.channels(this.id).call.ring.post({
usingApplicationJson: true,
data: {
recipients: null,
},
call(options = {}) {
return new Promise((resolve, reject) => {
this.client.api.channels(this.id).call.ring.post({
usingApplicationJson: true,
data: {
recipients: null,
},
});
const connection = joinVoiceChannel({
channelId: this.id,
guildId: null,
adapterCreator: this.voiceAdapterCreator,
selfDeaf: options.selfDeaf ?? false,
selfMute: options.selfMute ?? false,
});
entersState(connection, VoiceConnectionStatus.Ready, 30000)
.then(connection => {
resolve(connection);
})
.catch(err => {
connection.destroy();
reject(err);
});
});
}
get shard() {
return this.client.ws.shards.first();
}
get voiceAdapterCreator() {
return methods => {
this.client.voice.adapters.set(this.id, methods);
return {
sendPayload: data => {
data.d = {
...data.d,
self_video: false,
};
if (this.shard.status !== Status.READY) return false;
console.log('DM channel send payload', data);
this.shard.send(data);
return true;
},
destroy: () => {
this.client.voice.adapters.delete(this.id);
},
};
};
}
}
TextBasedChannel.applyToClass(DMChannel, true, ['bulkDelete']);

View File

@@ -1,12 +1,14 @@
'use strict';
const { Collection } = require('@discordjs/collection');
const { joinVoiceChannel, entersState, VoiceConnectionStatus } = require('@discordjs/voice');
const { Channel } = require('./Channel');
const Invite = require('./Invite');
const User = require('./User');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { Error } = require('../errors');
const MessageManager = require('../managers/MessageManager');
const { Status } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
/**
@@ -204,18 +206,58 @@ class PartialGroupDMChannel extends Channel {
// Testing feature: Call
// URL: https://discord.com/api/v9/channels/DMchannelId/call/ring
/**
* Call this DMChannel. [TEST only]
* @returns {Promise<void>}
* Call this Group DMChannel. Return discordjs/voice VoiceConnection
* @param {Object} options Options for the call (selfDeaf, selfMute: Boolean)
* @returns {Promise<VoiceConnection>}
*/
call() {
console.log('TEST only, and not working !');
return this.client.api.channels(this.id).call.ring.post({
usingApplicationJson: true,
data: {
recipients: null,
},
call(options = {}) {
return new Promise((resolve, reject) => {
this.client.api.channels(this.id).call.ring.post({
usingApplicationJson: true,
data: {
recipients: null,
},
});
const connection = joinVoiceChannel({
channelId: this.id,
guildId: null,
adapterCreator: this.voiceAdapterCreator,
selfDeaf: options.selfDeaf ?? false,
selfMute: options.selfMute ?? false,
});
entersState(connection, VoiceConnectionStatus.Ready, 30000)
.then(connection => {
resolve(connection);
})
.catch(err => {
connection.destroy();
reject(err);
});
});
}
get shard() {
return this.client.ws.shards.first();
}
get voiceAdapterCreator() {
return methods => {
this.client.voice.adapters.set(this.id, methods);
return {
sendPayload: data => {
data.d = {
...data.d,
self_video: false,
};
if (this.shard.status !== Status.READY) return false;
console.log('DM channel send payload', data);
this.shard.send(data);
return true;
},
destroy: () => {
this.client.voice.adapters.delete(this.id);
},
};
};
}
}
TextBasedChannel.applyToClass(PartialGroupDMChannel, false);

View File

@@ -132,6 +132,7 @@ class VoiceState extends Base {
* @readonly
*/
get member() {
if (!this.guild?.id) return null;
return this.guild.members.cache.get(this.id) ?? null;
}
@@ -141,6 +142,7 @@ class VoiceState extends Base {
* @readonly
*/
get channel() {
if (!this.guild?.id) return null;
return this.guild.channels.cache.get(this.channelId) ?? null;
}
@@ -169,6 +171,7 @@ class VoiceState extends Base {
* @returns {Promise<GuildMember>}
*/
setMute(mute = true, reason) {
if (!this.guild?.id) return null;
return this.guild.members.edit(this.id, { mute }, reason);
}
@@ -179,6 +182,7 @@ class VoiceState extends Base {
* @returns {Promise<GuildMember>}
*/
setDeaf(deaf = true, reason) {
if (!this.guild?.id) return null;
return this.guild.members.edit(this.id, { deaf }, reason);
}
@@ -188,6 +192,7 @@ class VoiceState extends Base {
* @returns {Promise<GuildMember>}
*/
disconnect(reason) {
if (!this.guild?.id) return this.callVoice?.disconnect();
return this.setChannel(null, reason);
}
@@ -199,6 +204,7 @@ class VoiceState extends Base {
* @returns {Promise<GuildMember>}
*/
setChannel(channel, reason) {
if (!this.guild?.id) return null;
return this.guild.members.edit(this.id, { channel }, reason);
}
@@ -215,16 +221,18 @@ class VoiceState extends Base {
* @returns {Promise<void>}
*/
async setRequestToSpeak(request = true) {
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.guild?.id) {
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.client.user.id !== this.id) throw new Error('VOICE_STATE_NOT_OWN');
if (this.client.user.id !== this.id) throw new Error('VOICE_STATE_NOT_OWN');
await this.client.api.guilds(this.guild.id, 'voice-states', '@me').patch({
data: {
channel_id: this.channelId,
request_to_speak_timestamp: request ? new Date().toISOString() : null,
},
});
await this.client.api.guilds(this.guild.id, 'voice-states', '@me').patch({
data: {
channel_id: this.channelId,
request_to_speak_timestamp: request ? new Date().toISOString() : null,
},
});
}
}
/**
@@ -245,18 +253,20 @@ class VoiceState extends Base {
* @returns {Promise<void>}
*/
async setSuppressed(suppressed = true) {
if (typeof suppressed !== 'boolean') throw new TypeError('VOICE_STATE_INVALID_TYPE', 'suppressed');
if (this.guild?.id) {
if (typeof suppressed !== 'boolean') throw new TypeError('VOICE_STATE_INVALID_TYPE', 'suppressed');
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
if (this.channel?.type !== 'GUILD_STAGE_VOICE') throw new Error('VOICE_NOT_STAGE_CHANNEL');
const target = this.client.user.id === this.id ? '@me' : this.id;
const target = this.client.user.id === this.id ? '@me' : this.id;
await this.client.api.guilds(this.guild.id, 'voice-states', target).patch({
data: {
channel_id: this.channelId,
suppress: suppressed,
},
});
await this.client.api.guilds(this.guild.id, 'voice-states', target).patch({
data: {
channel_id: this.channelId,
suppress: suppressed,
},
});
}
}
/**
@@ -264,6 +274,8 @@ class VoiceState extends Base {
* @returns {string} URL Image of the user's streaming video
*/
async getPreview() {
if (!this.guild?.id) return null;
if (!this.streaming) throw new Error('USER_NOT_STREAMING');
// URL: https://discord.com/api/v9/streams/guild:guildid:voicechannelid:userid/preview
const data = await this.client.api.streams[