mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-04-19 12:53:56 +02:00
Require user to be in voice channel
This commit is contained in:
parent
362ce89987
commit
4659717e5f
12 changed files with 42 additions and 9 deletions
11
src/bot.ts
11
src/bot.ts
|
@ -8,6 +8,8 @@ import debug from './utils/debug';
|
||||||
import NaturalLanguage from './services/natural-language-commands';
|
import NaturalLanguage from './services/natural-language-commands';
|
||||||
import handleGuildCreate from './events/guild-create';
|
import handleGuildCreate from './events/guild-create';
|
||||||
import handleVoiceStateUpdate from './events/voice-state-update';
|
import handleVoiceStateUpdate from './events/voice-state-update';
|
||||||
|
import errorMsg from './utils/error-msg';
|
||||||
|
import {isUserInVoice} from './utils/channels';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class {
|
export default class {
|
||||||
|
@ -81,10 +83,15 @@ export default class {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
handler.execute(msg, args);
|
if (handler.requiresVC && !isUserInVoice(msg.guild, msg.author)) {
|
||||||
|
await msg.channel.send(errorMsg('gotta be in a voice channel'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await handler.execute(msg, args);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
msg.reply('there was an error trying to execute that command!');
|
await msg.channel.send(errorMsg('¯\\_(ツ)_/¯'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ export default class implements Command {
|
||||||
['clear', 'clears all songs in queue except currently playing']
|
['clear', 'clears all songs in queue except currently playing']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
|
|
|
@ -13,6 +13,8 @@ export default class implements Command {
|
||||||
['disconnect', 'pauses and disconnects player']
|
['disconnect', 'pauses and disconnects player']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
||||||
|
|
|
@ -15,6 +15,8 @@ export default class implements Command {
|
||||||
['fseek 10', 'skips forward in current song by 10 seconds']
|
['fseek 10', 'skips forward in current song by 10 seconds']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
|
|
|
@ -4,5 +4,6 @@ export default interface Command {
|
||||||
name: string;
|
name: string;
|
||||||
aliases: string[];
|
aliases: string[];
|
||||||
examples: string[][];
|
examples: string[][];
|
||||||
|
requiresVC?: boolean;
|
||||||
execute: (msg: Message, args: string[]) => Promise<void>;
|
execute: (msg: Message, args: string[]) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,8 @@ export default class implements Command {
|
||||||
['pause', 'pauses currently playing song']
|
['pause', 'pauses currently playing song']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
||||||
|
|
|
@ -26,6 +26,8 @@ export default class implements Command {
|
||||||
['play https://open.spotify.com/playlist/37i9dQZF1DX94qaYRnkufr?si=r2fOVL_QQjGxFM5MWb84Xw', 'adds all songs from playlist to the queue']
|
['play https://open.spotify.com/playlist/37i9dQZF1DX94qaYRnkufr?si=r2fOVL_QQjGxFM5MWb84Xw', 'adds all songs from playlist to the queue']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
private readonly getSongs: GetSongs;
|
private readonly getSongs: GetSongs;
|
||||||
|
@ -37,16 +39,11 @@ 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!);
|
const [targetVoiceChannel] = getMostPopularVoiceChannel(msg.guild!);
|
||||||
|
|
||||||
const res = new LoadingMessage(msg.channel as TextChannel);
|
const res = new LoadingMessage(msg.channel as TextChannel);
|
||||||
await res.start();
|
await res.start();
|
||||||
|
|
||||||
if (nInChannel === 0) {
|
|
||||||
await res.stop(errorMsg('all voice channels are empty'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const queue = this.queueManager.get(msg.guild!.id);
|
const queue = this.queueManager.get(msg.guild!.id);
|
||||||
const player = this.playerManager.get(msg.guild!.id);
|
const player = this.playerManager.get(msg.guild!.id);
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ export default class implements Command {
|
||||||
['seek 1:00:00', 'seeks to 1 hour from beginning of song']
|
['seek 1:00:00', 'seeks to 1 hour from beginning of song']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,8 @@ export default class implements Command {
|
||||||
['shuffle', 'shuffles the current queue']
|
['shuffle', 'shuffles the current queue']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
|
|
|
@ -13,6 +13,8 @@ export default class implements Command {
|
||||||
['skip', 'skips the current song']
|
['skip', 'skips the current song']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@ export default class implements Command {
|
||||||
['unskip', 'goes back in the queue by one song']
|
['unskip', 'goes back in the queue by one song']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public requiresVC = true;
|
||||||
|
|
||||||
private readonly queueManager: QueueManager;
|
private readonly queueManager: QueueManager;
|
||||||
private readonly playerManager: PlayerManager;
|
private readonly playerManager: PlayerManager;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,16 @@
|
||||||
import {Guild, VoiceChannel} from 'discord.js';
|
import {Guild, VoiceChannel, User} from 'discord.js';
|
||||||
|
|
||||||
|
export const isUserInVoice = (guild: Guild, user: User): boolean => {
|
||||||
|
let inVoice = false;
|
||||||
|
|
||||||
|
guild.channels.cache.filter(channel => channel.type === 'voice').forEach(channel => {
|
||||||
|
if (channel.members.array().find(member => member.id === user.id)) {
|
||||||
|
inVoice = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return inVoice;
|
||||||
|
};
|
||||||
|
|
||||||
export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.array().reduce((s, member) => {
|
export const getSizeWithoutBots = (channel: VoiceChannel): number => channel.members.array().reduce((s, member) => {
|
||||||
if (!member.user.bot) {
|
if (!member.user.bot) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue