Skip to content

Instantly share code, notes, and snippets.

@acollierr17
Last active July 3, 2021 15:32
Show Gist options
  • Select an option

  • Save acollierr17/bf159f95e36adac098f1cc09a347af42 to your computer and use it in GitHub Desktop.

Select an option

Save acollierr17/bf159f95e36adac098f1cc09a347af42 to your computer and use it in GitHub Desktop.
Discord.js Rock, Paper, Scissors (Basic command handler included)
const { Client } = require('discord.js');
const client = new Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
const prefix = '!';
client.on('message', message => {
if (message.author.bot) return;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === 'rps') {
const acceptedReplies = ['rock', 'paper', 'scissors'];
const random = Math.floor((Math.random() * acceptedReplies.length));
const result = acceptedReplies[random];
const choice = args[0];
if (!choice) return message.channel.send(`How to play: \`${prefix}rps <rock|paper|scissors>\``);
if (!acceptedReplies.includes(choice)) return message.channel.send(`Only these responses are accepted: \`${acceptedReplies.join(', ')}\``);
console.log('Bot Result:', result);
if (result === choice) return message.reply("It's a tie! We had the same choice.");
switch (choice) {
case 'rock': {
if (result === 'paper') return message.reply('I won!');
else return message.reply('You won!');
}
case 'paper': {
if (result === 'scissors') return message.reply('I won!');
else return message.reply('You won!');
}
case 'scissors': {
if (result === 'rock') return message.reply('I won!');
else return message.reply('You won!');
}
default: {
return message.channel.send(`Only these responses are accepted: \`${acceptedReplies.join(', ')}\``);
}
}
}
});
client.login('TOKEN');
@BloodyBlink1982
Copy link
Copy Markdown

SyntaxError: Identifier 'client' has already been declared

SourceCode:
const client = new Client();

const prefix = 'i/';

client.on('message', message => {

if (message.author.bot) return;
if (message.content.indexOf(prefix) !== 0) return;

const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();

if (command === 'i/rps') {
    const acceptedReplies = ['rock', 'paper', 'scissors'];
    const random = Math.floor((Math.random() * acceptedReplies.length));
    const result = acceptedReplies[random];

    const choice = args[0];
    if (!choice) return message.channel.send(`How to play: \`${prefix}rps <rock|paper|scissors>\``);
    if (!acceptedReplies.includes(choice)) return message.channel.send(`Only these responses are accepted: \`${acceptedReplies.join(', ')}\``);
    
    console.log('Bot Result:', result);
    if (result === choice) return message.reply("It's a tie! We had the same choice.");
    
    switch (choice) {
        case 'rock': {
            if (result === 'paper') return message.reply('I won!');
            else return message.reply('You won!');
        }
        case 'paper': {
            if (result === 'scissors') return message.reply('I won!');
            else return message.reply('You won!');        
        }
        case 'scissors': {
            if (result === 'rock') return message.reply('I won!');
            else return message.reply('You won!');
        }
        default: {
            return message.channel.send(`Only these responses are accepted: \`${acceptedReplies.join(', ')}\``);
        }
    }
}

});

@acollierr17
Copy link
Copy Markdown
Author

acollierr17 commented Apr 1, 2021 via email

@theNeoJade
Copy link
Copy Markdown

That was ironically good timing to say you don't support it btw.

@weixik
Copy link
Copy Markdown

weixik commented Jul 3, 2021

Uhm, hello. Im kinda new in this, I want to ask if you can make scoring system. Thanks.

@acollierr17
Copy link
Copy Markdown
Author

acollierr17 commented Jul 3, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment