• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

236
Vistas
How to get a discord bot to pull a random cell from a Google Sheet every time a command is used?

I'm using Autocode so I have the Google API linked to my discord bot. I have figured out how to get the bot to retrieve a distinct cell, but i want it to pull a random one each time the command is used.

For example one user wants to throw something (a list of words in a google sheet) at another user. User1 would type "/throw @User2" and it would return a message in discord that says "User1 threw frogs at @User2" or something similar.

Below is my current code, but I know it is not correct (especially the line with description)

module.exports = async (event, context) => {
  if (event.content === `${process.env.prefix}throw` || event.content === `${process.env.prefix}throwing`) {
    let text = event.content.split(' ');//splits every word in your message
    let database = await lib.googlesheets.query['@0.3.0'].distinct({
      range: `throw!A:A`,
      bounds: `FIRST_EMPTY_ROW`,
      where: [{ }],
      field: `Throw`
    });
      await lib.discord.channels['@0.1.1'].messages.create({
        channel_id: event.channel_id,
        content: ``,
        embed: {
          title: ` `,
          type: 'rich',
          color: 0xff7700,
          description: `${.getRange(resultCellRow,resultCellColumn).setValue( vA[Math.floor(Math.random()*vA.length)]);}
          g ${database.distinct.values[0]}`,//tagged user and cell pulled from Gsheet
        }
      })
  }
}
almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

After a lot of trial and error and help from Autocode support, here is the solution I came up with

module.exports = async (event, context) => {
  if (event.content === `${process.env.prefix}throw`) {
    let text = event.content.split(' ');//splits every word in your message
    let query = await lib.googlesheets.query['@0.3.0'].select({
      range: `throw!A:A`,
      bounds: `FIRST_EMPTY_ROW`,
      where: [{}],
      limit: {
        'count': 0,
        'offset': 0
      }
    });//Google Sheet query
console.log(query)

let random = Math.floor(Math.random() * (query.rows.length - 1));//pulls random row from the query
console.log(random)
let throw = query.rows[random].fields.Throw;//pulls random word from the Throw column field
console.log(throw)

      await lib.discord.channels['@0.1.1'].messages.create({
        channel_id: `${context.params.event.channel_id}`,
        content: ``,
          embed: {
            title: ``,
            description: `${throw}`,
            color: 0xd5f2fc,
          },
        });
  }
}
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda