Preventing a Loop in a Message Logger

I’m creating a message logger for my server. I have the basic code, but the message logger logs when a message from the message logger is sent, creating an infinite loop. I’m trying to prevent any message from a certain channel from being sent by using if (message.channel =! <#1124411980799803462>) however, it does not prevent it. Here is my replit.

Try

client.on('messageCreate', async (message) => {
  if(message.author.bot) return;
  if (message.channel =! `<#1124411980799803462>`) {
  console.log(`${message.author.name} just said '${message}' in ${message.channel}.`)
   await webhook.send({ content: `${message.author} just said '${message}' in ${message.channel}.` }).catch(e => { console.log('Failed to send messages via Webhook') })
  }
}); 


Error:

node:events:491
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot set property channel of [object Object] which has only a getter

Try

client.on('messageCreate', async (message) => {
  if(message.author.bot) return;
  if (message.channel.id == '1124411980799803462') {
  console.log(`${message.author.name} just said '${message}' in ${message.channel}.`)
   await webhook.send({ content: `${message.author} just said '${message}' in ${message.channel}.` }).catch(e => { console.log('Failed to send messages via Webhook') })
  }
});

In your condition for excluding the channel, you’re using =! which is not a valid operator. You should use != instead.

You’re also trying to compare the channel ID directly with a string containing Discord’s channel syntax <#channel_id>, which won’t work as message.channel.id is just the ID, not the formatted string.

Should be something like this:

client.on('messageCreate', async (message) => {
  if(message.author.bot) return;
  if (message.channel.id != '1124411980799803462') {
    console.log(`${message.author.name} just said '${message}' in ${message.channel}.`)
    await webhook.send({ content: `${message.author} just said '${message}' in ${message.channel}.` }).catch(e => { console.log('Failed to send messages via Webhook') })
  }
});

4 Likes

Now it works, but after adding an embed throws this error:

CombinedPropertyError: Received one or more errors
    at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1268:70)
    at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
    at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
    at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
    at EmbedBuilder.setAuthor (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:291:26)
    at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:107:10)
    at Client.emit (node:events:513:28)
    at Client.emit (node:domain:489:12)
    at MessageCreateAction.handle (/home/runner/ggjhbot/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10) {
  errors: [
    [
      'name',
      CombinedError: Received one or more errors
          at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1104:23)
          at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
          at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
          at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
          at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
          at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
          at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
          at EmbedBuilder.setAuthor (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:291:26)
          at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:107:10) {
        errors: [
          ExpectedValidationError: Expected values to be equals
              at LiteralValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:745:76)
              at LiteralValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1099:32)
              at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
              at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
              at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
              at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
              at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88) {
            validator: 's.literal(V)',
            given: [User],
            expected: null
          },
          ValidationError: Expected a string primitive
              at StringValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1715:70)
              at StringValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1099:32)
              at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
              at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
              at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
              at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
Hint: hit control+c anytime to enter REPL.
Started refreshing application (/) commands.
24/7 Activation Complete
Gimkit Jam Bot#9254 has logged in!
node:events:491
      throw er; // Unhandled 'error' event
      ^

CombinedPropertyError: Received one or more errors
    at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1268:70)
    at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
    at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
    at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
    at EmbedBuilder.setAuthor (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:291:26)
    at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:107:10)
    at Client.emit (node:events:513:28)
    at Client.emit (node:domain:489:12)
    at MessageCreateAction.handle (/home/runner/ggjhbot/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10) {
  errors: [
    [
      'name',
      CombinedError: Received one or more errors
          at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1104:23)
          at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
          at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
          at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
          at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
          at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
          at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
          at EmbedBuilder.setAuthor (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:291:26)
          at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:107:10) {
        errors: [
          ExpectedValidationError: Expected values to be equals
              at LiteralValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:745:76)
              at LiteralValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1099:32)
              at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
              at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
              at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
              at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
              at ObjectValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88) {
            validator: 's.literal(V)',
            given: [User],
            expected: null
          },
          ValidationError: Expected a string primitive
              at StringValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1715:70)
              at StringValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at UnionValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1099:32)
              at UnionValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
              at runPredicate (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1231:32)
              at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1241:9)
              at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
              at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)

Refer to the same replit as last time.

You are not using setAuthor() properly.

According to the Discord.js documentation:

MessageEmbed

* `MessageEmbed` has now been renamed to `EmbedBuilder`.
* `EmbedBuilder#setAuthor()` now accepts a sole [`EmbedAuthorOptions` open in new window](https://discord.js.org/docs/packages/builders/stable/EmbedAuthorOptions:TypeAlias) object.
* `EmbedBuilder#setFooter()` now accepts a sole [`EmbedFooterOptions` open in new window](https://discord.js.org/docs/packages/builders/stable/EmbedFooterOptions:TypeAlias) object.
* `EmbedBuilder#addField()` has been removed. Use `EmbedBuilder#addFields()` instead.

So you should construct the embed like:

embed.setAuthor({
  name: message.author.username, 
  iconURL: message.author.displayAvatarURL(),
  url: 'http://url-to-the-author.com' // This is optional, only include it if you want
});

Remember to replace embed and message with the actual instances you’re using

3 Likes

Now there is a new error:

node:events:491
      throw er; // Unhandled 'error' event
      ^

CombinedPropertyError: Received one or more errors
    at ArrayValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:473:70)
    at ArrayValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
    at EmbedBuilder.addFields (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:222:31)
    at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:111:10)
    at Client.emit (node:events:513:28)
    at Client.emit (node:domain:489:12)
    at MessageCreateAction.handle (/home/runner/ggjhbot/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
    at module.exports [as MESSAGE_CREATE] (/home/runner/ggjhbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/ggjhbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:354:31)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10) {
  errors: [
    [
      0,
      CombinedPropertyError: Received one or more errors
          at ObjectValidator.handleIgnoreStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1268:70)
          at ObjectValidator.handleStrategy (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1121:47)
          at ObjectValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:1221:17)
          at ObjectValidator.run (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:198:23)
          at ArrayValidator.handle (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:467:37)
          at ArrayValidator.parse (/home/runner/ggjhbot/node_modules/@sapphire/shapeshift/dist/index.js:212:88)
          at EmbedBuilder.addFields (/home/runner/ggjhbot/node_modules/@discordjs/builders/dist/index.js:222:31)
          at Client.<anonymous> (file:///home/runner/ggjhbot/index.js:111:10)
          at Client.emit (node:events:513:28) {
        errors: [ [ 'value', [ValidationError] ] ]
      }
    ]
  ]
}

Node.js v18.12.1
repl process died unexpectedly: exit status 1

The error message indicates that the values passed to addFields method in EmbedBuilder are not correct.

Again, you need to look at the Discord Documentation Guide: https://discordjs.guide/

The add fields should be looking like this:

embed.addFields([
  { name: 'one', value: 'one', inline: true },
  { name: 'two', value: 'two', inline: true },
]);

On the error message, specifically errors: [ [ 'value', [ValidationError] ] ], looks like the problem is in the value property of the fields.

The value property should be a string, and the error suggests that it’s not a string.

2 Likes

This specific topic of the documentation talk about the changes made in v14.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.