i have made a discord bot my ban commands works but my kick command is not working - discord.js

const { getUserFromMention } = require("../userinfo/getuser.js");
module.exports = {
name: "kick",
description: "kick a player",
execute(message, client) {
const split = message.content.split(/ +/);
const args = split.slice(1);
const member = getUserFromMention(args[0], client);
if (!member) {
return message.reply("Say the name lets dew it !");
}
if (!message.member.permissions.has("KICK_MEMBERS")) {
return message.reply("I can't kick this user.");
}
return message.guild.members
.kick(member)
.then(() => message.reply(`${member.username} was kicked.`))
.catch((error) => message.reply("They are above my paygrade"));
},
};

You can use the kick function in the member object itself
Try this
return member.kick()
.then(() => message.reply(`${member.user.username} was kicked.`))
.catch(error => message.reply('They are above my paygrade'));

Try This
const { getUserFromMention } = require("../userinfo/getuser.js");
module.exports = {
name: "kick",
description: "kick a player",
execute(message, client) {
const split = message.content.split(/ +/);
const args = split.slice(1);
const member = getUserFromMention(args[0], client);
if (!member) {
return message.reply("Say the name lets dew it !");
}
if (!message.member.permissions.has("KICK_MEMBERS")) {
return message.reply("I can't kick this user.");
}
return member.kick()
.then(() => message.reply(`${member.user.username} was kicked.`))
.catch(error => message.reply('They are above my paygrade'));
},
};

Related

'message.reply not defined' discord.js v13

So I'm creating a bot with a handler online, and I'm trying to send an embed through message.reply, but it comes and error saying 'reply' is not defined. Here is the code:
const { Discord, MessageEmbed } = require('discord.js')
module.exports = {
callback: async ({ client, message, ...args }) => {
const embed = new MessageEmbed()
.setAuthor({
name: 'ยป Ping',
iconURL: client.user.defaultAvatarURL
})
.setTitle('Calculating ping...')
.setColor('GREEN')
message.reply({
embeds: [embed]
})
}
}
And here is the handler:
const fs = require('fs')
const getFiles = require('./get-files')
module.exports = (client) => {
const commands = {}
const suffix = '.js'
const commandFiles = getFiles('./commands', suffix)
for (const command of commandFiles) {
let commandFile = require(command)
if (commandFile.default) commandFile = commandFile.default
const split = command.replace(/\\/g, '/').split('/')
const commandName = split[split.length - 1].replace(suffix, '')
commands[commandName.toLowerCase()] = commandFile
}
client.on('messageCreate', (message) => {
if (message.author.bot || !message.content.startsWith('!')) {
return
}
const args = message.content.slice(1).split(/ +/)
const commandName = args.shift().toLowerCase()
if (!commands[commandName]) {
return
}
try {
commands[commandName].callback(client, message, ...args)
} catch (err) {
console.error('[ERROR]', err)
}
})
}
I would appreciate if you found out.
Thanks!

message.member.voice.channel; not working

Basically, i'm coding a discord bot and i'm trying to code a music bot / command. On line 14 whenever ?play is run (the command) i get TypeError: Cannot read property 'voice' of undefined (NOTE: i AM in a voice channel when i run this command) Im using discord js v.12.4 not sure how to check :/ I've gone over some stack overflow questions and they're all saying to update message.member.voiceChannel to message.member.voice.channel. btw i have tried message.member.voiceChannel it returns the same error.
code:
const ytSearch = require('yt-search');
const queue = new Map();
module.exports = {
name: 'play',
aliases: ['skip', 'stop'],
cooldown: 10,
description: 'Advanced music bot',
async execute(message, args, cmd, client, Discord){
const voice_channel = message.member.voice.channel;
if (!voice_channel) return message.channel.send('You need to be in a channel to execute this command!');
const permissions = voice_channel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
const server_queue = queue.get(message.guild.id);
if (cmd === 'play'){
if (!args.length) return message.channel.send('You need to send the second argument!');
let song = {};
if (ytdl.validateURL(args[0])) {
const song_info = await ytdl.getInfo(args[0]);
song = { title: song_info.videoDetails.title, url: song_info.videoDetails.video_url }
} else {
const video_finder = async (query) =>{
const video_result = await ytSearch(query);
return (video_result.videos.length > 1) ? video_result.videos[0] : null;
}
const video = await video_finder(args.join(' '));
if (video){
song = { title: video.title, url: video.url }
} else {
message.channel.send('Error finding video.');
}
}
if (!server_queue){
const queue_constructor = {
voice_channel: voice_channel,
text_channel: message.channel,
connection: null,
songs: []
}
queue.set(message.guild.id, queue_constructor);
queue_constructor.songs.push(song);
try {
const connection = await voice_channel.join();
queue_constructor.connection = connection;
video_player(message.guild, queue_constructor.songs[0]);
} catch (err) {
queue.delete(message.guild.id);
message.channel.send('There was an error connecting!');
throw err;
}
} else{
server_queue.songs.push(song);
return message.channel.send(`๐Ÿ‘ **${song.title}** added to queue!`);
}
}
else if(cmd === 'skip') skip_song(message, server_queue);
else if(cmd === 'stop') stop_song(message, server_queue);
}
}
const video_player = async (guild, song) => {
const song_queue = queue.get(guild.id);
if (!song) {
song_queue.voice_channel.leave();
queue.delete(guild.id);
return;
}
const stream = ytdl(song.url, { filter: 'audioonly' });
song_queue.connection.play(stream, { seek: 0, volume: 0.5 })
.on('finish', () => {
song_queue.songs.shift();
video_player(guild, song_queue.songs[0]);
});
await song_queue.text_channel.send(`๐ŸŽถ Now playing **${song.title}**`)
}
const skip_song = (message, server_queue) => {
if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!');
if(!server_queue){
return message.channel.send(`There are no songs in queue ๐Ÿ˜”`);
}
server_queue.connection.dispatcher.end();
}
const stop_song = (message, server_queue) => {
if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!');
server_queue.songs = [];
server_queue.connection.dispatcher.end();
}```

Discord.js voice, player on finish event

I'm trying to learn discord.js so I wanted to learn how music bots work. The code below is my shot and everything works but I'm having an issue with my queue.
I cannot get the player.on "finish" or "end" events. So when the song ends I want to call the recursive method that plays a song but I don't know how to get the correct event.
I have tried using player.on(AudioPlayerStatus.Idle) events and making something out of that but that didn't work out so well.
If anyone has any advice to what am I doing wrong or how to improve the code below, I'd be grateful.
const queue = new Map();
module.exports = {
name: "play",
aliases: ["skip", "stop", "leave"],
async execute(message, args, cmd, client, Discord) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.channel.send({
content: "You need to be in a channel to execute this command!",
});
}
const permissions = voiceChannel.permissionsFor(message.member);
if (!(permissions.has('CONNECT')) || !(permissions.has('SPEAK'))) {
return message.channel.send({
content: "You don't have the correct permissions!",
});
}
const serverQueue = queue.get(message.guild.id);
if (cmd == "play") {
if (!(args.length)) {
return message.channel.send({
content: "Invalid song link!",
});
}
let song = {};
if (ytdl.validateURL(args[0])) {
const songInfo = await ytdl.getInfo(args[0]);
song = { title: songInfo.videoDetails.title, url: songInfo.videoDetails.video_url }
} else {
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(" "));
if (video) {
song = { title: video.title, url: video.url }
} else {
message.channel.send({
content: `No video results found!`
})
}
}
if (!(serverQueue)) {
const queueConstructor = {
voiceChannel: voiceChannel,
textChannel: message.channel,
connection: null,
player: null,
songs: [],
}
queue.set(message.guild.id, queueConstructor);
queueConstructor.songs.push(song);
try {
const connection = await joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.member.voice.channel.guild.id,
adapterCreator: message.member.voice.channel.guild.voiceAdapterCreator,
});
queueConstructor.connection = connection;
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause
}
});
queueConstructor.player = player;
videoPlayer(message.guild, queueConstructor.songs[0]);
} catch (error) {
queue.delete(message.guild.id);
message.channel.send({
content: `There was an error connecting!`
})
throw error;
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
return message.channel.send({
content: `${song.title} added to the queue!`
})
}
} else if (cmd == "skip") {
// TODO
} else if (cmd == "leave") {
// TODO
}
}
}
const videoPlayer = async (guild, song) => {
const songQueue = queue.get(guild.id);
if (!(song)) {
songQueue.player.stop();
songQueue.connection.destroy();
queue.delete(guild.id);
return;
}
const stream = await ytdl(song.url, { filter: "audioonly" });
const songStream = await (createAudioResource(stream));
const subscription = songQueue.connection.subscribe(songQueue.player);
const dispatcher = songQueue.player.play(createAudioResource(stream)).on("finish", () => {
songQueue.songs.shift();
songQueue.playing = false;
videoPlayer(guild, songQueue.songs[0]);
songQueue.textChannel.send({
content: `Now playing: ${song.title}`
});
});
}
player.addListener("stateChange", (oldOne, newOne) => {
if (newOne.status == "idle") {
console.log("The song finished");
}
});
Use event stateChange. It called when voice state change from oldOne to newOne. (ex: playing -> idle)
newOne { status: "playing | idle | buffering | ..." }

Discord.js v13, #discordjs/voice Play Music Command

This is my code,
The command executes perfectly, The bot joins the voice channel and also sends the name of the song its about to play, but it doesnt play the song in the voice channel.
This is my first time ever asking a question on stackoverflow so dont mind the format and stuff. But I really need help here.
Discord v13 and latest node module.
const ytsearch = require('yt-search');
const Discord = require('discord.js')
const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
NoSubscriberBehavior
} = require('#discordjs/voice');
module.exports = {
name: "play",
description: "test command",
async run(client, message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please connect to a voice channel!');
if (!args.length) return message.channel.send('Please Provide Something To Play!')
const connection = await joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
const videoFinder = async (query) => {
const videoResult = await ytsearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(' '));
if (video) {
const stream = ytdl(video.url, { filter: 'audioonly' });
const player = createAudioPlayer();
const resource = createAudioResource(stream)
await player.play(resource);
connection.subscribe(player);
await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
} else {
message.channel.send('No video results found');
}
}
}```
I would suggest you look at the music bot example at #discordjs/voice.
They do a good job of how to extract the stream from ytdl.
I'm currently still learning how this all works but the part that you will want to look at is in the createAudioResource function.
public createAudioResource(): Promise<AudioResource<Track>> {
return new Promise((resolve, reject) => {
const process = ytdl(
this.url,
{
o: '-',
q: '',
f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
r: '100K',
},
{ stdio: ['ignore', 'pipe', 'ignore'] },
);
if (!process.stdout) {
reject(new Error('No stdout'));
return;
}
const stream = process.stdout;
const onError = (error: Error) => {
if (!process.killed) process.kill();
stream.resume();
reject(error);
};
process
.once('spawn', () => {
demuxProbe(stream)
.then((probe) => resolve(createAudioResource(probe.stream, { metadata: this, inputType: probe.type })))
.catch(onError);
})
.catch(onError);
});
}

My music play command isn't working or sending any error, how can I fix this?

I am trying to make a my bot play music, and so when I run my command, it doesn't show any error or joining the vc. however when I'm not in the vc it does send me a message telling me to execute this command after joining the vc. I do have Ytdl and YtSearch installed ofcourse, but I can't figure out how to fix this. I'm using repl btw. I'm using Aliases for the skip and stop command.
The code is below.
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const queue = new Map();
module.exports = {
name: 'play',
aliases: ['skip', 'stop'],
description: 'Plays music',
async execute(message, args, cmd, client, Discord){
const voice_channel = message.member.voice.channel;
if (!voice_channel) return message.channel.send('You need to be in a channel to execute this command!');
const permissions = voice_channel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
const server_queue = queue.get(message.guild.id);
if (cmd === 'play'){
if (!args.length) return message.channel.send('You need to send the second argument!');
let song = {};
if (ytdl.validateURL(args[0])) {
const song_info = await ytdl.getInfo(args[0]);
song = { title: song_info.videoDetails.title, url: song_info.videoDetails.video_url }
} else {
const video_finder = async (query) =>{
const video_result = await ytSearch(query);
return (video_result.videos.length > 1) ? video_result.videos[0] : null;
}
const video = await video_finder(args.join(' '));
if (video){
song = { title: video.title, url: video.url }
} else {
message.channel.send('Error finding video.');
}
}
if (!server_queue){
const queue_constructor = {
voice_channel: voice_channel,
text_channel: message.channel,
connection: null,
songs: []
}
queue.set(message.guild.id, queue_constructor);
queue_constructor.songs.push(song);
try {
const connection = await voice_channel.join();
queue_constructor.connection = connection;
video_player(message.guild, queue_constructor.songs[0]);
} catch (err) {
queue.delete(message.guild.id);
message.channel.send('There was an error connecting!');
throw err;
}
} else{
server_queue.songs.push(song);
return message.channel.send(`๐Ÿ‘ **${song.title}** added to queue!`);
}
}
else if(cmd === 'skip') skip_song(message, server_queue);
else if(cmd === 'stop') stop_song(message, server_queue);
}
}
const video_player = async (guild, song) => {
const song_queue = queue.get(guild.id);
if (!song) {
song_queue.voice_channel.leave();
queue.delete(guild.id);
return;
}
const stream = ytdl(song.url, { filter: 'audioonly' });
song_queue.connection.play(stream, { seek: 0, volume: 0.5 })
.on('finish', () => {
song_queue.songs.shift();
video_player(guild, song_queue.songs[0]);
});
await song_queue.text_channel.send(`๐ŸŽถ Now playing **${song.title}**`)
}
const skip_song = (message, server_queue) => {
if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!');
if(!server_queue){
return message.channel.send(`There are no songs in queue ๐Ÿ˜”`);
}
server_queue.connection.dispatcher.end();
}
const stop_song = (message, server_queue) => {
if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!');
server_queue.songs = [];
server_queue.connection.dispatcher.end();
}

Resources