Comparing version 1.1.0 to 1.1.1
@@ -47,2 +47,16 @@ "use strict"; | ||
} | ||
leave(channel) { | ||
const vc = this.client.channels.resolve(channel !== null && channel !== void 0 ? channel : ""); | ||
if (!vc || !vc.isVoice()) | ||
throw new Error("Voice channel was not provided!"); | ||
if (!["GUILD_STAGE_VOICE", "GUILD_VOICE"].includes(vc.type)) { | ||
throw new TypeError("Cannot leave non-voice channel"); | ||
} | ||
if (!this.connections.has(vc.guildId)) | ||
return; | ||
const connection = this.connections.get(vc.guildId); | ||
connection.disconnect(); | ||
connection.destroy(); | ||
this.connections.delete(vc.guildId); | ||
} | ||
updateChannel(connection, channel) { | ||
@@ -49,0 +63,0 @@ return __awaiter(this, void 0, void 0, function* () { |
@@ -20,3 +20,3 @@ "use strict"; | ||
this.connection = connection; | ||
this.audioPlayer = voice_1.createAudioPlayer(); | ||
this.audioPlayer = (0, voice_1.createAudioPlayer)(); | ||
this.audioResource = null; | ||
@@ -41,3 +41,3 @@ this.readyLock = false; | ||
try { | ||
yield voice_1.entersState(this.connection.voice, voice_1.VoiceConnectionStatus.Connecting, 5000); | ||
yield (0, voice_1.entersState)(this.connection.voice, voice_1.VoiceConnectionStatus.Connecting, 5000); | ||
} | ||
@@ -51,3 +51,3 @@ catch (_b) { | ||
else if (this.connection.voice.rejoinAttempts < 5) { | ||
yield Util_1.wait((this.connection.voice.rejoinAttempts + 1) * 5000); | ||
yield (0, Util_1.wait)((this.connection.voice.rejoinAttempts + 1) * 5000); | ||
this.connection.voice.rejoin(); | ||
@@ -67,3 +67,3 @@ } | ||
try { | ||
yield voice_1.entersState(this.connection.voice, voice_1.VoiceConnectionStatus.Ready, 20000); | ||
yield (0, voice_1.entersState)(this.connection.voice, voice_1.VoiceConnectionStatus.Ready, 20000); | ||
} | ||
@@ -107,4 +107,13 @@ catch (_c) { | ||
var _a; | ||
this.audioResource = voice_1.createAudioResource(stream, { | ||
inputType: (options === null || options === void 0 ? void 0 : options.type) || voice_1.StreamType.Arbitrary, | ||
this.audioResource = (0, voice_1.createAudioResource)(stream, { | ||
inputType: | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
{ | ||
// discord.js v12 types | ||
converted: voice_1.StreamType.Raw, | ||
unknown: voice_1.StreamType.Arbitrary | ||
}[options === null || options === void 0 ? void 0 : options.type] || | ||
(options === null || options === void 0 ? void 0 : options.type) || | ||
voice_1.StreamType.Arbitrary, | ||
inlineVolume: (_a = options === null || options === void 0 ? void 0 : options.inlineVolume) !== null && _a !== void 0 ? _a : true | ||
@@ -111,0 +120,0 @@ }); |
@@ -38,2 +38,12 @@ "use strict"; | ||
try { | ||
this.dispatcher.removeAllListeners(); | ||
this.receiver.removeAllListeners(); | ||
this.voice.disconnect(); | ||
} | ||
catch (_a) { | ||
/* noop */ | ||
} | ||
} | ||
destroy() { | ||
try { | ||
this.voiceManager.connections.delete(this.channel.guildId); | ||
@@ -51,3 +61,3 @@ this.dispatcher.removeAllListeners(); | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
const [error, connection] = yield Util_1.catchError(() => VoiceConnection.joinChannel(channel, options)); | ||
const [error, connection] = yield (0, Util_1.catchError)(() => VoiceConnection.joinChannel(channel, options)); | ||
if (error) | ||
@@ -65,3 +75,3 @@ return reject(error); | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
const rawConnection = voice_1.joinVoiceChannel({ | ||
const rawConnection = (0, voice_1.joinVoiceChannel)({ | ||
guildId: channel.guild.id, | ||
@@ -74,4 +84,4 @@ channelId: channel.id, | ||
}); | ||
const [error, connection] = yield Util_1.catchError(() => { | ||
return voice_1.entersState(rawConnection, voice_1.VoiceConnectionStatus.Ready, 30000); | ||
const [error, connection] = yield (0, Util_1.catchError)(() => { | ||
return (0, voice_1.entersState)(rawConnection, voice_1.VoiceConnectionStatus.Ready, 30000); | ||
}); | ||
@@ -95,3 +105,4 @@ if (error) | ||
get ping() { | ||
return this.voice.ping; | ||
const latency = this.voice.ping.udp; | ||
return typeof latency !== "number" ? NaN : latency; | ||
} | ||
@@ -98,0 +109,0 @@ } |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -30,3 +30,3 @@ /// <reference types="node" /> | ||
interface PlayOptions { | ||
type?: `${StreamType}` | StreamType; | ||
type?: `${StreamType}` | StreamType | "converted" | "unknown"; | ||
inlineVolume?: boolean; | ||
@@ -84,2 +84,3 @@ } | ||
disconnect(): void; | ||
destroy(): void; | ||
static createConnection(channel: VoiceChannels, manager: DartVoiceManager, options?: VoiceJoinConfig): Promise<VoiceConnection>; | ||
@@ -89,6 +90,3 @@ static joinChannel(channel: VoiceChannels, options?: VoiceJoinConfig): Promise<VoiceConnection$1>; | ||
get status(): VoiceConnectionStatus; | ||
get ping(): { | ||
ws: number; | ||
udp: number; | ||
}; | ||
get ping(): number; | ||
} | ||
@@ -101,2 +99,3 @@ | ||
join(channel: GuildVoiceChannelResolvable, options?: VoiceJoinConfig): Promise<VoiceConnection>; | ||
leave(channel: GuildVoiceChannelResolvable): void; | ||
updateChannel(connection: VoiceConnection, channel: VoiceChannels): Promise<VoiceConnection>; | ||
@@ -103,0 +102,0 @@ } |
@@ -0,0 +0,0 @@ "use strict"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "dartjs", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Very simple framework that provides discord.js v12 voice interface", | ||
@@ -27,3 +27,3 @@ "main": "dist/index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/CesiumLabs/dartjs.git" | ||
"url": "git+https://github.com/DevAndromeda/dartjs.git" | ||
}, | ||
@@ -34,3 +34,7 @@ "keywords": [ | ||
"voice", | ||
"v12" | ||
"v12", | ||
"v13", | ||
"discord.js-v12-voice", | ||
"discord.js-v13-voice", | ||
"discord.js-v12-voice-v13" | ||
], | ||
@@ -40,5 +44,5 @@ "author": "DevAndromeda", | ||
"bugs": { | ||
"url": "https://github.com/CesiumLabs/dartjs/issues" | ||
"url": "https://github.com/DevAndromeda/dartjs/issues" | ||
}, | ||
"homepage": "https://github.com/CesiumLabs/typescript-template#readme", | ||
"homepage": "https://github.com/DevAndromeda/typescript-template#readme", | ||
"devDependencies": { | ||
@@ -45,0 +49,0 @@ "@favware/rollup-type-bundler": "^1.0.3", |
@@ -13,15 +13,25 @@ # DartJS | ||
# Features | ||
* supports voice receiving | ||
* supports voice sending | ||
* close to discord.js v12 voice api | ||
* injects nothing to your client or discord.js | ||
# Note | ||
* Use `<Dispatcher>.once` instead of `<Dispatcher>.on` | ||
Use `<Dispatcher>.once` instead of `<Dispatcher>.on`. Since the concept of `StreamDispatcher` is gone with the [voice rewrite](https://npmjs.com/package/@discordjs/voice), the StreamDispatcher polyfill does not work exactly like discord.js v12 voice. Using `.on` to handle events may cause the dispatcher to trigger the event multiple times since same dispatcher instance is used throughout the session. Using `.once` to handle events will only trigger the event once per your music bot session and wont break your code since `.once` removes the listener immediately after the event is emitted. | ||
> This library was created just for learning purpose and a personal music bot. You don't have to use this | ||
unless you really want to. | ||
# Example | ||
## Music Bot | ||
```js | ||
const Discord = require("discord.js"); | ||
const client = new Discord.Client({ | ||
intents: [Discord.Intents.GUILDS, Discord.Intents.GUILD_VOICE_STATES, Discord.Intents.GUILD_MESSAGES] | ||
intents: [ | ||
Discord.Intents.GUILDS, | ||
Discord.Intents.GUILD_VOICE_STATES, | ||
Discord.Intents.GUILD_MESSAGES | ||
] | ||
}); | ||
@@ -35,18 +45,60 @@ const { DartVoiceManager } = require("dartjs"); | ||
client.on("messageCreate", message => { | ||
if (message.author.bot) return; | ||
if (message.author.bot) return; | ||
if (message.content === "!play") { | ||
voiceManager.join(message.member.voice.channel) | ||
.then(connection => { | ||
const dispatcher = connection.play(ytdl("https://www.youtube.com/watch?v=dQw4w9WgXcQ")); | ||
dispatcher.once("start", () => message.channel.send("Music started!")); | ||
dispatcher.once("finish", () => { | ||
connection.disconnect(); | ||
message.channel.send("Music finished!"); | ||
}); | ||
}); | ||
} | ||
if (message.content === "!play") { | ||
voiceManager.join(message.member.voice.channel) | ||
.then(connection => { | ||
const dispatcher = connection.play(ytdl("https://www.youtube.com/watch?v=dQw4w9WgXcQ")); | ||
dispatcher.once("start", () => message.channel.send("Music started!")); | ||
dispatcher.once("finish", () => { | ||
message.channel.send("Music finished!"); | ||
}); | ||
}); | ||
} | ||
}); | ||
client.login("XXX"); | ||
``` | ||
``` | ||
## Voice Receiving | ||
```js | ||
const Discord = require("discord.js"); | ||
const client = new Discord.Client({ | ||
intents: [ | ||
Discord.Intents.GUILDS, | ||
Discord.Intents.GUILD_VOICE_STATES, | ||
Discord.Intents.GUILD_MESSAGES, | ||
Discord.Intents.GUILD_MEMBERS | ||
] | ||
}); | ||
const { DartVoiceManager } = require("dartjs"); | ||
const voiceManager = new DartVoiceManager(client); | ||
const fs = require("fs"); | ||
client.on("ready", () => console.log("Bot is online!")); | ||
client.on("messageCreate", message => { | ||
if (message.author.bot) return; | ||
if (message.content === "!record") { | ||
voiceManager.join(message.member.voice.channel) | ||
.then(connection => { | ||
const receiver = connection.receiver.createStream(message.member, { | ||
mode: "pcm", | ||
end: "silence" | ||
}); | ||
const writer = receiver.pipe(fs.createWriteStream("./recorded.pcm")); | ||
writer.on("finish", () => { | ||
message.channel.send("Finished recording!"); | ||
}); | ||
}); | ||
} | ||
}); | ||
client.login("XXX"); | ||
``` | ||
See **[https://github.com/discord-player/voice-recorder-example](https://github.com/discord-player/voice-recorder-example)** for a complete voice recorder example. |
Sorry, the diff of this file is not supported yet
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
33014
567
102
1