{"size": 1789, "ext": "js", "max_stars_repo_path": "discord.js", "max_stars_repo_name": "Omnimorphs/omnifusion-bot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "discord.js", "max_issues_repo_name": "Omnimorphs/omnifusion-bot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "discord.js", "max_forks_repo_name": "Omnimorphs/omnifusion-bot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5230769231, "max_line_length": 138, "alphanum_fraction": 0.6484069312}
const Discord = require('discord.js');

const intents = new Discord.Intents();

intents.add(Discord.Intents.FLAGS.GUILDS);

const discordBot = new Discord.Client({intents});

let discordChannel;

const discordSetup = async () => {
  return new Promise((resolve, reject) => {
    ['DISCORD_BOT_TOKEN', 'DISCORD_CHANNEL_ID'].forEach((envVar) => {
      if (!process.env[envVar]) reject(`${envVar} not set`)
    })

    if (!discordChannel) {
      discordBot.login(process.env.DISCORD_BOT_TOKEN);
      discordBot.on('ready', async () => {
        console.log('bot connected');
        const channel = await discordBot.channels.fetch(process.env.DISCORD_CHANNEL_ID);
        discordChannel = channel;
        resolve(channel);
      });
    } else {
      resolve(discordChannel);
    }
  })
}

/**
 *
 * @param {{sender: string, toBurn: number, toFuse: number, imageUrl}} fusion
 * @return {MessageEmbed}
 */
const buildMessage = (fusion) => (
  new Discord.MessageEmbed()
    .setColor('#0099ff')
    .setTitle(`New OmniFusion!`)
    .setURL(`https://opensea.io/assets/0xb5f3dee204ca76e913bb3129ba0312b9f0f31d82/${fusion.toFuse}`)  // TODO - do all fusion have an URL?
    .addFields(
      { name: 'Owner', value: fusion.sender },
      { name: 'Fused token', value: fusion.toFuse.toString() },
      { name: 'Burned token', value: fusion.toBurn.toString() }
    )
    .setAuthor('OmniFusion Bot')
    .setTimestamp(new Date())
    .setImage(fusion.imageUrl)
    .setFooter('https://fusion.omnimorphs.com')
);

/**
 *
 * @param {{sender: string, toBurn: number, toFuse: number, imageUrl}} fusion
 */
const send = async (fusion) => {
  const channel = await discordSetup();
  const message = buildMessage(fusion);
  return channel.send({embeds: [message]});
}

module.exports = {
  send
}
