When I used this code to let my bot join my voicechannel, it didnt work, he keeps saying that I am not in a voicechannel, can someone help me because when I run the command I get no error or something.
exports.run = async (client, message) => {
const voiceChannel = message.member.voiceChannel;
if (!message.member.voiceChannel) { return message.channel.send("You are not in a voice channel, Don't force me to be lonely!"); }
const permissions = message.member.voiceChannel.permissionsFor(message.guild.me);
if (permissions.has("CONNECT") === false) { return message.channel.send(":x: I do not have enough permissions to connect to your voice channel. I am missing the Connect permission."); }
if (permissions.has("SPEAK") === false) { return message.channel.send("Wow. Invite me to play music for you, yet I can't speak in the channel. You're more heartless than my owner. Give me the channel permission to speak and then come back and invite me."); }
message.member.voiceChannel.join();
return message.channel.send(`Now tuned into: ${message.member.voiceChannel}. Ready and awaiting orders!`);
};
exports.conf = {
enabled: true,
runIn: ["text"],
aliases: [],
permLevel: 0,
botPerms: [],
requiredFuncs: [],
};
exports.help = {
name: "join",
description: "Joins the VC that you are in.",
usage: "",
usageDelim: "",
};
Related
I'm coding a discord bot and I'm trying to make a kick command right now. I managed to find how to check if there's any mention in the command message with message.mentions.members.first() but I couldn't find anything to check if a specific argument is a mention.
Code I have so far:
module.exports = {
name: "kick",
category: "moderation",
permissions: ["KICK_MEMBERS"],
devOnly: false,
run: async ({client, message, args}) => {
if (args[0]){
if(message.mentions.members.first())
message.reply("yes ping thing")
else message.reply("``" + args[0] + "`` isn't a mention. Please mention someone to kick.")
}
else
message.reply("Please specify who you want to kick: g!kick #user123")
}
}
I looked at the DJS guide but couldn't find how.
MessageMentions has a USERS_PATTERN property that contains the regular expression that matches the user mentions (like <#!813230572179619871>). You can use it with String#match, or RegExp#test() to check if your argument matches the pattern.
Here is an example using String#match:
// make sure to import MessageMentions
const { MessageMentions } = require('discord.js')
module.exports = {
name: 'kick',
category: 'moderation',
permissions: ['KICK_MEMBERS'],
devOnly: false,
run: async ({ client, message, args }) => {
if (!args[0])
return message.reply('Please specify who you want to kick: `g!kick #user123`')
// returns null if args[0] is not a mention, an array otherwise
let isMention = args[0].match(MessageMentions.USERS_PATTERN)
if (!isMention)
return message.reply(`First argument (_\`${args[0]}\`_) needs to be a member: \`g!kick #user123\``)
// kick the member
let member = message.mentions.members.first()
if (!member.kickable)
return message.reply(`You can't kick ${member}`)
try {
await member.kick()
message.reply('yes ping thing')
} catch (err) {
console.log(err)
message.reply('There was an error')
}
}
}
I'm coding a moderation bot in Discord.js v13, and for the mute, kick, warn and ban commands I need to check user permissions. However, my code fails to compile: "Cannot find name 'member'". I've checked the documentation and I've used the exact same method shown.
Here is my code, the error is in line 67. I've written this in TypeScript, however this should not have any effect on the error.
import { User, Permissions } from "discord.js";
import { SlashCommandBuilder } from "#discordjs/builders";
import mongoose from "mongoose";
import punishmentSchema from "./schemas/punishment-schema";
const config = require("./config.json");
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MEMBERS
]
});
client.on("ready", async () => {
console.log("Ready for your orders, boss!");
await mongoose.connect(`${config.mongodb_uri}`, {
keepAlive: true,
});
const guildId = "868810640913989653";
const guild = client.guilds.cache.get(guildId);
let commands;
if(guild) {
commands = guild.commands;
} else {
commands = client.application?.commands;
}
commands?.create({
name: "ping",
description: "Shows the bot's latency.",
});
commands?.create({
name: "help",
description: "Lists all commands and shows their usage.",
});
commands?.create({
name: "mute",
description: "Allows moderators to mute users.",
});
commands?.create({
name: "ban",
description: "Allows moderators to ban users.",
});
});
client.on("interactionCreate", async (interaction) => {
if(!interaction.isCommand()) return;
const { commandName, options } = interaction;
if(commandName === "ping") {
interaction.reply({
content: `Current latency: \`\`${client.ws.ping}ms\`\``,
ephemeral: false,
})
} else if(commandName === "help") {
interaction.reply({
content: `**__List of available commands:__**\n\n**/ping** Shows the bot's latency.\n**/help** Guess what this one does :thinking:\n**/cases** Allows non-moderators to check their punishment history.\n\n**/warn {user} [reason]** Applies a warning to a user. :shield:\n**/kick {user} [reason]** Kicks a user from the server. :shield:\n**/mute {user} [duration] [reason]** Kicks a user from the server. :shield:\n**/mute {user} [duration] [reason]** Mutes a user. :shield:\n**/ban {user} [duration] [reason]** Bans a user from the server. :shield:\n**/history {user}** Shows the punishment history of a user. :shield:`,
ephemeral: false,
})
} else if(commandName === "mute") {
if (member.permissions.has(Permissions.FLAGS.KICK_MEMBERS)) {
console.log('test if kick');
}
} else if(commandName === "mute") {
}
});
client.login(config.token);```
According to docs, interaction object, should have member property. Try to get it with interaction.member or const { commandName, options, member } = interaction;
Hello I am having a bit of an issue with my Discord ticketing system and it is making me really confused... It does not work when it creates the channel of the ticketing system and it is really confusing me...
code:
Open ticket command:
const Discord = require('discord.js')
module.exports = {
name: 'ticket',
description: "opens a ticket",
execute(message, args){
const user = message.author.id;
const name = "ticket-" + user;
if(message.guild.channels.cache.find(ch => ch.name == name)){
message.channel.send("You have already opened a ticket!")
}else{
message.guild.channels.create(name).then((chan)=>{
chan.updateOverwrite(message.guild.roles.everyone, {
SEND_MESSAGES: false,
VIEW_CHANNEL: false
})
chan.updateOverwrite(user,{
SEND_MESSAGES: true,
VIEW_CHANNEL: true
})
message.channel.send("I have created a ticket for you! Please go to the channel at the top of the server, Only you will have access to it and it will be called ticket(id)");
chan.send("Support will be here shortly").then((m)=>{
m.pin()
})
})
}
}
}
close ticket command (this is were the error is):
const Discord = require('discord.js');
module.exports = {
name: 'endticket',
description: "ends the ticket",
execute(client, message, args){
if(!message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("Only a moderator can end a ticket!")
if(message.member.hasPermission("ADMINISTRATOR")) message.channnel.delete()
}
}
console error:
TypeError: Cannot read property 'delete' of undefined
I dont understand what is going wrong...
thank you.
when there are lot of songs in my queue, my bot crashes if I ran the queue command.
and it gives me this error:
DiscordAPIError: Invalid Form Body
embed.description: Must be 2048 or fewer in length.
DiscordAPIError: Invalid Form Body
embed.description: Must be 2048 or fewer in length
I asked someone, and he told me that the embed for the queue is too long.
I want my bot to limit the queue that can be seen or split the embed into two or more pages if the queue is too long.
can someone help me to do that?
and here's my code:
async function handleVideo(video, message, voiceChannel, playlist = false) {
const serverQueue = queue.get(message.guild.id);
const song = {
id: video.id,
title: Util.escapeMarkdown(video.title),
url: `https://www.youtube.com/watch?v=${video.id}`
};
if (!serverQueue) {
const queueConstruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: process.env.VOLUME,
playing: true,
loop: false
};
queue.set(message.guild.id, queueConstruct);
queueConstruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueConstruct.connection = connection;
play(message.guild, queueConstruct.songs[0]);
} catch (error) {
console.error(
`[ERROR] I could not join the voice channel, because: ${error}`
);
queue.delete(message.guild.id);
return message.channel.send(
`I could not join the voice channel, because: **\`${error}\`**`
);
}
} else {
serverQueue.songs.push(song);
if (playlist) return;
else
return message.channel.send(
`✅ **|** **\`${song.title}\`** has been added to the queue! 😉`
);
}
return;
}
I am making a discord bot that is being activated by voice recognition, im at the very beginning right now im making him join a voice channel (which is working), and im trying to make a command to make him leave.
const commando = require('discord.js-commando');
class LeaveChannelCommand extends commando.Command
{
constructor(client){!
super(client,{
name: 'leave',
group: 'music',
memberName: 'leave',
description: 'leaves a voice channel'
});
}
async run(message, args)
{
if(message.guild.voiceConnection)
{
message.guild.voiceConnection.disconnect();
}
else
{
message.channel.sendMessage("seccessfully left")
}
}
}
module.exports = LeaveChannelCommand;
right now you can type !leave from anywhere in the server and the bot leaves,
i want to make it possible to control him only from the same voice channel,
what should i do
It's fairly easy to do. All you need to do is grab the bot's voiceChannel and the user's voiceChannel (if he is in one) and check if they are the same.
Below you can find some example code. Give it a try and let me know how it goes.
async run(message, args)
{
// If the client isn't in a voiceChannel, don't execute any other code
if(!message.guild.voiceConnection)
{
return;
}
// Get the user's voiceChannel (if he is in one)
let userVoiceChannel = message.member.voiceChannel;
// Return from the code if the user isn't in a voiceChannel
if (!userVoiceChannel) {
return;
}
// Get the client's voiceConnection
let clientVoiceConnection = message.guild.voiceConnection;
// Compare the voiceChannels
if (userVoiceChannel === clientVoiceConnection.channel) {
// The client and user are in the same voiceChannel, the client can disconnect
clientVoiceConnection.disconnect();
message.channel.send('Client has disconnected!');
} else {
// The client and user are NOT in the same voiceChannel
message.channel.send('You can only execute this command if you share the same voiceChannel as the client!');
}
}