The Issue
When executing my code i am really getting no errors at the moment but would like to add to a function but have tried almost every way of handling it. At this point the variable has been removed due to confusion and frustration.
What needs to happen is, the User that initiates the command, their message gets deleted after a short delay. I have tried message.delete(1000) and other Variants for v12 but no luck. Not able to pass the "message" variable from my active function?
Maybe i am completely off and just making it hard on myself, i don't know. Honestly embarrassing that i couldn't figure out a message.delete. Please help. Apologies for the ignorance.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "";
const PREFIX = "!";
const fs = require('fs');
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
bot.on('ready', () => {
console.log("The bot is active and ready to go!");
});
bot.on('message', function(message) {
if(message.content[0] === PREFIX) {
let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
}
});
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case "crash":
bot.commands.get('crash').execute(message, args);
break;
case "hello":
bot.commands.get('hello').execute(message, args);
break;
case "purge":
bot.commands.get('purge').execute(message, args);
break;
}
});
bot.login(token);
Here is an example of "crash.js" for reference. Don't know if i need to execute delete from there?
module.exports = {
name: 'crash',
description: "Crash",
execute(message, args){
message.author.send("Here is the link you requested");
}
}
You can execute delete from within your module. The message is passed as a full object so you just call the delete method on it. However, the Options are an Object which means it needs to be defined as such. For clarity, I'm going to use another variable but this can be done inline.
let options = {
timeout: 1000,
reason: 'Because I said so.'
}
message.delete(options);
or inline...
message.delete({timeout: 1000});
Related
I am very new to javascript and I don't understand a lot of the actual code.
However, I have one query, how can I make the "cahbResponses" appear in a MessageEmbed?
I've looked at the literature on MessageEmbeds from the Discord JS guides but I still have no clue.
The code I am working on is:
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
var args = message.content.substring('!'.length).split(' ');
switch (args[0].toLowerCase()) {
case 'cahb':
var response =
cahbResponses[Math.floor(Math.random() * cahbResponses.length)];
message.channel
.send(response)
.then()
.catch(console.error);
break;
default:
break;
}
});
First, make sure you are importing Discord
// you probably have this on the beginning of your file
const Discord = require("discord.js");
Then
var response = cahbResponses [Math.floor(Math.random()*cahbResponses .length)];
// Create a MessageEmbed object and set the description
const embed = new Discord.MessageEmbed().setDescription(response);
// Then send the embed
message.channel.send(embed);
Read other MessageEmbed methods and properties here: https://discord.js.org/#/docs/main/stable/class/MessageEmbed
I’m not sure what happened, I can’t seem to find anything in the code that would cause this, but suddenly only one person can use commands. Even commands set for certain permissions can’t be used by anyone except that person. The person it got stuck to is fairly new to the server too which is strange. It seems to work fine on our testing server but on the main one it’s only working for 1 person.
Below is my main file where I have the command handler
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const profanities = require('profanities/index.json');
var used = false;
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot Online!');
});
// This is the start of the main function when the bot is turned on
client.on('message', message => {
if (message.author.bot || !message.guild) return;
const words = message.content.toLowerCase();
if (words.includes('shalomi')) {
setTimeout(function() {
message.channel.send(`Shut up ${message.author}`);
}, 1500);
}
if (words.includes(' bum ')) {
setTimeout(function() {
message.channel.send('Are we talking about <#458068171241553921>?!');
}, 1500);
}
if (words == 'prefix') {
message.channel.send(`The current prefix is "${prefix}".`);
}
if (used) return;
else {
if (words == 'f') {
message.channel.send('F');
used = true;
setTimeout(() => {
used = false;
}, 1000 * 20);
}
}
for (let x = 0; x < profanities.length; x++) {
if (message.member.roles.some(role => role.id === '483641589193900043')) return;
else {
if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
message.channel.send('Oooooooh you said a bad word!');
client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
message.delete();
return;
}
}
}
// The bot will not respond if there is no prefix,
// the user that typed it was a bot,
// or if it was not sent from in the server
if (!message.content.startsWith(prefix) || message.author.bot || !message.guild) return;
// Creates the arguments variable and separates it with a space
// and creates the command variable
const args = message.content.slice(prefix.length).split(' ');
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName);
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
try {
command.execute(message, args);
}
catch (error) {
console.error(error);
message.channel.send('There was an error trying to execute that command!\nCheck the console for details.');
}
});
// This logs in the bot with the specified token found in config
client.login(token);
Sorry, couldn't format the code properly in comments. Try putting
if (!message.content.startsWith(prefix) || message.author.bot || !message.guild) return;
// Creates the arguments variable and separates it with a space
// and creates the command variable
const args = message.content.slice(prefix.length).split(' ');
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
All the way at the top, right after the if (message.author.bot || !message.guild) return; line.
So here is the most recent command I did which was done a couple days ago. When I first did this command, I forgot to put the comma and semi colon at the bottom (the ones that are there now) like I was supposed to. When I tested it in the normal server, the first person to use the command is now the only person that can use every command.
Would forgetting those punctuations be the reason why every command now only works for that person who used the "hug" command first? I've tried deleting that command and redoing it thinking it would delete information that could possibly be saved to a variable. Like maybe it's looking for that one author only since it never closed out of the loop? I'm not sure
module.exports = {
name: 'hug',
description: 'Used to hug everyone or mention a user to hug them specifically.',
execute(message, args) {
args = message.mentions.users.size;
if (!args) {
message.delete();
return message.channel.send(`${message.author} gives a big ol' hug to everyone!`);
}
else {
message.delete();
return message.channel.send(`${message.author} sends love to ${message.mentions.users.first()} with a hug!`);
}
},
};
I'm working on a discord bot (This is just the AI module, there's another one connecting to the same bot for commands and stuff), but it doesnt know its own #Mention.
const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();
// Set the client user's presence
client.on('ready', () => {
var prefix = client.user.tag;
console.log('PixelBot AI Loaded!');
console.log(prefix);
});
client.on('message', message => {
if (message.author.bot) return;
var prefix = + client.user.tag;
console.log(prefix);
if (message.content.substring(0, prefix.length) === prefix) {
//useful variables
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "help") {
console.info("Help Message Sent!\n");
};
};
});
client.login(token);
The above code prints out:
PixelBot AI Loaded!
PixelBot#9188
But does nothing when I send #PixelBot help. However, if I change the prefix to a string, eg: "pb", it works. It obviously knows it in "client.on('ready')", as it gets printed out. Any ideas?
EDIT: Here is the code with #Discord Expert's suggestion in:
const token = process.env.PixelBot_token;
const keep_alive = require('./keep_alive.js');
const sendReply = require('./sendReply.js');
const Discord = require('discord.js');
const client = new Discord.Client();
// Set the client user's presence
client.on('ready', () => {
console.log('PixelBot AI Loaded!');
});
client.on('message', message => {
if (message.author.bot) return;
if (message.mentions.users.first() === client.user) {
//useful variables
const command = message.content.slice(message.mentions.users.first().length).trim().split(/ +/g);
const identifier = command.shift().toLowerCase();
console.log('identifier = "' + identifier + '"');
console.log('command = "' + command +'"');
console.log('message content = "' + message.content + '"');
if (command === "help") {
console.info("Help Message Sent!\n");
};
};
});
client.login(token);
This prints out:
PixelBot AI Loaded!
identifier = "<#569971848322744320>"
command = "help"
message content = "<#569971848322744320> help"
So it knows that command === "help", so why does it not execute the if statement?
when people ping you on discord, an ID follows as with everything on discord,
So in reality is:
<#USERID>
I recommend exploring message.mentions.users.first()
and comparing it to your client.user
Alternatively message.mentions.users.first().id
and client.user.id
Best regards
Discord Expert
Ok, I'm answering my own post here because I found the problem. For some reason, using === instead of == stops it from working. I don't know why.
I'm still in the process of learning discord.js but I'm struggling with passing a discord message send function from one .js file to another. Below I stripped out all the complex stuff, and just left it with the barebones, in hopes someone can understand it, and provide guidance!
The idea is, (eventually) when there is a new update on 'true', it sends the message 'hello world' to all discord servers. Right now for debug purposes I have it using my developer discord channel.
Everything is ran via nf run npm start
Hello.js
const Discord = require('discord.js');
const client = new Discord.Client();
const disconfig = require("./config/default.json");
// new function
function message(message) {
const messageInfoEmbed = new Discord.RichEmbed()
.addField('Hey:')
.addField('Testing: ')
.addField('NoWorries: ')
return messageInfoEmbed
}
// Function OnUpdate() {
// if (updatedcontent === true) {
exports.wrapper = async(client, message) => {
client.channels.get("519344197078220804").send(message(message));
}
//}
//}
Server.js
const Discord = require('discord.js')
const client = new Discord.Client()
const disconfig = require("./config/default.json");
client.on("ready", () => {
client.user.setActivity(`Serving ${client.guilds.size} servers`);
var sayHello = require('./hello');
sayHello.wrapper(); // "Hello World! Message via Discord"
});
client.on("message", async(message) => {
if (message.author.bot) return;
if (message.content.indexOf(disconfig.prefix) !== 0) return;
const args =
message.content.slice(disconfig.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "test?") {
}
}
client.login(disconfig.token);
error
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'channels' of undefined
at Object.exports.wrapper (X:\xxx\xxx\xxx\xxx\app\hello.js:20:8)
In JavaScript (and pretty much every other programming language) variable declarations in inner scopes shadow (think override) declarations with the same names in an outer scope:
var foo = 3;
var bar = 5;
var f = (foo) => {
var bar = 1;
return [foo, bar];
};
var results = f(0);
console.log(results); // [0, 1]
N.B. you don't get [3, 5]. So in your code:
exports.wrapper = async(client, message) => {
means that the function expects to have client passed in. That parameter declaration shadows the client defined at the top of the file (meaning it's invisible inside the function). So when you call it in the other file like so
sayHello.wrapper();
with no arguments, kaboom.
Use message.guild instead. It should work.
I'm quite a newbie to node.js and I'm currently using discord.js to make a Discord bot. As soon as any bot command gets used the console prints a DeprecationWarning.
for example:
(node:15656) DeprecationWarning: Collection#find: pass a function instead
(node:15656) sometimes is another number, nearly always changing.
This is what my code looks like (only one command, I've got multiple, I get this error with all of them though):
const botconfig = require("./botconfig.json")
const Discord = require("discord.js");
const bot = new Discord.Client();
bot.on("ready", () => {
console.log(`Launched ${bot.user.username}...`);
bot.user.setActivity("Games", { type: "PLAYING" });
});
bot.on("message", async message => {
if (message.author.bot) return;
let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
let botico = bot.user.displayAvatarURL;
if (cmd == `${prefix}help`) {
let helpEmbed = new Discord.RichEmbed()
.addField(".kick", "kick a user", true)
.addField(".ban", "ban a user", true)
.addField(".unban", "unbans a user", true)
.addField(".mute", "mutes a user over a period of time", true)
.setColor("#005b5f")
.setThumbnail(botico);
message.channel.send(helpEmbed);
console.log(`command used: help`);
};
});
bot.login(botconfig.token)
It is in one of your other commands. You more than likely are using something like #Collection.find('name', 'keyname') in one of the other commands.
This has been updated to #Collection.find(x => x.name === "name").
Like it says in the error. #Collection.find() requires a function instead. So use one and the error goes away.