text: const Discord = require('discord.js')
  const fetch = require('node-fetch')
  const client = new Discord.Client()
  require('dotenv').config()
  
  const GAS_ENDPOINT =
    'https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle'
  
  client.once('ready', () => {
    console.log('Running...')
    getGas()
  })
  
  let gasPrices = {}
  
  function getGas() {
    fetch(GAS_ENDPOINT)
      .then((res) => res.json())
      .then(({ fast, average, slow }) => {
        gasPrices = { fast, average, slow }
        client.user.setActivity(`⚡️${fast} · 🚶${average} · 🐢${slow}`, {
          type: 'WATCHING',
          url: 'https://explorer.poa.network/poa/xdai/',
        })
      })
  }
  
  // oracle updates every 5 minutes
  // run updates every 30 seconds to not get too far off from the oracle
  setInterval(getGas, 30 * 1000)
  
  client.on('message', (message) => {
    const prefix = '!gas'
    if (message.content.startsWith(prefix)) {
      let args = message.content
        .slice(prefix.length + 1)
        .trim()
        .split(' ')
      if (args[0] === '') args = args.splice(0, 0)
      if (args.length === 0) {
        message.channel.send(
          `Fast: ${gasPrices.fast}\nAverage: ${gasPrices.average}\nSlow: ${gasPrices.slow}`
        )
      }
      if (args[0] === 'alert' && args.length === 2) {
        let amount = args[1]
        let user = message.author
        let name = message.member.nickname
          ? message.member.nickname
          : message.member.user.username
        message.channel.send(
          `Thanks, ${name}. I will send you a private message when gas price drops below ${amount} gwei.`
        )
        if (!alerts.has(user)) {
          alerts.set(user, [amount])
        } else {
          let newAlertList = alerts.get(user)
          newAlertList.push(amount)
          alerts.set(user, newAlertList)
        }
      }
    }
  })
  
  client.login(process.env.DISCORD_TOKEN)
  