Check if command is run in a server? - node.js

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

Related

How edit messages in Discord.js V12 using message ID

There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});

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

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.

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