-
-
Save acollierr17/bf159f95e36adac098f1cc09a347af42 to your computer and use it in GitHub Desktop.
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'); |
It doesnt work... only the choosing works.
Can you expand on this? I haven't touched this code in years, so your further feedback would be great.
Have you tried to fix the issue? If so, what have you come across?
It doesnt work... only the choosing works.
Can you expand on this? I haven't touched this code in years, so your further feedback would be great.
Have you tried to fix the issue? If so, what have you come across?
I have tried to fix it but I can't :D the problem is that when I do the prefix and rps, it starts doing the code up until when I have to choose rock, paper, or scissors. I can't see any errors in the code... If you could help that would be great!! :D
I have tried to fix it but I can't :D the problem is that when I do the prefix and rps, it starts doing the code up until when I have to choose rock, paper, or scissors. I can't see any errors in the code... If you could help that would be great!! :D
I will take a look at this. Thanks for reporting the issue!
Np!
So the code works as intended. However, I didn't make it clear that you have to provide either rock
, paper
, or scissors
as an argument to play the game. I made some significant updates to the code to make things look cleaner and overall helps the end-user play the game better.
So whenever I type the rps command it replies but only said only these responses are accepted, I tried doing the args thing you said but I don't exactly know what that means. Could you help?
So whenever I type the rps command it replies but only said only these responses are accepted, I tried doing the args thing you said but I don't exactly know what that means. Could you help?
You do !rps rock|paper|scissors
as stated in the default
case. The default
case runs when the condition of the switch statement doesn't match any of the cases defined. So !rps pencil
wouldn't work as the argument/choice pencil
isn't allowed.
Whenever i use the bot it always wins!
made this a command handler yeah?
made this a command handler yeah?
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.
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(', ')}\``);
}
}
}
});
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.
It doesnt work... only the choosing works.