Message Specific PFP. (like TupperBox) - javascript

I am looking to send a message that has a different profile picture than the bot. Basically, like Tupper Bot does. I can't seem to figure out how to do it anywhere, I've read the documentation and searched around.

You can use a webhook to do what you describe. If you already have a webhook link see here on how to use it. This works from external programs too.
If you don't, you can create a webhook with your bot and then send messages to it. In this example we will create a simple webhook and send a message to it.
First we should check if the channel already has a webhook. That way you don't create a new one everytime you use the command. You do that with the fetchWebhooks() function which returns a collection if there is a webhook present. If that is the case we will get the first webhook in the collection and use that. Note: if you have more then one webhook you can of course add additional checks after the initial one.
If there are no webhooks present, we create a new one with createWebhook(). You can pass additional options after the name.
let webhook;
let webhookCollection = await message.channel.fetchWebhooks();
if (webhookCollection.first()) {
webhook = webhookCollection.first();
} else {
webhook = await message.channel.createWebhook("new webhook", {
avatar: "https://i.imgur.com/tX5nlQ3.jpeg"
});
}
Note: in this example we use the channel the message came from but you can use any channel you want
Technically we can now send messages already.
webhook.send("this is a test");
What you can also do is create a webhookClient. Thats a little like the discord client you create when you start your bot. It takes the webhook ID and token as well as additional options you can set for clients.
const hookclient = new Discord.WebhookClient(webhook.id, webhook.token);
This lets you use a few additional methods like setInterval.
hookclient.setInterval(() => {
hookclient.send("This is a timer test");
}, 1000)
This lets you use the webhook without creating or fetching one first.

Related

How do I get info from an embed?

I've been making a suggestion command and I want it so that I can use a -approve <The message id> to edit the message. The problem is when I try to fetch info from it using
const Suggestionchannel = client.channels.cache.get("833796404383973442");
const messages = Suggestionchannel.messages.fetch({ limit: 100 })
it only fetches all the messages and not the embeds. I want it to fetch the embeds so I can get the data and edit the message by its id. With the data, I could display who made the suggestion and what the suggestion was while at the same time showing that it was approved. How would I get the info from an embed using a message id?
On a side note: I know I could use async and await to easily edit it while on the same code but I want it so that it's accessible. If I do that and restart the bot, I won't be able to approve the previous suggestions, only the new ones after I restart the bot.
First, you have to either await fetching the messages from the suggestion channel, or put it into a .then to not have messages be a Promise. Awaiting it wouldn't change anything in your ability to edit the message, you just have to make sure your message cache has the actual message in it.
Fetching the messages from a channel would return a Promise<Collection<Snowflake, Message>>. Once you resolve the message, you get a collection containing messages. On the Message class, there is a array called embeds. It contains all of the embeds part of the message, normally, if sent by a bot, only contains one MessageEmbed.
Another thing is, to make it easier for you, the fetch message also takes in a message ID, and if it's provided, it would fetch the message with that ID from the channel. Doing this would change what it returns to Promise<Message>
Implementing all of these changes in your code would look something like;
const Suggestionchannel = client.channels.cache.get("833796404383973442");
// make sure that your outer scope is asynchronous
const message = await Suggestionchannel.messages.fetch('INSERT MESSAGE ID OR VARIABLE HERE')
const embed = message.embeds[0]
//would be undefined if there is no embed
//would be a MessageEmbed if there is an embed

How would I go about editing a message sent by a a discord bot? But only when reacted to by a user with a certain role?

I'm trying to setup a bug report system. I have the token system and everything for it, however I need to know how to have the bot edit a message when it is reacted to by a user with a particular role, which for this sake is named 'Debugger'
The event you want to listen to is client.messageReactionAdd. Be aware, that it will not listen to non-cached messages. To listen to older messages, you must enable partials, like so:
//top of your code
const client = new Discord.Client({partials: [`MESSAGE`, `CHANNEL`, `REACTION`]});
.
.
.
This will allow you to receive events for partial messages. In order to get full message from partial, you need to fetch it:
client.on('messageReactionAdd', async msgReaction => {
let msg = msgReaction.message;
//note that messageReactionAdd event returns MessageReaction object, not Message.
//this line makes it simpler to write code.
if (msg.partial) await msg.fetch().catch();
//
//rest of your reaction handler goes here
//
});
You will be able to figure out the rest by reading the documentation.

Send a DM and then reacts to the message once a new user joins

so every time a new user joins I sent the users a message using
client.on ("guildMemberAdd", member => {
member.send("WELCOME")})
Now, is there any way for the bot to react to that message?
Thanks!
Similar to Syntle's answer, but uses promises instead. (My preferred use)
member.send("WELCOME").then(function(msg){
msg.react("😀");
}
You need to contain member.send() in a variable and then use the .react() method on it.
So your solution is:
const msg = await member.send("WELCOME")
await msg.react("🥚");

bot.sendMessage is not a function

I'm currently making a discord bot that will send out messages. Unfortunately the messages are receiving errors such as bot.sendMessage is not a function. I'm fairly new to coding so this one has me stumped. Even any google searches have not been able to help me to the point where I can understand it.
I've tried bot.send as maybe .sendMessage is now outdated I had read somewhere I believe.
var exampleSocket = new WebSocket(dataUrl);
bot.send({to: flowChannel,message: 'Websocket connected'});
exampleSocket.onopen = function (event) {
logger.info('got to here');
The output should post in my channel that the websocket connected.
bot.send({to: flowChannel,message: 'Websocket connected'});
Client.sendMessage() was removed in Discord.js 9.0 back in 2016.
I've tried bot.send as maybe .sendMessage is now outdated I had read somewhere I believe.
TextBasedChannel.sendMessage(), TextBasedChannel.sendCode(), TextBasedChannel.sendEmbed(), TextBasedChannel.sendFile(), and TextBasedChannel.sendFiles() are all deprecated.
The equivalent of your code today is as follows...
flowChannel.send('Websocket connected')
.catch(console.error); // Catch the rejected promise in the event of an error.
Discord.js Docs (Stable)
#sendMessage is deprecated.
You need to pass 'message' through and use message.channel.send()
Channel can also be a DM.
TextChannel.sendMessage (and DMchannel.sendMessage) along with all type of sendXX functions, is deprecated.
The function TextBasedChannel.send take as argument a MessageOptions value (or directly an Attachment or RichEmbed) if you want to pass anything other than just text.
To access the send function, you need a TextChannel or a DMchannel which you can get:
In the message that triggered your function like this: client.on('messages', (msg) => {msg.channel.send('Hello')})
If you have a Guild instance: guild.channels.get('channel id') or guild.channels.find(d => d.name === '<channel name>)') (note: you'll have to check if the channel is a TextChannel and not a voice one)
with the Client.channels method, which list all of the Channels that the client is currently handling, mapped by their IDs - as long as sharding isn't being used, this will be every channel in every guild, and all DM channels

Return the channel ID of a channel mentioned in a command

The title says all. I'm making a Discord bot in node.js, and one part that I'm trying to add is a .setup command, to make the bot less dependent on manually changing the value of client.channels.get().send(), allowing it to be more easily set up in the future.
Anyway, in my case, I'm trying to have the user reply to a message with a channel mention (like #welcome for example), and have the bot return that ID and save it to a variable.
Currently, I have this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.mentions.channels.first().id;
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
});
}
message.mentions.channels returns undefined.
I know that the message.mentions.channels.first().id bit works when its a user instead of a channel, is there a different way of getting a channel mention in a message?
Here is the most easy and understandable way i found :
message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))
But it not really what we want we could use <#####9874829874##><<<547372>> and it will work. So i figured out this :
message.guild.channels.cache.get(args[0].substring(2).substring(0,18))
I like to just remove all non-integers from the message, as this allows for both IDs and mentions to be used easily, and it will return the ID for you from that, as mentions are just IDs with <# and > around them. To do this you would use
message.content.replace(/\D/g,'')
With this, your code would look like this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.content.replace(/\D/g,'');
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
});
}
From this you will always get an ID. And we can surround the ID to trun it into a mention.

Categories