AlexzanderFlores / WOKCommands

130 stars 60 forks source link

errorMsg.replace issue on version 1.2.8 #225

Open MatteoBax opened 2 years ago

MatteoBax commented 2 years ago

Hi, I have a problem for some time wokcommands from this error when I run the command. It is always gone, it has been giving me this error for about 1 month. I have not changed the code of the command so I would like to know if it is possible to fix. The version in question is 1.2.8

Error:

/sdcard/bot/node_modules/wokcommands/dist/CommandHandler.js:190
                    errorMsg = errorMsg.replace(/{COMMAND}/g, name);
                                        ^

TypeError: Cannot read properties of undefined (reading 'replace')
    at Client.<anonymous> (/sdcard/bot/node_modules/wokcommands/dist/CommandHandler.js:190:41)
    at Client.emit (node:events:539:35)
    at MessageCreateAction.handle (/sdcard/bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/sdcard/bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/sdcard/bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/sdcard/bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/sdcard/bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/sdcard/bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (node:events:527:28)
    at Receiver.receiverOnMessage (/sdcard/bot/node_modules/ws/lib/websocket.js:1008:20)
    at Receiver.emit (node:events:527:28)
    at Receiver.dataMessage (/sdcard/bot/node_modules/ws/lib/receiver.js:517:14)
    at Receiver.getData (/sdcard/bot/node_modules/ws/lib/receiver.js:435:17)
    at Receiver.startLoop (/sdcard/bot/node_modules/ws/lib/receiver.js:143:22)
    at Receiver._write (/sdcard/bot/node_modules/ws/lib/receiver.js:78:10)
    at writeOrBuffer (node:internal/streams/writable:389:12)
Benzo-Fury commented 2 years ago

What command is causing this error? Please provide all the code in that command aswell. I presume your talking about wok commands version 1.2.8 aswell, may I ask why you dont use the latest version?

MatteoBax commented 2 years ago

Yes, the version of wokcommands is 1.2.8 I don't update it because if I update it the command no longer works.

const allowedusers = [
  "530079936766279680",
  "621362828049383424",
  "433693486156611585",
]
const momentTimezone = require('moment-timezone')
const { MessageCollector } = require('discord.js')

const scheduledSchema = require('../models/scheduled-schema')

module.exports = {
  requiredPermissions: ['ADMINISTRATOR'],
  expectedArgs: '<Channel tag> <YYYY/MM/DD> <HH:MM> <"AM" or "PM"> <Timezone>',
  minArgs: 5,
  maxArgs: 5,
  init: (client) => {
    const checkForPosts = async () => {
      const query = {
        date: {
          $lte: Date.now(),
        },
      }

      const results = await scheduledSchema.find(query)

      for (const post of results) {
        const { guildId, channelId, content } = post

        const guild = await client.guilds.fetch(guildId)
        if (!guild) {
          continue
        }

        const channel = guild.channels.cache.get(channelId)
        if (!channel) {
          continue
        }

        channel.send(content)
      }

      await scheduledSchema.deleteMany(query)

      setTimeout(checkForPosts, 1000 * 10)
    }

    checkForPosts()
  },
  callback: async ({ message, args }) => {
    const { mentions, guild, channel } = message

    const targetChannel = mentions.channels.first()
    if (!targetChannel) {
      message.reply('Please tag a channel to send your message in.')
      return
    }

    // Remve the channel tag from the args array
    args.shift()

    const [date, time, clockType, timeZone] = args

    if (clockType !== 'AM' && clockType !== 'PM') {
      message.reply(
        `You must provide either "AM" or "PM", you provided "${clockType}"`
      )
      return
    }

    const validTimeZones = momentTimezone.tz.names()
    if (!validTimeZones.includes(timeZone)) {
      message.reply(
        'Unknown timezone! Please use one of the following: <https://gist.github.com/AlexzanderFlores/d511a7c7e97b4c3ae60cb6e562f78300>'
      )
      return
    }

    const targetDate = momentTimezone.tz(
      `${date} ${time} ${clockType}`,
      'YYYY-MM-DD HH:mm A',
      timeZone
    )

    message.reply('Please send the message you would like to schedule.')

    const filter = (newMessage) => {
      return newMessage.author.id === message.author.id
    }

    const collector = new MessageCollector(channel, filter, {
      max: 1,
      time: 1000 * 60, // 60 seconds
    })

    collector.on('end', async (collected) => {
      const collectedMessage = collected.first()

      if (!collectedMessage) {
        message.reply('You did not reply in time.')
        return
      }

      message.reply('Your message has been scheduled.')

      await new scheduledSchema({
        date: targetDate.valueOf(),
        content: collectedMessage.content,
        guildId: guild.id,
        channelId: targetChannel.id,
      }).save()
    })
  },
}
MatteoBax commented 2 years ago

So how can I fix it?

MatteoBax commented 2 years ago

Can anyone help me please?

tippfehlr commented 2 years ago

I am looking over your code and maybe I can say about it.

Small side note though:

message.reply('You must provide either "AM" or "PM", you provided "${clockType}"' )

I would think about abuse here: if this is a public bot users could enter bad words and the bot would display them. Could theoretically be that your bot is banned in servers with automoderation because of this.

EDIT: I don't get how to escape a ` so i replaced them with '

MatteoBax commented 2 years ago

Thanks for the answer but it crashes anyway and gives the same error

MatteoBax commented 2 years ago

Can anyone help me please?

MatteoBax commented 2 years ago

Can anyone help me please?