vkashti-bots / utils.js
utils.js
Raw
import fs from 'fs'

function formatCustomDate(dateString) {
  const date = new Date(dateString)
  const day = date.getDate()
  const months = [
    'яну.',
    'фев.',
    'март',
    'апр.',
    'май',
    'юни',
    'юли',
    'авг.',
    'септ.',
    'окт.',
    'ноем.',
    'дек.',
  ]
  const month = months[date.getMonth()]

  return `${day} ${month}`
}

async function deleteAllMessagesInChannel(channel) {
  try {
    // Fetch messages in batches of 100 (Discord limits it to 100 at a time)
    let fetched
    do {
      fetched = await channel.messages.fetch({ limit: 100 })
      await channel.bulkDelete(fetched)
    } while (fetched.size >= 100) // Continue fetching until no more messages are left
    logger('All messages have been deleted.')
  } catch (error) {
    console.error('Error deleting messages:', error)
  }
}

// formatted logger that shows formatted date and time before the message
export const logger = (message) => {
  const date = new Date()
  const formattedDate = date.toISOString().split('T')[0]
  const formattedTime = date.toTimeString().split(' ')[0]
  console.log(`[${formattedDate} ${formattedTime}] ${message}`)
}

// Helper function to get number emoji based on index (1-based)
export const getNumberEmoji = (number) => {
  const numberEmojis = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣']
  return numberEmojis[number - 1]
}

export const loadJSONFile = (filePath, defaultValue = {}) => {
  try {
    if (fs.existsSync(filePath)) {
      return JSON.parse(fs.readFileSync(filePath, 'utf-8'))
    }
  } catch (error) {
    console.error(`Error loading file ${filePath}:`, error)
  }
  return defaultValue
}

export { formatCustomDate, deleteAllMessagesInChannel }