Help!, im creating a bot for discord and i put it a prefix, so the error comes when i want to use a command how "(a.richembed)" in discord in the terminal of VSC I get the error "ReferenceError: mesagge is not defined" and i dont know how to fix it, please help me!, heres my code
const Discord = require("discord.js");
const client = new Discord.Client();
let prefix = "a."
client.on("ready", () => {
console.log("Ready to give Atis");
});
client.on("message", (message) => {
if(message.content.startsWith("ATIS JTPH")) {
message.channel.send("Information for airport: JTPH--------- Information: Zulu--------- Time: .......... (eg 16:45:00z)--------- Active Runways: 09L, 09R, 27L, 27R--------- WInd: .....#... (eg 360#15)--------- Clouds:Clear--------- Visibility: >20KM--------- Remarks:None--------- Tower Frequency: 121.20--------- Ground Frequency: 121.30--------- Delivert Center: 121.40--------- Center Frequency: 121.50--------- Emergency Frequency 121.60.");
}
if(message.content.startsWith(prefix+ "Help!")){
message.channel.send("Sending the emergency services to you!")
}
if(mesagge.channel.send.startsWith(prefix,"richembed")){
const embed = new Discord.RichEmbed()
.setTitle("What i can do?")
.setAuthor(message.author.username, message.author.displayAvatarURL)
.setColor(0x00AE86)
.setDescription("Well, mainly I'm designed by my creator Carlos and he coded me to be able to give information to the pilots who need it.")
.setFooter("They will ask: Why is that bot there and why does it have those roles? well, very simple, if you say ATIS JTPH I will tell you a few things about the defined airport such as JTPH!", client.user.avatarURL)
.setImage(message.author.displayAvatarURL)
.setThumbnail(message.author.displayAvatarURL)
.setTimestamp()
.setURL("https://github.com/CraterMaik")
.addField("These texts are still configured by my creator, please be patient!.", true)
.addField("These texts are still configured by my creator, please be patient!", true)
.addBlankField(true)
.addField("These texts are still configured by my creator, please be patient!.", true);
message.channel.send(embed)
}
});
client.login("NjM1MTk5NzE1NTUzODM3MDg2.XbO1Sw.mappUgFXIYeqj5_FgYFQ30TBDto")
Its a typo you typed mesagge instead of message
Related
I am very new to coding and I am taking Colt Steele's web development bootcamp course online. The course has been great so far, however I think some portions are outdated.
When I am in node and enter .load .index.js, the terminal gets stuck in some sort of loop and continuously prints "const mongoose = require('mongoose')." I pasted my code below if that helps. Please let me know if you think you know my error, thank you!
const mongoose = require('mongoose');
mongoose.set('strictQuery', false);
mongoose.connect('mongodb://127.0.0.1:27017/movieApp')
.then(() => {
console.log("connection open!");
})
.catch(err => {
console.log("oh no, error!");
console.log(err);
})
const movieSchema = new mongoose.Schema( {
title: String,
year: Number,
score: Number,
rating: String
})
const Movie = mongoose.model('Movie', movieSchema);
I tried resaving the file, and attempting different ways to connect to the database. However, each time I still got the same loop. I am hoping for node to load my code and enter the REPL.
I found the answer in the question forum in the course I am taking. The TA posted this:
UPDATE (SOLUTION):
This seems like a bug with the newest versions of node with how it works with mongoose.
Instead of using .load index.js inside of the node shell, use the command
node -i -e "$(< index.js)" in the system terminal (outside of the node shell, just be sure that you first change directories into the folder containing the index.js file) — this will load the file and start the node with this one command instead, and then it should work.
I am trying to split the code for my discord.js bot into multiple js files for better organization and troubleshooting. When doing so, I chained my functions using module.exports linking multiple files together. Going from my index file to the first function file, everything works fine. Going to the second, I get a TypeError said above. I am not sure why this occurs but I think it may have to do with nesting multiple module.export commands?? Is there a solution/alternative to achieve this? I've looked at other questions on this site but I can't seem to find one with the same issue I am facing. Code below.
message.js
module.exports = {
name: 'message',
execute(client, message) {
const{ commandinit } = require(`./commandinit.js`);
if(message.content.startsWith("-") || !message.author.bot) {
var commandObj = message.content.slice(2);
command = commandObj.toLowerCase();
commandinit.execute(client, message, command);
}else{return;}},};
commandinit.js
module.exports = {
name: 'commandinit',
execute(client, message, command){
//do this stuff
},};
Sorry if this is a stupid question, as you can tell I am very new with this. Thanks in advance!
My problem is pretty weird but essential for my discord.js bot. I tried setting up a command handling process, but when a command needs to use a variable used in the config.json file, I need to add it in the command code, and index code every time, and it gets annoying and crammed.
Here's what I mean.
In index.js:
bot.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 (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, bot, Discord, prefix, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
} });
and for example, here's a command file:
module.exports = {
name: 'help',
descriprion: 'displays the help center',
execute(message, bot, Discord, prefix, args) {
if(!args[1]){
const helpEmbed = new Discord.MessageEmbed()
.setTitle('help center')
.setColor(0x348a58)
.setAuthor('froggo')
.setThumbnail('https://i.imgur.com/0RLKc78.jpg?1')
.setFooter('need more help? use ' + prefix + 'supportserver')
message.channel.send(helpEmbed);
}
}
}
Do I always need to include in both files (in the execute part) the variables that I need to use, or is there a more efficient way? Because then, in the index file, there would be something like:
execute(discord, name, member, prefix , crewmember, supporting, other-link bla bla bla)
instead of client.commands.get, you can use require.
Like this:
require(command).execute(message, bot, Discord, prefix, args)
You also could store your additional variables inside the client itself (discord in your case), message or anything else.
For example, let's say you need to get the prefix in another command, just before running your command with message as a parameter, do something like :
message.prefix = 'yourCoolPrefix';
and you will be able to access it inside the command itself ! :)
Sorry to disturb. I am programming a Slack robot to reply user message by using the API
In the morning, it works totally okay. Then after I returned back from my office it just shut down and show me this error
Jiatongs-MacBook-Pro:news-bot jiatongli$ node index.js
Assertion failed: token must be defined
/Users/jiatongli/Desktop/news-bot/node_modules/vow/lib/vow.js:105
throw e;
^
Error: not_authed
at _api.then.fail (/Users/jiatongli/Desktop/news-bot/node_modules/slackbots/index.js:46:33)
at Array.<anonymous> (/Users/jiatongli/Desktop/news-bot/node_modules/vow/lib/vow.js:773:56)
at callFns (/Users/jiatongli/Desktop/news-bot/node_modules/vow/lib/vow.js:24:35)
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at _api.then.fail (/Users/jiatongli/Desktop/news-bot/node_modules/slackbots/index.js:46:19)
at Array.<anonymous> (/Users/jiatongli/Desktop/news-bot/node_modules/vow/lib/vow.js:773:56)
at callFns (/Users/jiatongli/Desktop/news-bot/node_modules/vow/lib/vow.js:24:35)
at process._tickCallback (internal/process/next_tick.js:61:11)
Jiatongs-MacBook-Pro:news-bot jiatongli$
I personally have no idea what is going on because it seems like the program itself do not have bug. What i miss?
Here is the code in my index.js file:
var SlackBot = require("slackbots");
var request = require("request");
var NewsAPI = require("newsapi");
var unirest = require("unirest");
var API_KEY = process.env.API_KEY;
var Slack_API = process.env.Slack_API;
// create a bot
var bot = new SlackBot({
token: Slack_API,
name: "aloha-ai"
});
bot.on("message", msg => {
switch (msg.type) {
case "message":
// we only want to listen to direct messages that come from the user
if (msg.channel[0] === "D" && msg.bot_id === undefined) {
getRandomTechNews(postMessage, msg.user)
}
break
}
})
const postMessage = (message, user) => {
bot.postMessage(user, message, {
as_user: true
});
}
const getRandomTechNews = (callback, user) => {
unirest.get("https://nuzzel-news-v1.p.rapidapi.com/news?count=10&q=product")
.header("X-RapidAPI-Host", "nuzzel-news-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", API_KEY)
.end(function (response) {
var newsJSON = response.body;
var news = "*Viral News* in product : \n\n\n\n";
for (i = 0; i < newsJSON.results.stories.length; i++) {
news += "_Excerpt:_ \n" + ">" + newsJSON.results.stories[i].excerpt + "\n"
news += "_Let's see the article!_ \n" + newsJSON.results.stories[i].url + "\n\n\n"
};
callback(news, user);
});
}
Your error message seems to indicate that your program is not authenticated with the Slack API: Error: not_authed
Since you are retrieving your API key and token from environment variables:
var API_KEY = process.env.API_KEY;
var Slack_API = process.env.Slack_API;
my guess is that you have started a new terminal session where you have not yet set that environment variable, or you are on a different computer where it is not set.
Before running your program, try exporting those variables from the command line:
export API_KEY=<my-api-key>
export Slack_API=<my-token>
If you have security concerns about your API keys showing up in your bash history you can do one of two things (these are examples of things that I do, but there are probably better, safer practices out there):
You can put an empty space before your command [space]export API_KEY=<my-api-key> instead of export API_KEY=<my-api-key>. This will make it so the command does not show up in your history.
You can put your export commands in a separate file called e.g., ~/.secrets and then run the command source ~/.secrets which will run your export commands.
Probably these will give you a sense of security rather than actual security though, since you can just echo the value of the environment variables, but I personally like taking one of these steps as an extra precaution.
i use this answer and can solve it.
Add a bot https://my.slack.com/services/new/bot and put the token
do you get token for your bot from above url:
did you set this ?
I want to set my discord bot status but I'm using discord.io no discord.js so I can't find some tutorial for me. I tried also watch for discord.js tutorials and try:
bot.user.setPresence({
status: 'online',
game: {
name: '/help'
}
});
Error: It crash after start node bot.js
Or:
bot.on('ready', () => {
bot.user.setGame('/help')
})
Error: Parsing error: Unexpected token )
Can anybody help me please?
I'm writting my code with Visual Studio Code. It seems to have a nice autocomplete feature, and by exploring it a little, I was able to find this tip :
bot.setPresence(
options : { idle_since: any; game: {name: string; type: number; url?: string;}}
): void
I'm not sure what everything means precisely, but most of it seems to be optional. I was for exemple able to change the bot's playing game with :
bot.on('ready', function (evt) {
bot.setPresence( {game: {name:"/help"}} );
});
var name = "with the boys. Built by Ayser & Khalid"
var type = 0;
var url = "https://www.twitch.tv/legendarysharkkk";
bot.setPresence({
game: {
name,
type,
url
}});