Discord.js SetNickname Function Is Not Working - node.js

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.

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
}
})

How do i make a discord bot reply to my command in a certain way

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
}

Get message ID from reaction discord.js

Note: I am using discord.js V11, I know I plan on updating it to V12 next month after I unspaghetti my spaghetti code.
So I have no idea how to grab the messageID from a message that has triggered the reaction in the bot.
The way I want it to work is as follows: A user reacts to a message, any message, with the reaction programmed within the bot. The bot then grabs the message url that the reaction was given to, and then sends a message to a client.channels.get("id").
So I tried using this code but really couldn't quite get to where I needed to be:
client.on('messageReactionAdd', async(reaction, user, message) => {
if(reaction.emoji.name === "hm") {
let ticket = client.channels.get("CHANNEL_ID");
let ticketurl = message.url
ticket.send("Test confirmed" + ticketurl);
}
});
Figured it out!
I just needed to add this:
let ticketurl = react.message.url

issue with creating role and channel automatically from private message with bot

Not sure how the create channel and create role isn't working inside the following code, towards the bottom. (EDIT: Nothing is sent to the console and nothing happens regardng the code. It is like it is entirely ignored.) This is a snippet from code that User A challenges User B. User B is messaged, alerting them that a challenge has been issued to them via a Private Message. If the challenge is accepted, I want the bot to 1)Make a role specifically for User A and User B named "User A vs User B" 2) take User A and User B and put them both into that new role and 3) Make a battlefield named "User A vs User B" inside a specific category inside the server the bot is on.
I am unsure if the problem lies in how the bot is trying to make the role and channel in a sever while the bot is talking to the user in a private message instead of on the server. I thought putting the "server" variable as the server ID would help but it doesn't seem to do anything after the accept message.
// Awaits reply from user
if (message.channel.id === '541736552582086656') return target.send("Do you accept the challenge? Please reply with 'accept' or 'deny'.")
.then((newmsg) => {
newmsg.channel.awaitMessages(response => response.content, {
max: 1,
time: 150000,
errors: ['time'],
}).then((collected) => {
// Grabs the first (and only) message from the collection.
const reply = collected.first();
if (reply.content === 'accept'){
reply.channel.send(`You have ***accepted *** the challenge from ${challenger}. Please wait while your battlefield is made...`);
message.author.send(`${target} has accepted your challenge! Please wait while the channel is made for your brawl...`)
/// Problems start here
function createChannel(message){
var server = "SERVER ID";
var name = `${target} vs ${challenger}`;
message.guild.createRole({
role: {
name: `${target} vs ${challenger}`,
color: "#00fffa",
permissions: [] }
}).then(role => {
target.addRole(role, name)
challenger.addRole(role, name)
.catch(error => client.catch(error))
}).catch(error => client.catch(error))
server.createChannel(Name, name).then(
(channel) => {
channel.setParent("CATEGORY ID")
})
} // problems end here
} else if (reply.content === 'deny') {
reply.channel.send("You have ***denied *** the challenge.")
} else {
reply.channel.send("Your response wasn't valid.");
}
})
})
}
I have been wondering if I need to go about making the channel and role in a different way since it is trying to be made from a private message and not inside the server..
Thanks for any and all help! I also apologize if I'm using stack overflow too much for problems like this... You guys are great at helping me see different ways to do things and what I'm doing wrong, so I am learning, but I don't want to feel like I'm abusing it too much.
I think the problem is the fact that you create a function called createChannel with the code to create a rol and channel, but you never call said function.
You can either call the function after you've declared it or (which is in my opinion better) you can remove the following lines
function createChannel(message){
} // problems end here

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