mirror of
https://github.com/BluemediaGER/muse.git
synced 2024-11-12 21:05:29 +01:00
Add auto disconnect
This commit is contained in:
parent
32cb3ca4ae
commit
ac21b5657b
|
@ -2,9 +2,10 @@ import {Client, Message, Collection} from 'discord.js';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import {TYPES} from './types';
|
import {TYPES} from './types';
|
||||||
import {Settings, Shortcut} from './models';
|
import {Settings, Shortcut} from './models';
|
||||||
import handleGuildCreate from './events/guild-create';
|
|
||||||
import container from './inversify.config';
|
import container from './inversify.config';
|
||||||
import Command from './commands';
|
import Command from './commands';
|
||||||
|
import handleGuildCreate from './events/guild-create';
|
||||||
|
import handleVoiceStateUpdate from './events/voice-state-update';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class {
|
export default class {
|
||||||
|
@ -87,12 +88,11 @@ export default class {
|
||||||
console.log(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot`);
|
console.log(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot`);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('error', error => {
|
this.client.on('error', console.error);
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Register event handlers
|
// Register event handlers
|
||||||
this.client.on('guildCreate', handleGuildCreate);
|
this.client.on('guildCreate', handleGuildCreate);
|
||||||
|
this.client.on('voiceStateUpdate', handleVoiceStateUpdate);
|
||||||
|
|
||||||
return this.client.login(this.token);
|
return this.client.login(this.token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,13 @@ export default class implements Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execute(msg: Message, args: string []): Promise<void> {
|
public async execute(msg: Message, args: string []): Promise<void> {
|
||||||
|
const [targetVoiceChannel, nInChannel] = getMostPopularVoiceChannel(msg.guild!);
|
||||||
|
|
||||||
|
if (nInChannel === 0) {
|
||||||
|
await msg.channel.send('error: all voice channels are empty');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const queue = this.queueManager.get(msg.guild!.id);
|
const queue = this.queueManager.get(msg.guild!.id);
|
||||||
|
|
||||||
if (args.length === 0) {
|
if (args.length === 0) {
|
||||||
|
@ -50,9 +57,7 @@ export default class implements Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const channel = getMostPopularVoiceChannel(msg.guild!);
|
await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel);
|
||||||
|
|
||||||
await this.playerManager.get(msg.guild!.id).connect(channel);
|
|
||||||
await this.playerManager.get(msg.guild!.id).play();
|
await this.playerManager.get(msg.guild!.id).play();
|
||||||
|
|
||||||
await msg.channel.send('play resuming');
|
await msg.channel.send('play resuming');
|
||||||
|
@ -252,9 +257,7 @@ export default class implements Command {
|
||||||
await res.stop('song(s) queued');
|
await res.stop('song(s) queued');
|
||||||
|
|
||||||
if (this.playerManager.get(msg.guild!.id).status === STATUS.DISCONNECTED) {
|
if (this.playerManager.get(msg.guild!.id).status === STATUS.DISCONNECTED) {
|
||||||
const channel = getMostPopularVoiceChannel(msg.guild!);
|
await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel);
|
||||||
|
|
||||||
await this.playerManager.get(msg.guild!.id).connect(channel);
|
|
||||||
|
|
||||||
await this.playerManager.get(msg.guild!.id).play();
|
await this.playerManager.get(msg.guild!.id).play();
|
||||||
}
|
}
|
||||||
|
|
17
src/events/voice-state-update.ts
Normal file
17
src/events/voice-state-update.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import {VoiceState} from 'discord.js';
|
||||||
|
import container from '../inversify.config';
|
||||||
|
import {TYPES} from '../types';
|
||||||
|
import PlayerManager from '../managers/player';
|
||||||
|
import {getSizeWithoutBots} from '../utils/channels';
|
||||||
|
|
||||||
|
export default (oldState: VoiceState, _: VoiceState): void => {
|
||||||
|
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);
|
||||||
|
|
||||||
|
const player = playerManager.get(oldState.guild.id);
|
||||||
|
|
||||||
|
if (player.voiceConnection) {
|
||||||
|
if (getSizeWithoutBots(player.voiceConnection.channel) === 0) {
|
||||||
|
player.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -16,9 +16,9 @@ export enum STATUS {
|
||||||
|
|
||||||
export default class {
|
export default class {
|
||||||
public status = STATUS.DISCONNECTED;
|
public status = STATUS.DISCONNECTED;
|
||||||
|
public voiceConnection: VoiceConnection | null = null;
|
||||||
private readonly queue: Queue;
|
private readonly queue: Queue;
|
||||||
private readonly cacheDir: string;
|
private readonly cacheDir: string;
|
||||||
private voiceConnection: VoiceConnection | null = null;
|
|
||||||
private dispatcher: StreamDispatcher | null = null;
|
private dispatcher: StreamDispatcher | null = null;
|
||||||
private playPositionInterval: NodeJS.Timeout | undefined;
|
private playPositionInterval: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
import {Guild, VoiceChannel} from 'discord.js';
|
import {Guild, VoiceChannel} from 'discord.js';
|
||||||
|
|
||||||
export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel => {
|
export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.array().reduce((s, member) => {
|
||||||
|
if (!member.user.bot) {
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
export const getMostPopularVoiceChannel = (guild: Guild): [VoiceChannel, number] => {
|
||||||
interface PopularResult {
|
interface PopularResult {
|
||||||
n: number;
|
n: number;
|
||||||
channel: VoiceChannel | null;
|
channel: VoiceChannel | null;
|
||||||
|
@ -9,18 +17,16 @@ export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel
|
||||||
const voiceChannels: PopularResult[] = [];
|
const voiceChannels: PopularResult[] = [];
|
||||||
|
|
||||||
for (const [_, channel] of guild.channels.cache) {
|
for (const [_, channel] of guild.channels.cache) {
|
||||||
if (channel.type === 'voice' && channel.members.size >= min) {
|
if (channel.type === 'voice') {
|
||||||
|
const size = getSizeWithoutBots(channel as VoiceChannel);
|
||||||
|
|
||||||
voiceChannels.push({
|
voiceChannels.push({
|
||||||
channel: channel as VoiceChannel,
|
channel: channel as VoiceChannel,
|
||||||
n: channel.members.size
|
n: size
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (voiceChannels.length === 0) {
|
|
||||||
throw new Error('No voice channels meet minimum size');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find most popular channel
|
// Find most popular channel
|
||||||
const popularChannel = voiceChannels.reduce((popular: PopularResult, elem: PopularResult) => {
|
const popularChannel = voiceChannels.reduce((popular: PopularResult, elem: PopularResult) => {
|
||||||
if (elem.n > popular.n) {
|
if (elem.n > popular.n) {
|
||||||
|
@ -31,7 +37,7 @@ export const getMostPopularVoiceChannel = (guild: Guild, min = 0): VoiceChannel
|
||||||
}, {n: -1, channel: null});
|
}, {n: -1, channel: null});
|
||||||
|
|
||||||
if (popularChannel.channel) {
|
if (popularChannel.channel) {
|
||||||
return popularChannel.channel;
|
return [popularChannel.channel, popularChannel.n];
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error();
|
throw new Error();
|
||||||
|
|
Loading…
Reference in a new issue