Coinflip, the game – Full code!

Coinflip, the game – Full code!

I wrote a simple little “game” – Coinflip (aliases coin and flip). It’s really as simple as a random function that returns either 0 or 1, and assigning heads/tails as you want.
The code is without commenting for now, but in a future release there will be comments explaining the different aspects.

module.exports = {
    name: 'coinflip',
    description: 'Flip a coin!',
    guildOnly: true,
    args: false,
    aliases: 'coin,flip',
    usage: 'Flips either Heads or Tails!',
    async execute(message, args) {

        const dato = new Date();
        dato.setHours(dato.getHours() + 2);
        let sicon = message.guild.iconURL();
        const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
        message.client.channels.cache.get('LOG_CHANNEL_ID').send(`${message.author} asked to flip a coin! `+ dato.toLocaleTimeString() + dato.toLocaleDateString());
        coin = getRandomInt(2);
        let URL = '';
        let value = '';
        let coinStart = new Discord.MessageEmbed()
            .setDescription("Coinflip - The Game")
            .setColor('#0099ff')
            .setThumbnail(sicon)
            .addField('**Action**', 'Flipping.....')
            
        const coinMessage = await message.channel.send(coinStart);
        await delay(4000);
        if (coin === 0){
            URL = 'http://codelog.network/resources/coinheads.png';
            value = 'Heads';
        } else if (coin === 1){
            URL = 'http://codelog.network/resources/cointails.png';
            value = 'Tails';
        }
        let coinEmbed = new Discord.MessageEmbed()
            .setDescription("Coinflip - The Game")
            .setColor('#0099ff')
            .setThumbnail(sicon)
            .addField('__Result:__', value)
            .setImage(URL)
        coinMessage.edit(coinEmbed);
    }
}

function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
}

This is very simple and probably a bit crude, but it works and I felt thrilled when I wrote it ?
Just about everything I write, refers to a LOG_CHANNEL_ID that I use. Sort of for knowing what goes on. If you use the code, just enter the ID of a channel you use/want to use as a log or just remove the line completely. dato (Norwegian for date) converts timestamp to Norwegian time from UTC, which my host uses,

Leave a Reply

Your email address will not be published. Required fields are marked *