Get message ID from reaction discord.js - node.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

Related

I just made a welcome and goodbye message for my discord.js bot but it doesn't send the messages to the required channels

Here's my code,
Welcome message:
client.on('guildMemberAdd', async member => {
if(member.guild.id !== "737222740305641472")return;
const channel = guild.channels.cache.get("750952211659620413")
if (!channel) return;
let data = await canva.welcome(member, { link: "https://imgur.com/a/BPTpkDT", blur: false })
const attachment = new Discord.MessageAttachment(
data,
"welcome-image.png"
);
channel.send(
"Welcome message here",
attachment
);
});
And my goodbye message's code:
client.on('guildMemberRemove',(member) => {
if(member.guild.id !== "737222740305641472")return;
guild.channels.cache.get('781737515421138984').send(`Goodbye message here`);
});
I'd like to make it clear that I dont get any error's in my console and that I've given the bot all the perms it needs to send messages.
Also, I've already declared my guild before, so that isn't supposed to be the problem.
Can anyone tell what I'm doin wrong?
As directed by this comment, it is appearent that you have not enabled privilliged intents for your application. For your application to listen to events such as guildMemberAdd and guildMemberRemove you would have to enable to Members Intent from the Discord Developer Portal. Here is what you would be looking for:
This can be found under
Your Application > Bot > ( Scroll down ) Privilleged Gateway Intents > Server Members Intent ( toggleable ).
This was a change introduced in the Discord API version v8 for the intents to be mandatorily enabled for such API requests.

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

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.

Telegram Bot Private bot [security]

I have a Telegram Bot written in javascript (node-telegram-bot-api + SailsJs). I want to create a bot that answer private information, for example, my /todayTasks or /myAgenda. Can I block people from using my bot in private chats?
I tried to use leaveChat() when the bot receives /start but it only works in group chat.
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: chat member status can't be changed in private chats
My Code:
bot.onText(/\/start/, function(msg) {
const chatId = msg.chat.id;
CheckAuthorized(chatId).then(function(user) {
if (!user) {
bot.sendMessage(chatId, 'Sorry, you are not allowed to ask me.');
bot.leaveChat(chatId); //<---- ERROR HERE!
return;
}
bot.sendMessage(chatId, 'Hello my friend.');
});
});
Of course, I can use an Authentication Policy to run before every request, if there is no other option.
At the moment it is not possible for bots to block or ignore specific users, or everyone except a whitelist.
What I do is put the chat-id of every user or group I want my bot to work in into an array.
On every received message the bot then checks if the chat-id is in that array, if it's not it simply exits the function.
Here is an example for python-telegram-bot:
whitelist = [-10012344586,-2457841,-100554879472]
def on_message(bot, update):
if not update.message.chat_id in whitelist:
return
Unfortunately, bots can only leave group chat at this time, so the only thing you can do is just ignore them in your code :(

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