How do i make a discord bot reply to my command in a certain way - node.js

I have tried to write the conditional statement in various ways i found on the internet. But, the bot doesn’t seem to be recognizing my client id. What am I doing wrong?
Code:
if(msg.author === '<#8421258276382****>')
{
const yorepLKB = yoRepliesKB[Math.floor(Math.random() * yoRepliesKB.length)];
msg.reply(yorepLKB);
}
Here, the id inside the quotations is my discord id.

message#author returns a User class, which does not equal <#ID>. (Though if you send a User class in a message, Discord.JS will parse it as a mention.)
You can check the User's ID like this:
if (msg.author.id === "123456789012345678")
{
// Code
}

Related

Check if command is run in a server?

I am making a bot in discord js, but I wanna check if the command is run in a specific server. How could I do this?
I have tried searching the internet but I couldn't find anything except for checking if member has permission or role.
If you're using interactions (slash commands):
// Where `interaction` is your `Interaction` object
if (interaction.guildId === "your guild id here") {
// Your guild-specific logic here
}
Or if you're using message-based commands:
// Where `message` is your `Message` object
if (message.guildId === "your guild id here") {
// Your guild-specific logic here
}
Based if you want to use slash commands or not. I think, that you are new to discord.js, so I would recommend normal commands.
You can use message event to run function when someone sends the message. In the funcion, you get the message as 1st parameter. First you have to get the message content via the .content property and then use the .startsWith function to check if it is the command that you need, then you can just compare the guild id, that you get from the .guildId property.
Code (untested):
client.on("messageCreate", message => {
if (message.content.startsWith("your command") && message.guildId === "your guild id") {
//your code
}
})

Welcoming of Bot not working because of "Cannot read property 'find' of undefined"

Ok so I want my bot to welcome new users in a channel for welcoming, so I used find. Here is what I got so far:
client.on('guildMemberAdd', member => {
let guild = member.guild;
guild.channel.find('name','welcome','welcoming','greeting','general').send(`AYYY! Welcome ${member.user} to our Discord Server! Check out the FAQ, Info, and/or The Rules channels (if there is) for some documentation and support to help you get ready!`);
});
Expected result: Welcomes the user in the channel that was used find on.
Actual Result: Cannot read property 'find' of undefined.
I tried a lot of things, but the results are the same. Also, channels didn't not work, only channel.
I also don't believe you can use .find() to return multiple channels, since it always returns the first element it finds in the array.
You can, however, create another array that is a filter of guild.channels.cache based on channel names and then use .forEach() on that array to send a message to each of them like so:
function channelNamesFilter(channel) {
let channelNames = ['name','welcome','welcoming','greeting','general'];
if(channelNames.includes(channel.name)) {
return true;
}
return false;
}
let filteredChannels = guild.channels.cache.filter(channelNamesFilter);
filteredChannels.forEach(element => element.send('AYYY! Welcome ${member.user.name} to our Discord Server! Check out the FAQ, Info, and/or The Rules channels (if there is) for some documentation and support to help you get ready!'));
Notice too how I changed ${member.user} to ${member.user.name}, the first one is an object the second one is its name property in string form.
It's guild.channels with a s and you have to use the cache so your code would be:
client.on('guildMemberAdd', member => {
let guild = member.guild;
guild.channels.cache.find('name','welcome','welcoming','greeting','general').send(`AYYY! Welcome ${member.user} to our Discord Server! Check out the FAQ, Info, and/or The Rules channels (if there is) for some documentation and support to help you get ready!`);
});
Edit:
You can't find multiple channels. You have to put only one name.

Discord.js SetNickname Function Is Not Working

I am trying to make a bot that gives a role and sets nickname of it's user.
My goal is if someone types " -verify SomeNickname " on the text channel the bot will set their nickname as SomeNickname and give them a certain role.
mem.AddRole is working without any errors but .setNickname function is not working with anything.
The error is TypeError: mem.setNickname is not a function
This duplicate thread did not work for me: Change user nickname with discord.js
I also tried:
message.member.setNickname & message.author.setNickname &
client.on('message', message => {
if (message.content.startsWith('-verify')) {
message.author.setNickname({
nick: message.content.replace('-verify ', '')
});
}
});
so far.
My code is:
module.exports = bot => bot.registerCommand('verify', (message, args) => {
message.delete();
var title = args.join(' ');
var mem = message.member;
mem.addRole('560564037583241227').catch(console.error);
mem.setNickname(title);
}
The bot is giving the role without any problems but its not setting a nickname to the user.
Additional Info: Bot has every permission and im not trying to change server owner's nickname.
The message.member object looks like this:
As determined through the chat, your current code is using discord.io, not discord.js. These libraries are different, so that's the source of your various issues.
I'd recommend using discord.js from now on, but you may have to restructure your code a little bit. Documentation can be found here for future reference.
If you'd like to continue using discord.io, you can edit your question to be clearer, although from our conversation you don't intend to.

How to forward message from channel to the groups on telegram bot?

im trying to implement my bot a function. Function that if the channel write any message it will be forwarded to the groups where the bot already is.
Trying to use scope method that worked like a charm on welcome message when new user joined the group.
//index.js
const Telegram = require('telegram-node-bot'),
tg = new Telegram.Telegram('MYAPI', {
workers: 1
});
const ForwardController = require('./controllers/forward')
tg.router.when(new Telegram.TextCommand('/info', 'infoCommand'), new InfoController())
.otherwise(new ForwardController());
//forward.js
const Telegram = require('telegram-node-bot');
class ForwardController extends Telegram.TelegramBaseController {
handle(scope) {
if ('channel' == scope.message.chat._type) {
scope.api.forwardMessage(scope.message._chat._id, _forwardFromChat._text);
}
}
}
module.exports = ForwardController;
I tried many combinations but the message is never forwarded... The bot is already administrator on the channel and is also putted in the groups. (Have also private message opened with bot so i think it should forward also there)
Take a look at the API reference for the library, the documentation page appears to be down so Github is your friend.
The forwardMessage call you are making has incorrect arguments and is accessing the private class variables. It is also returning a promise so you should await the promise or chain a .then to it. You can use the class methods on the Scope instance itself.
It should be more like:
// using async/await - note the containing function must be async for this approach
const result = await forwardMessage(<id of chat here>, scope.message().id());
// or to chain a .then
forwardMessage(<id of chat here>, scope.message().id())
.then(result => /* do something with result */)
.catch(err => /* handle the error */);
This will use the Scopes instance method and handle sending the id of the current chat for you, all you need is the id of the chat you want to send the message to and then replace the <id of chat here> with that id.

Private messaging a user

I am currently using the discord.js library and node.js to make a discord bot with one function - private messaging people.
I would like it so that when a user says something like "/talkto #bob#2301" in a channel, the bot PMs #bob#2301 with a message.
So what I would like to know is... how do I make the bot message a specific user (all I know currently is how to message the author of '/talkto'), and how do I make it so that the bot can find the user it needs to message within the command. (So that /talkto #ryan messages ryan, and /talkto #daniel messages daniel, etc.)
My current (incorrect code) is this:
client.on('message', (message) => {
if(message.content == '/talkto') {
if(messagementions.users) { //It needs to find a user mention in the message
message.author.send('Hello!'); //It needs to send this message to the mentioned user
}
}
I've read the documentation but I find it hard to understand, I would appreciate any help!
The send method can be found in a User object.. hence why you can use message.author.send... message.author refers to the user object of the person sending the message. All you need to do is instead, send to the specified user. Also, using if(message.content == "/talkto") means that its only going to run IF the whole message is /talkto. Meaning, you can't have /talkto #me. Use message.content.startsWith().
client.on('message', (message) => {
if(message.content.startsWith("/talkto")) {
let messageToSend = message.content.split(" ").slice(2).join(" ");
let userToSend = message.mentions.users.first();
//sending the message
userToSend.send(messagToSend);
}
}
Example use:
/talkto #wright Hello there this is a dm!

Resources