I just started making a Discord bot using Node.js in Visual Studio Code, but I’ve stumbled upon an issue that I haven’t been able to fix.
When I run, for example node deploy-commands.js
, the terminal writes a message, and it is usable right after. But, when I try to run the main file, node index.js
, the terminal writes a message that I expected, but is not usable until I make a new terminal or close VS Code and open it again.
Here is my code in index.js
:
const Discord = require("discord.js");
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS", "MESSAGE_CONTENT"],
partials: ["CHANNEL", "MESSAGE"]
});
const token = ("MTI1NDA2MDQzNzc2MzM5NTY1NQ.Gez0CQ.evKz7FEHh8jqVgyodbHKxy2QoFijgdMWSBniqI") // your bot token here
client.on('ready', async () => {
console.log(`Client has been initiated! ${client.user.username}`)
});
client.login(token)
client.on('messageCreate', async (message) => {
if (message.content.toLowerCase() === "test") { // if someone says 'test'
console.log("Test had been said")
message.channel.send("Test reply, but not to your message :)")
let role = message.guild.roles.cache.find(role => role.name === "python");
if (role) message.guild.members.cache.get(message.author.id).roles.add(role);
message.reply("I gave you the role!")
console.log("Role given!")
}
});
// -----------------------------
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
} else if (commandName === 'server') {
await interaction.reply('`Server name: ${interaction.guild.name}nTotal members: ${interaction.guild.memberCount}`');
} else if (commandName === 'user') {
await interaction.reply('Your tag: ${interaction.user.tag}nYour id: ${interaction.user.id}');
}
});
client.on('guildMemberAdd', (guildMember) => {
// commands here
});
I wanted to run more commands after running index.js (I run everything by saying node filename.js), but I can’t because it does not allow me to type.
frapsed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.