How do i take a user's message as a variable? - node.js

I am trying to make a discord bot where I made prefix as "!e" and after that I wanted it to be a variable of anything...
How do I do that?
client.on("message", (message) => {
var botmessage = usermessage
if (message.content === `${prefix} ${usermessage}`) {
var usermessage = botmessage
message.channel.send(`:${botmessage}:`)
}
})

The solution is substr logic
client.on("message", (message) => {
if (message.content.startsWith('!e ')) {
var usermessage = message.content.substr(3)
var botmessage="whatever"
message.channel.send(`:${botmessage}:`)
}
})

Related

tag a user using discord node js

I want to make a command that tags a user when there name got mentoined. But now the bot only tags the username and not a nickname the person has in the server
client.on("message", message => {
if(message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
})
client.on("message", message => {
const list = message.guild;
list.members.cache.each(member => {
pinging = member.user.id
console.log(member.user.username)
if(message.content.includes(member.user.username)){
message.channel.send(`<#${pinging}>`)
}
})
})
can anyone help?
First thing, you only want to have one listener for the message event, so try this code:
client.on("message", message => {
if (message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
// insert choices from below here
})
If you want it to listen for a member being tagged
const mentionedMember = message.mentions.members.first()
if (mentionedMember){
message.channel.send(`${mentionedMember}`)
}
Now if you want it to listen for username instead of #username
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.user.username.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})
To listen for displayName
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.displayName.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})

Discord.js Multiple Commands

how can i make multiple commands with the discord.js module?
code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', msg => {
if (msg.content === '!hello') {
msg.reply('Hello!!');
}
});
client.login('token');
So, how do i make it so I can use multiple commands? Like !hi and !whatsup.
I hope someone can help.
Thanks
You can resume your if statement with else if()
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', msg => {
if (msg.content === '!hello') {
msg.reply('Hello!!');
} else if(msg.content === "!hi") {
msg.reply("Hi there!!");
}
});
client.login('token');
as a temporary solution, you can use "else if()" like so:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', message => {
if(message.content === '!command1') {
message.channel.send('working!');
} else if(message.content === '!command2') {
message.channel.send('working!');
} else if(message.content === '!command3') {
message.channel.send('working');
}
});
client.login('Your Token goes here');
a better solution is to set up a command handler. there are a lot on youtube, one is: https://youtu.be/AUOb9_aAk7U
hope this helped!
its really easy
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', async message => {
if(message.content.toLowerCase().startsWith("hello")) {
message.channel.send("hola")
}
if(message.content.startsWith("hi there")) {
message.channel.send("hello")
}
})
You can use if() as a temporary solution but it would be better to setup a command handler.
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', async message => {
if(message.content.toLowerCase().startsWith("command1")) {
message.channel.send("...")
}
if(message.content.toLowerCase().startsWith("command2")) {
message.channel.send("...)
}
})

Nodejs change the bot's nickname every time the variable change DISOCRD.JS

I'm doing a dominance ticker for bitcoin and I want to show the price in the nickname of the bot, the thing is that I don't know why but I can't change the nickname, it throws me an error.
const { Client } = require('discord.js');
const client = new Client();
const request = require("request");
var LastBitcoinDominance = 100;
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
})
myFunction()
function myFunction() {
const url = "API";
request(url, (err, res, body) => {
const data = JSON.parse(body);
var bitcoinDominance = data.bitcoin_dominance_percentage;
if (bitcoinDominance == LastBitcoinDominance){
console.log("Bitcoin dominance and LastBitcoinDominacne are the same")
}else {
client.user.setUsername(bitcoinDominance)
LastBitcoinDominance = bitcoinDominance;
}
});
setTimeout(myFunction, 10000);
}
client.login(TOKEN)
I think it is because client could not be detected. Because null means literally nothing.
Try using:
client.login(TOKEN);
instead of:
client.token(TOKEN);
Edit:
Try this:
const { Client } = require('discord.js');
const client = new Client();
const request = require("request");
var LastBitcoinDominance = 100;
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
})
myFunction(client)
function myFunction(client) {
const url = "API";
request(url, (err, res, body) => {
const data = JSON.parse(body);
var bitcoinDominance = data.bitcoin_dominance_percentage;
if (bitcoinDominance == LastBitcoinDominance){
console.log("Bitcoin dominance and LastBitcoinDominacne are the same")
}else {
client.user.setUsername(bitcoinDominance);
LastBitcoinDominance = bitcoinDominance;
}
});
setTimeout(myFunction, 10000);
}
client.login(TOKEN)
I added a parameter to your function myFunction, because you cannot use your client inside a function. Therefor you provide it for the parameter client. Inside the function the parameter client is now your client, because you provided it.
make sure the bot has permissions.
i tried running this and i didn't get any errors:

Discord.js -- message is not defined

const client = new Discord.Client();
const ayarlar = require('./ayarlar.json');
var prefix = ayarlar.prefix;
client.on('ready', () => {
console.log(`Botun olan ${client.user.tag}sunucuya giriş yaptı ve artık aktif!`);
});
client.on('message', msg => {
if (msg.content.toLowerCase() === 'salak aziz') {
msg.channel.send('nerden bildin? helal olsun');
}
if (message.content === `${prefix}mal`) {
message.channel.send('Aziz Mert');
} else if (message.content === `${prefix}adam`) {
message.channel.send('Ata');
}
});
Im a beginner.
The error is 'ReferenceError: message is not defined' How can i fix it?
Thanks.
That is because you defined the parameter of the message event as msg not message
const client = new Discord.Client();
const ayarlar = require('./ayarlar.json');
var prefix = ayarlar.prefix;
client.on('ready', () => {
console.log(`Botun olan ${client.user.tag}sunucuya giriş yaptı ve artık aktif!`);
});
client.on('message', message => {
if (message.content.toLowerCase() === 'salak aziz') {
message.channel.send('nerden bildin? helal olsun');
}
if (message.content === `${prefix}mal`) {
message.channel.send('Aziz Mert');
} else if (message.content === `${prefix}adam`) {
message.channel.send('Ata');
}
});

How to change files in discord.js by using commands?

I tried to many things the thing. But I m not able to change the config file by using the command. I want to add channel id in that config
const fs = require('fs');
const config = require('../config.json')
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports.run = async (client, message, args) => {
let owner = process.env.OWNER.split(',')
if(!owner.includes(message.author.id)) {
return message.reply("This command is not made for everyone")
}
if (message.channel.type === "dm" || message.author.bot || message.author === client.user) return; // Checks if we're on DMs, or the Author is a Bot, or the Author is our Bot, stop.
fs.writeFile('./config.json', args[0], (err) => {
if (err) console.log(err)
})
message.channel.send("Done")
}
exports.help = {
name: "wtp",
category: "General",
description: "Add this channel into WTP",
usage: "wtp <channel_id>"
};```
Also you can do it like this, add/change variable of config and then save it.
const fs = require('fs');
const config = require('../config.json')
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports.run = async (client, message, args) => {
let owner = process.env.OWNER.split(',')
if(!owner.includes(message.author.id)) return message.reply("This command is not made for everyone")
if (message.channel.type === "dm" || message.author.bot || message.author === client.user) return; // Checks if we're on DMs, or the Author is a Bot, or the Author is our Bot, stop.
if (args.length === 0) return message.reply('Pls mention a channell')
let targetChannel = message.mentions.channels.first() || message.guild.channels.get(args[0])
if (!targetChannel) return message.reply('Cant fin`d channel')
config.channed_id = targetChannel.id
fs.writeFile('./config.json',JSON.stringify(config) , (err) => {
if (err) console.log(err);
message.channel.send("Done")
})
}
exports.help = {
name: "wtp",
category: "General",
description: "Add this channel into WTP",
usage: "wtp <channel_id>"
};

Resources