Discord js message sending to specific channel - javascript

first off all, i know there are similar threads, but i can't find a single one that uses the same method as i would like. My code:
ticketChannel.send({
embeds: supportEmbed,
components: new MessageActionRow().addComponent(supportButton).addComponent(claimButton),
message.ticketChannel.send("<#&874175844422135808> Prosím o vyřešení ticketu");
})
The last line should send a message that pings a role with that id. Well, it doesn't. The message must be sent to the ticketChannel channel.
Thanks for helping me

if you want to always send it to a specific channel, you'll need to use the id of the channel to define it like so: (assuming you have guild defined)
let channelid = "your channel id"
let ticketchannel = guild.channels.fetch(channelid)
ticketchannel.send("<#&874175844422135808> Prosím o vyřešení ticketu")

Related

Delete messages in a specific channel using channel id

I'm trying to make a command for my discord bot that sends embeds in different channels(using the id) and deletes the message that was sent before. I've googled a lot and I still can't find a working solution on how to delete messages in a specific channel using the channel ID. I only need 1 message to be deleted if that's easier but I don't know.
This is my code:
client.channels.cache.get('channelid').messages.fetch().then(message => message.delete())
client.channels.cache.get('channelid').send({ embeds: [FAQEmbed] }); // sends FAQ Embed
I replaced the id with channelid in case of anything. The embed works and it sends, but the message before doesn't get deleted.
Yes your code correct, but if you want to delete the message when bot replied.
use message.channel.id and message.id to return message and channel id
example:
client.channels.cache.get(message.channel.id).messages.fetch(message.id).then(message => message.delete())
hope this help you, but you need to defined the client as well

How do I post a welcome message when my discord bot joins a server

I am new to JavaScript and I have made a small discord bot but I cannot find a way to make it have a welcome message when it joins the server. I want it to say something like "Hello my name is {bot-name}, type !help to view my commands." Does anyone know how to do this?
You can use the guildCreate event that is emitted whenever the bot joins a guild. You need to find a channel though where you can post your message.
To find a channel you can use the .find() method that returns the first channel where the given function returns a truthy value.
client.on('guildCreate', (guild) => {
const channelNames = ['general', 'welcome']
const channel = guild.channels.cache.find(ch => channelNames.includes(ch.name))
if (channel)
channel.send(
`Hello my name is ${client.user.username}, type \`!help\` to view my commands.`
)
.catch(console.error);
})

Discord.js sending message with #person and #channel

With my discord bot, i am trying to create a welcome message.
I got the following working:
Sending a message when someone joins
Getting the name of the person who joins
Now the problem I have is with doing a # or # in that message. I use the following code for the message:
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #:page_with_curl:rules and assign yourself a role at #:mortar_board:role-assignment`;
But I have also tried:
Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment
Both of them do not what i want them do to. I am getting the following result from this code:
What you can see there is that the #MEE6 and the 2 channel # is not blue, and not clickable. But when I copy the full text, past it and send it in the chat it does show blue.
The result I would like to have is the result of when I copy, paste and send it. That is the following:
Here is a little more of the code:
const welcomeChannel = client.channels.cache.get('738678076174630922');
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment`;
welcomeChannel.send(welcomeMessage)
What should I do to fix this?
const welcomeMessage = `Welcome ${member}! Please look through the <#id-of-rules-channel-here> and assign yourself a role at <#id-of-role-assignment-channel-here>`
You can auto-embed Discord classes like channels and members if you already have them stored in a variable (like you do for member), or you can use their IDs in text directly in the form <#id> for channels or <#id> for users.

Is there a way for a Javascript Discord Bot to send a DM to a specific person?

I am trying to create a system that allows a user to send a message and receive a DM from a bot. That part is working fine. However, I also want the bot to send a message to myself when that happens. Whenever I try to get it to send a message to me, it will always have an error.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', message => {
if (message.toString().toLowerCase().includes('support')) {
message.author.send('Your assistance ticket has been created. Please wait for a DM from ageekdude.')
message.ageekdude.send(message.author + ' has requested an assistance ticket.')
}
});
client.login('token here');
At the part where it says message.ageekdude.send, I'm trying to get it to send that message to me. Also, at the part where it says message.author, could you tell me how to make it say the name of the user instead of the user ID?
if (message.toString().toLowerCase().includes('support')) {
message.author.send('Your assistance ticket has been created. Please wait for a DM from ageekdude.');
const ageekdude = client.users.cache.get('PASTE YOUR USER ID HERE');
if (ageekdude) ageekdude.send(message.author + ' has requested an assistance ticket.')
}
This will try to find you in the list of all bot's users, and then send you the message,
I hope this will solve your issue !

Assigned Roles by Reactions

I have recently been trying to make a Reaction-to-Role bot and have been struggling with some criteria for making it.
I have researched and looked at other peoples' versions of a similar bot on it, yet I have not been able to use it purposefully in my own code.
The goal of the bot is to have multiple options on a message. Here's an example of what it would look like as a message.
**What do you drive?**
🚙 - A car
🚲 - A bicycle
❌ - Nothing
There would be reactions under the message of each emoji that was specified. Once a user clicked on one of these emojis, it would add a role to them such as "Drives a Bike" or "Rides a Bicycle."
I have managed to go as far as making an addrole command (which receives the message the bot will react to, the emoji, and the role it assigns) and stores some info to a JSON file.
The area in which I have been having issues is actually capturing each user that reacts to the specific message, finding out which reaction they sent, and assigning the role assigned to that emoji.
Here is my current code, I'm thank you for any help you may provide :)
addrole.js
if(!args[0] || !args[1] || !args[2] || !args[3]) return message.reply("please use the following format: `autorole #channel messageID icon #role`.");
let channel = message.mentions.channels.first();
let messageID = args[1];
let icon = args[2];
let role = message.mentions.roles.first();
if(!channel.fetchMessage(messageID)) return message.reply("could not find the specified message. Please check the channel and message ID again.");
channel.fetchMessage(messageID).then(msg => {
msg.react(icon);
info[role.name] = {
roleID: role.id,
channelID: channel.id,
icon: icon,
};
fs.writeFile("./configs/info.json", JSON.stringify(info), (err)=>{
if(err) console.log(err);
});
});
I know that creating an event named messageReactionAdd and messageReactionRemove are needed, but I'm not sure how to find their variables and match them accordingly.
Additionally, I am not sure how to make it constantly watch one message even after a restart. From some testing, I noticed that messageReactionAdd would not continue watching the specified messages reactions after a restart.
I am looking for any guidance/help that anyone may provide, thank you!
I've seen on: https://github.com/Sam-DevZ/Discord-RoleReact/blob/master/roleReact.js that there is an event. I've tried this out and it's working.

Categories