Last active
July 3, 2021 15:32
-
-
Save acollierr17/bf159f95e36adac098f1cc09a347af42 to your computer and use it in GitHub Desktop.
Discord.js Rock, Paper, Scissors (Basic command handler included)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
finally, someone who didn't use command handler
finally, someone who didn't use command handler
Well, I don't normally provide code examples using command handlers because it's an example. It should generally be able to fit into any context.
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(', ')}\``);
}
}
}
});
No longer maintaining this. It's a simple fix if you choose to learn the
fundamentals of JavaScript.
…On Thu, Apr 1, 2021, 01:10 BloodyBlink1982 ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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(', ')}\``);
}
}
}
});
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/bf159f95e36adac098f1cc09a347af42#gistcomment-3689093>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD6QZUNWY52KUUB54BKKS5LTGP53XANCNFSM4OKF4M4A>
.
That was ironically good timing to say you don't support it btw.
Uhm, hello. Im kinda new in this, I want to ask if you can make scoring system. Thanks.
Hey,
If you read the previous comment, you would have read that I no longer
maintain this piece of code. Also, making a scoring system for you isn't in
my purview either.
…On Sat, Jul 3, 2021, 02:18 weixik ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Uhm, hello. Im kinda new in this, I want to ask if you can make scoring
system. Thanks.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/bf159f95e36adac098f1cc09a347af42#gistcomment-3801257>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD6QZUNIX2JABQLDALGS22TTV2TURANCNFSM4OKF4M4A>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to make it into one. It's super easy. Just copy and paste code inside the message event.
Simple stuff like that is possible with and without a command handler. You don't learn anyways by always needing the answers in front of you.