Tmi.js Bot You don't have permission to perform that action

Hello there, I have a simple bot created with node.js and tmi.js library.

So far I managed to connect the bot to my channel and listen for messages but problem comes when I want to delete a message.

According tmi.js documentation, in order to use the method to delete a certain message you need 2 arguments: channel and message ID.

At this point, I found that there is already a question in this link, basically I am having the same problem but with a specific error message: You don’t have permission to perform that action.

Here below a code sample to replicate my error

const tmi = require('tmi.js');

const opts = {
  identity: {
    username: '<BOT_USERNAME>',
    password: '<OAUTH_TOKEN>'
  },
  channels: [
    '<CHANNEL_NAME>'
  ]
};

const client = new tmi.client(opts);

client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

client.connect();

function onMessageHandler (target, context, msg, self) {
  if (self) { return; }
  if (msg === 'ciao') {
	client.deletemessage(channel, context.id);
}


function onConnectedHandler (addr, port) {
  console.log(`* Connected to ${addr}:${port}`);
}

Below my context object

context {
  'badge-info': null,
  badges: { broadcaster: '1' },
  'client-nonce': 'xxxxxxxxxxxx',
  color: null,
  'display-name': 'a_name',
  emotes: null,
  'first-msg': false,
  flags: null,
  id: 'xxxxxxxxxxxxxxx',
  mod: false,
  'returning-chatter': false,
  'room-id': xxxxxxxxx
  subscriber: false,
  'tmi-sent-ts': 'xxxxxx',
  turbo: false,
  'user-id': 'xxxxxxxxxx',
  'user-type': null,
  'emotes-raw': null,
  'badge-info-raw': null,
  'badges-raw': 'broadcaster/1',
  username: 'a_name',
  'message-type': 'chat'
}

Am I missing something in the setup of my app? I do realize that this may come from user role (which in my case is set to “broadcaster”) but after many hours I could not find a proper thread or solution to this.

Edit: worth to mention, there is an explanation on this page about /delete command:

A bot may not delete the broadcaster’s messages.

I tried to delete a msg from a separate account not connected to the bot in the target channel, but I still got the same error mentioned in the title.

Is your bot a moderator in that channel, and you’re using an OAuth token with the channel:moderate scope?

thanks for the answer, I am using OAuth token but with no channel:moderate scope. Honestly I don’t know where is needed as I have not found any possible implementation of it. The bot is not a moderator of the channel, I tried to declare it using the method client.mod("channel", "username") but I got the same error message.

Well if your OAuth token doesn’t have the channel:moderate scope, then any chat connections with that token wont have the permission to perform moderation actions on channels where it is a moderator.

And you can’t just have your bot give itself moderator status, you need the broadcaster of whose channel it is to give your bot mod status. If the bot is the same account as the channel you’re trying to run these actions on, then the broadcaster status already grants the ability to perform moderator actions, you just need to have the appropriate scope on the OAuth token.

understood, then the question will be, how can I setup that scope within the application?

Follow the documentation for the Auth Code flow and request the scopes you need for what you need your bot to do.

solved, I was sending a wrong call putting the scope in a wrong way (this is what worked for me, channel:read was not necessary => https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=clientid&redirect_uri=localhostspecifiedinconsole&scope=channel:moderate+channel:read )