This is an RPS command meant as an add on to StarsBlaster's Economy App: https://autocode.com/app/starsblaster/economy-app/. You can use it to wager money and play against the bot with !rps <amount> <sign>. If you have any issues, DM me on Discord at UnknownUseristhebest#7474.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
module.exports = async (event, context) => {
if (event.content.startsWith(`${process.env.prefix}rps`)) {
let cooldownActive = await lib.utils.kv['@0.1.16'].get({
key: `rps_cooldown_${event.author.id}`,
});
if (cooldownActive) {
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: `Wait for **\`10 seconds\`** please!`,
});
} else {
let text = event.content.split(' ');
let database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
if (!database.distinct.values[0]) {
await lib.discord.channels['@0.0.6'].messages.create({
channel_id: event.channel_id,
content: `You don't have a bank account, use **\`createbank\`** to make one`,
});
return;
}
if (!text[1]) {
return lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: `Please insert an amount of money`,
});
}
if (parseInt(text[1]) > parseInt(database.distinct.values[0])) {
await lib.discord.channels['@0.0.6'].messages.create({
channel_id: event.channel_id,
content: `You don't have that much money to do rps`,
});
return;
} else if (parseInt(text[1]) < 1) {
await lib.discord.channels['@0.0.6'].messages.create({
channel_id: event.channel_id,
content: `You can't do the rps with money lower than 1!`,
});
return;
} else if (text[1].match(/\D/gi)) {
await lib.discord.channels['@0.0.6'].messages.create({
channel_id: event.channel_id,
content: `Invalid amount of money`,
});
return;
}
const rps = ['rock', 'paper', 'scissors'];
const Sign = new RegExp(rps.join('|'), 'gi');
if (!text[2]) {
return lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: `Please provide a Sign. like **rock**, **paper** or **scissors**`,
});
} else if (!text[2].match(Sign)) {
await lib.discord.channels['@0.0.6'].messages.create({
channel_id: event.channel_id,
content: `Invalid Sign. It should be **rock**, **paper** or **scissors**`,
});
return;
}
await lib.utils.kv['@0.1.16'].set({
key: `rps_cooldown_${event.author.id}`,
value: event.author.id,
ttl: 10,
});
let messagePrompts = ['rock', 'paper', 'scissors'];
let messageChoice = Math.floor(Math.random() * messagePrompts.length);
let botSign = messagePrompts[messageChoice];
let userSign = text[2];
if (userSign === 'rock') {
if (botSign === 'scissors') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) + parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Rock destroys scissors\nGG, you won **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`scissors\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'rock') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `It's a draw!\nCurrent Money: **$${database.distinct.values[0]}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`rock\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'paper') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) - parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Paper covers rock\nYou lost **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`paper\`**`,
inline: true,
},
],
},
});
}
} else if (userSign === `paper`) {
if (botSign === 'rock') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) + parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Paper covers rock\nGG, you won **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`rock\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'paper') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `It's a draw!\nCurrent Money: **$${database.distinct.values[0]}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`paper\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'scissors') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) - parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Scissors cut paper\nYou lost **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`scissors\`**`,
inline: true,
},
],
},
});
}
} else if (userSign === `scissors`) {
if (botSign === 'paper') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) + parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Scissors cut paper\nGG, you won **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`paper\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'scissors') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `It's a draw!\nCurrent Money: **$${database.distinct.values[0]}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`scissors\`**`,
inline: true,
},
],
},
});
} else if (botSign === 'rock') {
database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:F`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:F`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) - parseInt(text[1]),
},
});
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
embed: {
title: `${event.author.username}'s RPS game`,
type: 'rich',
color: 0x00aa00,
description: `Rock destroys scissors\nYou lost **$${text[1]}**!\nCurrent Money: **$${updatedSheet.rows[0].fields['money']}**`,
fields: [
{
name: event.author.username,
value: `**\`${userSign}\`**`,
inline: true,
},
{
name: `YOUR_BOTS_NAME_HERE`,
value: `**\`rock\`**`,
inline: true,
},
],
},
});
}
}
}
}
};