Why does the purge command not work? (no errors) discord.js - javascript

Bot #1 (Eulogist Official Bot)
Bot #2 (Prosser Recoveries)
So here we have two of my bots. the purge command is:
const { MessageEmbed } = require("discord.js");
const config = require("../../config.json");
module.exports = {
config: {
name: "purge",
description: "Purges messages",
usage: " ",
category: "moderation",
accessableby: "Moderators",
aliases: ["clear", "prune"],
},
run: async (prosser, message, args) => {
message.delete();
let hrps = new MessageEmbed()
.setTitle(`**Command:** ${config["Bot_Info"].prefix}purge`)
.setDescription(
`**Aliases:** /prune, /clear\n**Description:** Delete a number of messages from a channel. (limit 100)\n**Usage:**\n${config["Bot_Info"].prefix}purge 20\n${config["Bot_Info"].prefix}bc`
)
.setColor();
let done = new MessageEmbed()
.setDescription(`Purged \`${args[0]}\` message(s). ✅`)
.setColor(`${config["Embed_Defaults"].EmbedColour}`);
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.reply("Doesn't look like you can do that");
if (!args[0]) return message.channel.send(hrps);
message.channel.bulkDelete(args[0]).then(() => {
message.channel
.send(done)
.then((msg) => msg.delete({ timeout: 1000 }));
});
},
};
These two bots have the same purge command but only one of the bots command works. (i've checked perms & invited to different servers).
Has anyone got a solution for this?

Fixed! All i done was moved the js file to a different command folder and it suddenly worked.

Related

TypeError: command.execute is not a function when catching an error. Also, cooldown doesn't work. On Discord.js

I've been working on a Discord bot for a while and have implemented a cooldown command for a while too. It never works, and today i decided that i will try to fix it.
At first, it kept sending me an error message saying TypeError: command.execute is not a function and an error message on the channel, so i just removed catch (err) so it wont send that annoying message. But of course, doing that is perhaps the equivalent of removing a scratched limb.
Now that more people uses my bot, i was trying to rework on the cooldown feature, which is located on ../events/guild/message.js and it goes like this:
require('dotenv').config();
const Discord = require('discord.js');
const cooldowns = new Map();
module.exports = async (Discord, client, message) => {
if (message.author.bot) return;
const prefix = message.content.includes("nabe ") ? "nabe " : "n!"
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.get(command) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if(message.channel.type === "dm")return message.channel.send("you can't use commands on dm")
if(cmd){
cmd.execute(client, message, args, Discord);
}else return
if(!cooldowns.has(command.name)){
cooldowns.set(command.name, new Discord.Collection());
}
const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown) * 1000;
if(time_stamps.has(message.author.id)){
const expiration_time = time_stamps.get(message.author.id) + cooldown_amount;
if(current_time < expiration_time){
const time_left = (expiration_time - current_time) / 1000;
return message.reply(`wait **${time_left.toFixed(1)}** more seconds to do ${command.name} again.`).then(msg => { msg.delete({ timeout: 7000 }) });
}
}
time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);
try{
command.execute(message, args, cmd, client, Discord);
} catch (err){
message.reply("There was an error trying to execute this command.");
console.log(err);
}
}
How it was implemented on each command files:
module.exports = {
info: {
name: "command name",
description: "command description",
cooldown: 30,
},
async execute(client, message, args, Discord) {
/*code here*/
}
By the way, i got most of this code from CodeLyon on youtube, and here's the sourcebin.
Everytime i executed a command it will return the TypeError: command.execute is not a function error and an error message on the channel. I am aware that some people said that command.execute does not exist, but it works on the tutorial video, and i don't know any alternatives. And it probably won't even fix the cooldown anyway.
I will definitely really appreciate it if anybody can find a solution.
NodeJS 16.13.0, NPM 8.1.0, DiscordJS 12.5.3, Heroku server.
I am aware that some people said that command.execute does not exist
And they would be right. As you can see in your defined command:
module.exports = {
info: {
name: "command name",
description: "command description",
cooldown: 30,
},
You did not appear to specify an execute function in the command body. If you want to run the command, you need to do that:
module.exports = {
info: {
name: "command name",
description: "command description",
cooldown: 30,
execute: (client, message, args, Discord) => {
// command logic goes here
}
},
P.S I believe the sourcebin tutorial that you linked also included an execute function:

Is there any way to delete more than 100 messages at once in Discord.js?

currently im making a Discord Bot but im having troubles, I don't know how to delete more than 100 messages in discord.js, since limit its 100, Im looking for functions that will help me with my issue, my current code its:
const Discord = require("discord.js");
const { Client, MessageEmbed } = require("discord.js");
module.exports = {
name: "purge",
alias: [],
devOnly: false,
guildOnly: true,
nsfwOnly: false,
botPermissions: [
"SEND_MESSAGES",
"ATTACH_FILES",
"VIEW_CHANNEL",
"MANAGE_MESSAGES",
],
userPermissions: ["SEND_MESSAGES", "MANAGE_MESSAGES"],
category: "Moderation",
usage: "clear (messages)",
description: "Deletes the specified amount of messages",
run: async (client, message, args) => {
let prefix = await client.prefix(message);
let member = message.mentions.members.first();
if (!message.guild.me.permissions.has("MANAGE_MESSAGES"))
return message.channel.send(
new Discord.MessageEmbed()
.setDescription(
`<:no:860978971771928647> ***I don't have the necessary permissions to do that, please use the command \`${prefix}setup\` to check all it's working correctly.***`
)
.setColor(16711680)
);
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send(
new Discord.MessageEmbed()
.setDescription(
"<:no:860978971771928647> ***You don't have the necessary permissions to do that.***"
)
.setColor(16711680)
);
if (member) {
const userMessages = (await messages).filter(
(m) => m.author.id === member.id
)
await message.channel.bulkDelete(userMessages)
message.channel.send(new Discord.MessageEmbed()
.setDescription(`<:si:860978510079852544> ***${member} messages has been cleared.***`)
.setColor('GREEN')
)
} else {
let mensajes = parseInt(args[0]);
if (!mensajes)
return message.channel.send(
new Discord.MessageEmbed()
.setDescription(
"<:no:860978971771928647> ***Please provide a amount of messages to delete, it needs to be a number***"
)
.setColor(16711680)
);
if (mensajes < 1)
return message.channel.send(
new Discord.MessageEmbed()
.setDescription(
"<:no:860978971771928647> ***The arguments provided are not correctly, the amount of messages must be greater than 1***"
)
.setColor(16711680)
);
let embedlimitmessages = new Discord.MessageEmbed()
.setDescription(
"<:no:860978971771928647> ***You can't delete more than 100 messages**"
)
.setColor(16711680);
if (mensajes > 100) return message.channel.send(embedlimitmessages);
message.delete();
message.channel.bulkDelete(mensajes);
let embed = new Discord.MessageEmbed()
.setDescription(
`<:si:860978510079852544> ***I deleted ${mensajes} messages successfully!***`
)
.setColor("GREEN");
message.channel.send(embed).then((m) => m.delete({ timeout: 10000 }));
}
},
};
I was reading in other forums for how to do it, also I looked up in Discord Help Servers, and they say with a for or a while i can do that to don't make API abuse, i would appreciate you guys can give me an example
NOTE: you should still keep a max amount on this
Discord.TextChannel.prototype._bulkDelete = async function(amount) {
if(amount > MAX_HERE) return;
for (let i = amount; i > 0; i-=100) {
if (i > 100) {
this.bulkDelete(100);
} else {
this.bulkDelete(i)
}
}
}
//you can now call the function on a text channel and put more than 100 if your new cap is higher
This code is creating an _bulkDelete method for all Discord text channels. I did not test this however, so if you get any errors, just tell me!
Notes: Replace MAX_HERE with your new cap. You may also get ratelimited. Maybe you want to add a "sleep" function to avoid being ratelimited.

invite Created event

so I'm making a Discord Bot, with auto logging capabilities, until now I managed due to do most of the code, these include the command for setting a mod-log channel and the event code, here's the code for the command:
let db = require(`quick.db`)
module.exports = {
name: "modlog",
aliases: ["modlogs", "log", "logs"],
description: "Change the modlogs channel of the current server",
permission: "MANAGE_CHANNELS",
usage: "modlog #[channel_name]",
run: async (client, message, args) => {
if(!message.member.hasPermission(`MANAGE_CHANNELS`)) {
return message.channel.send(`:x: You do not have permission to use this command!`)
} else {
let channel = message.mentions.channels.first();
if(!channel) return message.channel.send(`:x: Please specify a channel to make it as the modlogs!`)
await db.set(`modlog_${message.guild.id}`, channel)
message.channel.send(`Set **${channel}** as the server's modlog channel!`)
}
}
}
And the inviteCreate event:
client.on("inviteCreate", async invite => {
const log = client.channels.cache.get(`${`modlog_${message.guild.id}`}`)
let inviteCreated = new Discord.MessageEmbed()
log.send(`An invite has been created on this server!`)
})
The issue is that since the inviteCreate event only accepts one parameter (I think that's what they are called) which is invite, there is no message parameter, so message becomes undefined, does anyone have a solution?
Thanks!
You don't need message in this case. See the documentation of invite.
Your message.member can be replaced with invite.inviter
Your message.channel can be replaced with invite.channel
Your message.guild can be replaced with invite.guild
Invites also have a guild property, that's the guild the invite is for, so you can use that instead of the message.guild:
client.on('inviteCreate', async (invite) => {
const log = client.channels.cache.get(`${`modlog_${invite.guild.id}`}`);
let inviteCreated = new Discord.MessageEmbed();
log.send(`An invite has been created on this server!`);
});

Cannot read property 'xy' of undefined in Discord.js

My lock command is not working for some reason. It was working before and recently it has been annoying me. A terminal picture is below.
My code was working a month ago and now recently it has been acting up every time I add new code. I have compared my code from a month ago, only new code that I wrote was added.
const Discord = module.require("discord.js");
const fs =require("fs");
module.exports = {
name: "Timed Lockdown",
description: "Start a timed lockdown in a channel.",
run: async(client, message, args) => {
const time = args.join(" ");
if (!time) {
return message.channel.send("Enter a valid time period in `Seconds`, `Minutes` or `Hours`")
}
if (!message.member.hasPermission("MANAGE_SERVER", "MANAGE_CHANNELS")) {
return message.channel.send(`You don't have enough Permisions`)
}
message.channel.overwritePermissions([
{
id: message.guild.id,
deny : ['SEND_MESSAGES'],
},
],);
const embed = new Discord.MessageEmbed()
.setTitle("Channel Updates")
.setDescription(`${message.channel} has been locked for **${time}**`)
.setColor("RANDOM");
message.channel.send(embed)
let time1 = (`${time}`)
setTimeout(function(){
message.channel.overwritePermissions([
{
id: message.guild.id,
null: ['SEND_MESSAGES'],
},
],);
const embed2 = new Discord.MessageEmbed()
.setTitle("Channel Updates")
.setDescription(`Locked has been lifted in ${message.channel}`)
.setColor("RANDOM");
message.channel.send(embed2);
}, ms(time1));
message.delete();
}
}
You only have a run method in the object you're exporting from lock.js and you're calling execute in main.js.
You either need to update the method name in lock.js like this (and leave main.js as is:
module.exports = {
name: "Timed Lockdown",
description: "Start a timed lockdown in a channel.",
execute: async(client, message, args) => {
const time = args.join(" ");
// ... rest of code
Or call the run method in main.js like this:
if (command === "lock") {
client.commands.get("lock").run(message, args);
}

Cannot destructure property "commands" of message.bot as it is undefined (discord.js)

So, I was switching to module.exports for my bot and for the help command I got the error: "Cannot destructure property "commands" of message.bot as it is undefined"
I never experienced the error before so I don't know how to fix it.
Also I copied the advanced command handler code from the official discord.js guide and it still does not work.
const Discord = require('discord.js')
module.exports = {
name: 'help',
description: 'List all of my commands or info about a specific command.',
aliases: ['commands'],
usage: '!help | !help <command name>',
cooldown: 1,
async execute(message, args, bot) {
const data = []
const { commands } = message.bot;
if(!args[1]){
let embed = new Discord.MessageEmbed()
.setTitle('Commands')
.addField('Fun 🎲', Wide)
.addField('Games 🎮', funay)
.addField('Information 📙', inf)
.addField(`Images 🖼`, imagess)
.addField('Moderation 👩‍⚖️', mod)
.addField('Giveaway🎉', gib)
.addField('Other', other)
.setColor('RANDOM')
.setThumbnail(message.author.displayAvatarURL())
message.channel.send(embed)
} else {
const name = args[1].toLowerCase()
const cmd = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name));
if (!cmd) {
return message.reply('that\'s not a valid command!');
}
const embedd = new Discord.MessageEmbed()
.setTitle(`Name: ${cmd.name}`)
.addField('Aliases', `${cmd.aliases.join(", ")}`)
.addField('Description', `${cmd.description}`)
.addField('Usage', data.push `${cmd.usage}`)
.setFooter(`Cooldown: ${cmd.cooldown || 3} second(s)`)
.setColor("RANDOM")
message.channel.send(embedd);
}
},
}
Try to change message.bot to message.client

Categories

Resources