1
votes

I'm kinda new to programming a discord bot, but now I have a problem and can't find a solution. I'm not even sure which language I'm using - I work in Atom, start my bot with node index.js and use functions / classes / ... from here: https://discord.js.org/#/docs/main/stable/general/welcome

My problem

However, with a specific command my bot sends a message to a certain channel and pins it. But I want the bot to delete the system message that says "user pinned message to channel" (it has to be something like that, my discord isn't in english), but I somehow can't get that message.

What I tried

I tried to get it like that:

if (message.content.toLowerCase().startsWith(`${BOT} hat eine nachricht an diesen kanal angeheftet`) { somefunction}

where BOT is <@ Bot ID> and "hat eine nachricht an diesen kanal angeheftet" is kinda the german version of "pinned message to channel", but nothing happens. It doesn't execute somefunction (tested it with message.channel.send(something))

Then I thought I could use fetchMessages() (https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=fetchMessages) like that:

function botAngeheftet(message, planungsChannel) {
  let mes = planungsChannel.fetchMessages({limit:1})
    .then(messages => planungsChannel.send("TEST:\n" + messages.first())
    .catch(console.error);

but then the message in planungsChannel is

TEST:

without any message. I changed the limit to 2 and wrote something in planungsChannel (sometext), executed the function again and the result was

TEST:
sometext

So.. fetchMessages() seems to work - but not for the system message.

My question

How can I "get" that system message to work with it? Or: How can I have the bot delete that system message saying "user pinned message to channel" ?

1

1 Answers

1
votes

This is a theoretical solution (untested) but it should work.

Each message on discord has a type property, which falls under a couple of types, you can see here. The specific type you can use in this case is if message.type === "PINS_ADD", which is the type of the

the system message that says "user pinned message to channel"

that you were referring to.

Put simply, all you have to do is:

on msg event (msg) 
     if(msg.type === "PINS_ADD") msg.delete();

This is the pseudocode not actual code, hopefully you can convert it. Documentation