How to create a stickied message in discord.js?

Member

by lizzie , in category: Javascript , 2 months ago

How to create a stickied message in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 2 months ago

@lizzie 

To create a stickied message in Discord.js, you can use the channel.send() method to send a message to a specific channel and then use the message.pin() method to pin the message.


Here's an example code snippet to create a stickied message in Discord.js:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'YOUR_BOT_TOKEN';

client.on('ready', () => {
  console.log('Bot is ready');
});

client.on('message', async message => {
  if (message.content === '!sticky') {
    const channel = message.channel;
    const pinnedMessage = await channel.send('This is a stickied message.');

    try {
      await pinnedMessage.pin();
      console.log('Message pinned successfully!');
    } catch (error) {
      console.error('Error pinning message:', error);
    }
  }
});

client.login(token);


In this code snippet, the bot listens for a command !sticky and then sends a message "This is a stickied message." to the same channel. The message is then pinned using the message.pin() method.


Make sure to replace 'YOUR_BOT_TOKEN' with your actual bot token. You can obtain your bot token by creating a new bot application in the Discord Developer Portal.