Discord.js Bots // Say Command - javascript

I've coded a "say command" that's supposed to execute a message every time I type in -say.
It's working fine, I just want the prefix to be set to "?" instead of "-" only for that one command, the other ones are supposed to be set to the main one ("-"). On top of that I want it to delete the command after typing it in, so all that remains is the message [(e.g ?say hello --> (delete "?say hello" --> send message "hello" to text channel)]. I also want to specify the text channel in my command, instead of just setting it to send the messages only to one specific channel [e.g -say (message) (text channel)] It would also be pretty cool if it said something like "Done." and deleting that confirmation after ~5 sec.
So here is the code:
client.on('message', function(message) {
if(message.author.bot) return;
else if(isValidCommand(message, "say")) {
let sendMessage = message.content.substring(4);
let sendChannel = client.channels.cache.get('767374258492932106');
sendChannel.send(sendMessage)
}
});
In the following I will show you my not working code, trying to set the prefix to "?" but it didnt execute, only saying "declaration or statement expecting and missing or wrong punctuation..
client.on('message', function(message) {
if (['?'].every((prefix) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
else if(isValidCommand(message, "say")) {
let sendMessage = message.content.substring(4);
let sendChannel = client.channels.cache.get('767374258492932106');
sendChannel.send(sendMessage)
}
});
It'd be really grateful if someone helped me with this. Thank you!

I am not sure what you trying to achieve with
if (['?'].every((prefix) => {
but this is what I would do
using args, substring and switch to identify the command.
The usage of the
client.on('message', async message =>{
if (message.author.bot) return;
if (message.content.startsWith(prefix)) {
let args = message.content.substring(prefix.length).split(" ");
switch (args[0].toLowerCase()){
case 'say': {
let sendMessage = message.content.substring(prefix.length +args[0].length+ args[1].length + 2); //2 is accounting for the 2 space between prefix and # and prefix and the main content
setTimeout(()=>{message.delete()},5000)
let sendChannel = client.channels.cache.get(args[1]);
sendChannel.send(sendMessage)
break;
}
}
}
if (message.content.startsWith(otherPrefix)) {
let args = message.content.substring(otherPrefix.length).split(" ");
switch (args[0].toLowerCase()){
case 'say': {
// Do something else
break;
}
}
}
});
Edit: The usage of the commend would be like
!say #generalTextChannel abc
where #generalTextChannel is tagging the channel
or
!say 767374258492932106 abc
where 767374258492932106 is the channel Id

I don't quite understand what are you trying to do with the prefixes.
When trying to detect a prefix in front of a command, you can just use the other line you used without ['?'].every
Use just this: if (!message.content.startsWith(prefix) || message.author.bot) return;.
Then check for each command individually under this. if (command == 'say')
A very simple way of getting arguments:
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
The say command would look something like this:
if(command == 'say'){
//then you could find the channel
//https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=fetch
client.channels.fetch('222109930545610754')
.then(channel => {
channel.send(args.join(" ")) //sending the arguments joined with a space in the fetched channel
.then(msg => {setTimeout(function(){msg.delete()},5000)}) //delete after 5 seconds, please check if delete is a function (I didn't)
})
.catch(console.error);
}
['?'].every got me thinking you could use if(!['?', '-'].some(prefix => content.startsWith(prefix))) return to use more prefixes instead of the if statement.

I don't know what your saying. But here is how I would do it.
const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = '!';
client.on('message', message => {
const args = message.content.slice(prefix.length).trim().split(/+ /);
const command = args.shift().toLowerCase();
if (command === 'say') {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const user = message.author;
if (!args[0]) {
user.send("Provide a word to say in the say command\nExample: !say Hello")
}
const say = args.join(" ");
message.channel.send(say)
message.delete()
}
})
client.login("PUT YOUR TOKEN HERE")

For the other prefix, you could do this:
client.on('message', async message => {
if(message.content === '?say') {
//the code (I don't know what is the code, I'm giving the way for the prefix)
} else {}
if(!message.content.startsWith(prefix) || message.author.bot) return
const args = message.content.slice(prefix.length).trim().split(/+ /);
const command = args.shift().toLowerCase();
//bla bla bla (the rest of the commands, etc.)
}

Related

I have issues making my bot copy the text then send it back

I use discord.js and I am making a bot that sends back the text that a user types but whatever I try I run into problems.
This is what I tried:
let s4 = (message.channel, message.content);
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command) {
messege.channel.send(s1 + s4);
}
});
It looks like your issue is in
const command = args.shift().toLowerCase();
if (command) {
messege.channel.send(s1 + s4);
}
you are putting an array into in if statement with nothing to turn it into a Boolean. Another issue you might have is that the command is a constant. I suggest replacing it with
let command = args.shift().toLowerCase();
messege.channel.send(s1 + s4);
Also, reading the comments, it looks like you referenced the message object before it was defined
In the future, please add the errors you are receiving to your question

discord.js ban/kick commands

basically all i need are simple commands that lets me type
<ban [user here]
or
<kick [user here]
and have have it kick/ban the refrenced user, i know this is probably the simplest thing in the world but i suck and im really new to discord bot coding and js in general so help me random people :D also this is probobly the stupidest thing in the world but i found another thing on this and it didnt work heres the code they tried:
if (msg.member.hasPermission("KICK_MEMBERS") {
if (msg.members.mentions.first()) {
try {
msg.members.mentions.first().kick();
} catch {
msg.reply("I do not have permissions to kick " + msg.members.mentions.first());
}else {
msg.reply("You do not have permissions to kick " + msg.members.mentions.first());
}
here is my code so far:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '<';
client.once('ready', () => {
console.log('Bot Online')
client.channels.cache.get('707840645192220714').send('Bot Online');
})
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping>') {
message.channel.send('pong!');
}
else if (command === 'test>') {
message.channel.send('Test Working');
}
else if (command === 'help>') {
message.channel.send('<Ping> <Test> <Help> <Youtube>')
}
else if (command === 'youtube>'){
message.channel.send('https://www.youtube.com/channel/UCFK-ry9dVqsPsjr638g1ygw')
}
else if (command === 'kick>'){
}
else (message.channel.send('That Command Isnt Reconised Use <Help> To View
A List Of Commands'))
})
client.login('Token Here');
also im not going to require it but if you want you could help me get the suffix system working without having to just shove it at the end of my commands
For the code you tried, it doesn't work as there is no members property on the Message object. I think what you want to try is msg.mentions.members.first() - this gives you the first valid mention as a GuildMember object. From here, you can use the kick() and ban() methods as you please.

Why are my Commands Only Working for 1 Person

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!`);
}
},
};

Discord.js How do i spacebar in command line?

discordjs version 11.4.2
I type !a hello in channel but bot don't sent message
if(command === "!a hello"){
const msg = await message.channel.send("Checking Command...")
msg.edit("hello");
}
First of all, you need to define 'command', but in your case you can change out command with msg.content === '!a hello'. You also don't have to define 'msg', although I suppose you could do that still.
You can do this:
bot.on("message", msg => {
if (msg.content === "!a hello") {
msg.channel.send("Checking Command...").then(msg => {
msg.edit(`hello`);
});
}
});
Tell me if you need anything or need to remove something or add something. Hope this helps :)
My guess is that you have defined "command" to something like this somewhere in your code
const args = message.content.split(/ +/g);
const command = args.shift().toLowerCase();
What's causing you this problem would be this part of it: ".split(/ +/g);"
This splits everything with a space in between into substrings. So how can you fix it now? Either use the command "!a-hello" or don't use "command"
if (message.content === "!a hello") {
const msg = await message.channel.send("Checking Command...")
msg.edit("hello");
};

Why doesn't my discord bot know its #Mention?

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.

Categories

Resources