The entire command works fine, when executed it shows what expected, however it can be executed in any channel when it should only be executable within a specific channel.
Ive tried basically everything that popped into my mind
const Discord = require('discord.js');
const guilds = require('../data/guilds.json');
module.exports.run = async (bot, message, args) => {
if (!message.channel.id === guilds[message.guild.id].botChannelID)
return;
var img
if (args[0] == 'aea') {
img = bot.utils.randomSelection([
I'm really stupid and probably made a dumb mistake so if anyone could please help me with this that would be great
Try to replace:
if (!message.channel.id === guilds[message.guild.id].botChannelID)
To:
if (message.channel.id !== guilds[message.guild.id].botChannelID)
You used a „!“ in front of the message.channel.id although you should use it in your „===“.
Related
I am writing my first discord bot with help from a tutorial. I am stuck because my bot hasn't been able to respond to commands and I've checked the tutorials and my code many times over. Is there something I'm doing wrong?
const discord = require('discord.js');
const client = new discord.Client();
const prefix = '!';
client.once('ready' , () => {
console.log('Zach Is Bad is 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!');
}
});
client.login('redacted')
The only issue with your code is message.content.startswith(prefix).
It is startsWith, it's case sensitive.
if (!message.content.startsWith(prefix) || message.author.bot) return;
There is a typo at client.on('ready', () => ...), but this shouldn't be the cause of the error.
You wrote if (!message.content.startswith(prefix) || message.author.bot) return; but I'm pretty sure startwith must be startWith (caps are importants). Try modifying this, then restart your bot.
If it doesn't work put console.log("test OK") right before the ping command, restart and send !ping. If there is "test OK" in your bot logs, then the problem come from the ping command. If you doesn't see this log, try moving the console.log line before the if statement. This is a simple yet effective method to know where does a problem come from.
I am trying to make a bot that would add a role that is in the server when a person types !join choiceOutOfVariousRoles. I am currently using discord version 12. My error message is:
fn = fn.bind(thisArg);
Although trying various techniques I could not get the code to work.
const Discord = require('discord.js');
const client= new Discord.Client();
const token = process.env.DISCORD_BOT_SECRET
client.on('ready', () => {
console.log("I'm in");
console.log(client.user.username);
});
client.on('message', msg => {
if (msg.content.toLowerCase().startsWith("!join"))
{
var args = msg.content.toLowerCase().split(" ")
console.log(args)
if (args[1] === 'sullen')
{
msg.channel.send('You have successfully joined Sullen!')
const sullenRole = msg.guild.roles.cache.find('name','Sullen')
msg.member.addRole(role.id)
}
}
});
client.login(token)
**EDIT: Fixed what everyone was saying and all I need to do now Is update the permissions, (my friend has to do that because its not my bot) and I should be all good. Thanks everyone! :D
discord.js introduces breaking changes very frequently, and v12 is no exception. You need to make sure you find up-to-date code otherwise it won't work. GuildMember#addRole was moved to GuildMemberRoleManager#add, which means you must use msg.member.roles.add(sullenRole).
I am currently working on an announce command. I only have problem, when I run the command I get an error.
Code:
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
let channel = message.mentions.channels();
let announcement = args.slice(1).join(" ");
channel.send(announcement);
}
module.exports.help = {
name: "ann"
}
Error:
TypeError: message.mentions.channels is not a function
I hope someone can help me! :-)
channels() is not a thing.
And you also did not specify which channel from the mentions you want as there could be multiple at the same time, that's why you need to use first() to choose the first mentioned channel.
let channel = message.mentions.channels.first();
So it's my first ever time coding and i'm creating a discord bot. It's all been going fine until I try to run the bot.js file on commmand line (using "node bot.js")
But It just comes up with a bunch of errors.
My Code:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
});
client.login(auth.token);
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
Question: Will you re-post you error picture? When you click on it it says the page doesn't exist.
PLEASE READ ALL BEFORE MAKING CHANGES!
First (Bad) Guess: But without the picture, I would guess (and this is not a good guess) that it's because "client.login(auth.token)" isn't at the bottom. Another guess is that ".content ===" does nothing. You should try and remove ".content" to see if it then works.
Here is your code with just that change:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`)
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong')
}
});
client.login(auth.token);
Logging bot is ready: The is also some other things I think you should change, this changing "client.user.tag" to "client.user.username" to instead show the bot's username. Another thing is "msg.content" I'm pretty sure this does nothing, and should be changed to just "===", there are some other ones, but that's my favorite one because it's the least amount of characters and easiest to type.
Here is your code with all of these changes:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`)
});
client.on('message', msg => {
if (msg === "ping") {
msg.reply('pong')
}
});
client.login(auth.token);
Token (& patrik's answer): (No hate to patrik) What patrik says that putting your token in the actual script will help (It won't, and makes it easier to hack), now while I do this, I really don't care if my bot gets hacked, it's in one server. He/She also says that the token error means that discord.js can't get the token, this is a node.js error, not a discord.js error. You probably messed up on writing a piece of code, that is likely in "auth.json". You should probably re-run through your files before doing any of these changes.
A auth/config/token (token file) .json file should look like this:
{
"token":"TOKEN-HERE"
}
And then it should be used by doing
const auth|config|token = require(./auth|config|token.json);
client.login(auth.token);
I hope this helps with coding your bot!
It's because the node.js version is not updated to be compatible with the new discord.js version
First, install old version of discord.js in console type
npm i discord.js.old#11.6.4
In your script change
this:
const Discord = require('discord.js')
To:
const Discord = require('discord.js.old')
Glad to see you are into making bots too!
I would firstly suggest replacing all those "client" words with "bot"
The unexpected token might be because of that above mentioned thing or that your token is not just simply there.
Remove line:
client.login(auth.token);
and replace it with:
bot.login('YOUR-TOKEN-HERE');
You can check what if your token at the Discord Developer page
const Discord = require("discord.js");
const prefix = ("d");
const client = new Discord.Client ();
client.on("ready", async () => {
console.log(`${client.user.username} is now working online!`);
});
client.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
client.login("MY TOKEN");
When I try doing the command: "dhelp" that I've made and coded for example, it doesn't work. Why is this? People have said that I need a command handler and I think it's done but don't why it is still not working, why is this? Please help me, I really need help right now.
Well, to start off, it doesn't actually look like your code does anything regardless of what the value of command is. Perhaps using if/else statements would be a good place to start.
if (command === "help") {
/* some code here */
}
where /* some code here */ is the code for your bot.