Is there any way to send messages to channels without replying? - javascript

With Discord switching to application commands, bots have no way to send messages without replying to a command a user has sent. I would like to know if this is true?

For sending messages without replying in discord.js v14, you can use this code:
client.on("interactionCreate", async (interaction) => {
await interaction.channel.send("Lorem Ipsum")
});

Related

How to fix DiscordAPIError: Unknown Member?

I have had a discord bot running for about 3 months, and today I started getting this DiscordAPIError: Unknown Member error message. It is coming from an interaction:
client.on("interactionCreate", async interaction => {
interaction.member.roles.add(<roleId>);
});
I am unable to consistently reproduce the error, but it seems like it may be coming from new members joining the server, and perhaps the discord API is not recognizing them as a guild member just yet. The server had a large influx of users today, and that's when the issue started.
Is there any way I can work around this issue, or perhaps force update the guild's member list?
The member does not seem to be properly cached. Before accessing it, use await interaction.member.fetch()
client.on("interactionCreate", async interaction => {
await interaction.member.fetch()
interaction.member.roles.add(roleId)
})

Using Discord.js, how can I trigger a bot when a webhook is triggered?

I have a bot in a server (for example purposes, let’s say the server is called “John’s Server”), so, in John’s Server, there is also a webhook.
What I want to do is, whenever a webhook message is sent in John’s server, the bot sends a message in a channel. (For the example, let’s say the channel ID id 1234567890)
So, if the webhook in John’s Server with the ID 0000000000 is used to send a message, the bot sends a message in channel 1234567890.
How is this possible? (Using Discord.js, by the way)
To do this you'll have to listen to the message event, when it fires you'll check the Message#webhookID property. If it has a truthy value then the message is sent from a webhook, otherwise it's sent from a user.
Then all you need to do is get the channel from the server and send the message.
Here's an example:
client.on("message", (message) => {
if (!message.webhookID) return; // If the message isn't from a webhook, don't do anything
const channel = message.guild.channels.fetch("0123456789");
// Using the fetch method, discord.js will check the cache first, if the channel is there
// then it will return it, otherwise discord.js will hit the Discord API and get the channel
channel.send(`
A message from ${message.author.username} (${message.webhookID}) has been sent in
${message.channel.name}.
Message: ${message.url}
`);
});

How do I make it so when a user tags the discord bot, it responds with a message?

I have a discord bot, in JavaScript and I was wondering how I could make it so when a user ran a message which only contained the bots mention, it would respond with a message such as The server prefix is... <prefix>.
I have tried adding this in the prefix file but nothing actually happened when I mentioned the bot.
if (message.content.startsWith("<#649042475557584896>") {
return message.channel.send(`The server prefix is... ${args[0]}`)
}.
I even tried setting it in an embed, to make it cleaner but that didnt help either.
You can use the propertie isMemberMentioned() from the Message object.
Little example:
client.on("message", (message) => {
if (message.isMemberMentioned(client.user)){
//do your code here...
}
}
WARNING! It seems it's deprecated. V12 seems to don't work with it.
https://discord.js.org/#/docs/main/v11/class/Message?scrollTo=isMemberMentioned

Sending a message on a guildMemberAdd event in the server on Discord

i'm coding a discord bot with JavaScript. It's running pretty good, but the only thing i can't do is sending a message in the server when a new member joins the server. (Not a private message, directly a message in the server). I don't find any way to do that, any ideas ?
bot.on('guildMemberAdd', function(member){
member.send("Hello there !");
});
This one sends a private message, it does exactly what i don't want it to do ^^'
The member parameter that gets passed into the callback function is exactly that--an object representing a discord member. If you want to send a message to the guild the member just joined, you will need to get the guild property of the member, member.guild and then get the channel you want from channels and send the message you wish. All together it should look something like this:
bot.on('guildMemberAdd', function(member){
member.guild.channels.get("put-channel-id-here").send("Hello there!")
});
I think you may know by now but in Discord.js v12 (current stable version), you would use
member.guild.channels.cache.get("put-channel-id here").send("Hello there!")

How to send a message to a specific channel?

I'm building a reminder bot for Discord.
The server owner or moderator should subscribe to the server via a message.
I'm having trouble finding the way to send the message to the specific server and channel.
Any idea if there is a way?
If you have the channel ID all you need to do is find the channel and send a message to it:
let channel = client.channels.get('THECHANNELID');
if(channel) {
channel.send("My Message");
}

Categories