I want to have a command which is similar to "say" command but it's only restricted to bot owner and only sent in one channel at a specific server. Any tips?
Here's my code (not working atm):
client.on("message", (message) => {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === "+ubersay") {
if(message.author.id !== process.env.ownerID) return;
const sayMessage = args.join(" ");
message.delete().catch(O_o=>{});
client.channels.get(process.env.specifiedChannel).send(sayMessage);
}
});
The code should work flawlessly, the only thing that could be a problem is your const args = message.content.slice(prefix.length).trim().split(/ +/g); combined with if(command === "+ubersay") { Because this askes for your Command to be used in the format [prefix]+ubersay so if your prefix is + you would need to do ++ubersay
Related
When I have this code here:
client.on("message", msg => {
const args = msg.content.trim().split(/ +/g);
let second = args[1];
let x = Math.floor((Math.random() * second) + 1);
if(msg.content === "random") {
msg.channel.send(`${x}`)
console.log(x)
console.log(second)
}
})
I need to send "random 10 (example)" twice and it still doesn't work cause it uses the "random" as the input for "second¨ how do i make this work?
Since "msg" is a string, you need to parse it as an integer before preforming an operation on it. I recommend replacing the "let x" line with the following:
let x = Math.floor((Math.random() * parseInt(second)) + 1);
parseInt is an internal function that takes a string with number contents and turns it into an integer.
const triggerWords = ['random', 'Random'];
client.on("message", msg => {
if (msg.author.bot) return false;
const args = msg.content.trim().split(/ +/g);
let second = args[1];
let x = Math.floor((Math.random() * parseInt(second)) + 1);
triggerWords.forEach((word) => {
if (msg.content.includes(word)) {
msg.reply(`${x}`);
}
});
});
To add on to quandale dingle's answer, what you can do with your bot is to create a command handler, which will make the developing process much easier and will look nice. If you don't really want to create a command handler, you can also use the following code:
if (message.content.startsWith(prefix)){
//set a prefix before, and include this if statement in the on message
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const commandName = args.shift().toLowerCase()
//use if statements for each command, make sure they are nested inside the startsWith statement
}
If you want to create a command handler, there's some sample code below. It works on discord.js v13, but I've no idea if it will work in v12. This is a more advanced command handler.
/*looks for commands in messages & according dependencies
or command handler in more simple terms*/
const fs = require("fs");
const prefix = "+"; //or anything that's to your liking
client.commands = new Discord.Collection();
fs.readdirSync("./commands/").forEach((dir) => {
const commandFiles = fs.readdirSync(`./commands/${dir}/`).filter((file) =>
file.endsWith(".js")
); //subfolders, like ./commands/helpful/help.js
//alternatively... for a simpler command handler
fs.readdirSync("./commands/").filter((file) =>
file.endsWith(".js")
);
for (const file of commandFiles) {
const commandName = file.split(".")[0]
const command = require(`./commands/${file}`);
client.commands.set(commandName, command);
}
});
//looks for commands in messages
client.on("messageCreate", message => {
if (message.author == client.user) return;
if (message.content.startsWith(prefix)){
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const commandName = args.shift().toLowerCase()
const command = client.commands.get(commandName)
if (!command) return;
command.run(client, message, args)
}
});
I know some basic python and I decided to try my hand at making a discord bot, but failed to get the bot to respond to a command. I tried changing the '-ping' to 'ping' and tried typing on my discord server:
ping
-ping
but neither did anything. Furthermore,
console.log('This does not run');
does not show up in console.
I'm not quite sure where I wrong. Any help would be appreciated. Thanks!
const {Client, Intents} = require('discord.js');
const {token} = require('./config.json');
const client = new Client({intents:[Intents.FLAGS.GUILDS]});
const prefix = '-';
client.once('ready', () => {
console.log('Bot works');
});
client.on('message', message => {
console.log('This does not run');
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');
}
});
client.login(token);
I think you need the GUILD_MESSAGES intent enabled. You can add it to the list of intents in the client constructor, like so:
const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
const Discord = require('discord.js');
const client = new Discord.Client()
const prefix = '+';
client.once('ready' , () => {
console.log('MemeStrifer is online')
});
client.on('message', message => {
if(message.content.startsWith(prefix) ||message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/+/);
const command = args.shift().toLowerCase();
if(command === 'blackhumour'){
message.channel.send('What is the difference between a kid and a bowling ball? when you put your fingers in one it doesnt scream');
} else if (command === 'sex') {
message.channel.send('no you f***ing pervert');
}
});
client.login('***');
Sorry if I am making any stupid errors, I just started yesterday by a YouTube video.
here,try this:
client.on('message', message =>{
if(message.content.startsWith(prefix) ||message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/+/);
const command = args.shift().toLowerCase();
if(command === 'blackhumour'){
message.channel.send('What is the difference between a kid and a bowling ball? when you put your fingers in one it doesnt scream');
} else if(command === 'sex'){
message.channel.send('no you fucking pervert');
}
});
Working on making a bot for a personal discord server, wanting it to simply join general voicechat when command "Blyat" is detected, play mp3 file, when its done to leave. Code:
var Discord = require('discord.js');
var client = new Discord.Client();
var isReady = true;
client.on('message', message => {
if (command === "Blyat") {
var VC = message.member.voiceChannel;
if (!VC) return message.reply("MESSAGE IF NOT IN A VOICE CHANNEL")
VC.join()
.then(connection => {
const dispatcher = connection.playFile('C:\Users\Wyatt\Music\soviet-anthem.mp3');
dispatcher.on("finish", end => { VC.leave() });
})
.catch(console.error);
};
});
client.login('token, not putting in code, is there in real code');
Error: "ReferenceError: command is not defined
at Client. (C:\Users\jeffofBread\Desktop\Discord Bot\main.js:6:5)"
Any help or tips will be much appreciated, haven't coded in many years and have lost any knowledge I had once held.
Your problem comes from an unidentified variable. Fortunatly that is very easy to fix. All you have to do is define command before you call it.
For this we'll splice the message content into two parts. The command and any arguments that may be included like mentions or other words. We do that with:
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
Note: The args constant is an array.
Which would make your entire onMessage event look like this:
client.on('message', message => {
const prefix = "!";
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === "blyat") {
var VC = message.member.voice.channel;
if (!VC) return message.reply("MESSAGE IF NOT IN A VOICE CHANNEL")
VC.join()
.then(connection => {
const dispatcher = connection.play('C:\Users\Wyatt\Music\soviet-anthem.mp3');
dispatcher.on("finish", end => { VC.leave() });
})
.catch(console.error);
};
});
I'm using node.js and visual studio
the bot is seemingly not working but does respond to the node . command but doesn't respond to the -ping command and I can find out why, is it spelling or updated code or am I just stupid please help.
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
client.once('ready', () => {
console.log('sirjunkbot is awake');
});
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('123');
}
});
What does the console say?
Also you dont have client.login()