#Posting gifs into a channel and the bot automatically records the link into a database

51 messages · Page 1 of 1 (latest)

serene horizon
#

Hello!

I am not familiar with discord.js and not a very good coder, so I am relying on tutorials to code my own bot.

The bot I have already has commandhanders and I also already connected it to a mongoose database.

So, the idea is:
The bot should grab an uploaded gif from a specific channel and record it to a mongoose database. Additionally the gif-upload in that channel also has one Gifname as text in the same upload message.

And then, when I type /gif [tag] with tag being the text string in the upload, the bot looks up the database and posts the gif according to that specific tag.

I searched for something like this for days now, but no success. I don't even know how to call that. So I'm asking here if someone know a similiar type of thing or even a tutorial on how to do that.

Much thanks! PEPEcute

hallow cliff
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

serene horizon
#

Push!

coarse sleet
#

Are they being added with a command or just posted?

#

like this for example

serene horizon
#

in that mongoose database

#

And he gets the string from the channel (as an example) #gifs-for-bot, that I post a gif in with a given tag

#

now that i think about it

#

it could be easier if i just post a link into that channel and the bot just records it

#

so the order of operations would be this:

I post a gif with a text: [gif-link] [text]

bot records it into a mongoose database:
String 0: [gif-link]
String 1: [text]

I type in a different channel: /gif [string]
And now the bot searches for this particular string in the database and if found, it just posts the gif-link according to [string 1].

weary panther
#

And what part of that flow do you need help with?

serene horizon
# weary panther And what part of that flow do you need help with?

well i do not know how I can do that from scratch, I'm not very good with coding in js just yet.

I do however already set up the database model:

const { Schema, model } = require('mongoose');

const scgifSchema = new Schema({
  giflink: {
    type: String,
    required: true,
  },
  tag: {
    type: String,
    required: true,
  },
});

module.exports = model('SCgifs', scgifSchema);```
weary panther
#

You start by implementing one of those steps, then the next. Hence my question: which of those steps do you want/need help with (right now)?

serene horizon
weary panther
#

Why not use a slashcommand for that too? With a StringOption for the tag and either a StringOption for url or AttachmentOption for uploaded image?

#

Way better UX for the users and way less work for you to parse

serene horizon
#

yeah i have no idea how, thats the thing. I would ask someone to write code, but that's a no-go here and I would actually love to learn it myself.

#

I have this bit, where I followed a tutorial with:

const { Client, Interaction, ApplicationCommandOptionType } = require('discord.js');
const User = require('../../models/User');

module.exports = {
  /**
   *
   * @param {Client} client
   * @param {Interaction} interaction
   */
  callback: async (client, interaction) => {
    if (!interaction.inGuild()) {
      interaction.reply({
        content: 'You can only run this command inside a server.',
        ephemeral: true,
      });
      return;
    }

    const targetUserId = interaction.options.get('user')?.value || interaction.member.id;

    await interaction.deferReply();

    const user = await User.findOne({ userId: targetUserId, guildId: interaction.guild.id });

    if (!user) {
      interaction.editReply(`<@${targetUserId}> doesn't have a profile yet.`);
      return;
    }

    interaction.editReply(
      targetUserId === interaction.member.id
        ? `Your wewcoin balance is **${user.balance}**`
        : `<@${targetUserId}>'s wewcoin amount is **${user.balance} wewcoins** `
    );
  },

  name: 'wewcoin',
  description: "See yours/someone else's wewcoin balance",
  options: [
    {
      name: 'user',
      description: 'The user whose balance you want to get.',
      type: ApplicationCommandOptionType.User,
    },
  ],
};```
vital sigilBOT
#

guide Creating Your Bot: Creating slash commands
read more

serene horizon
#

lets see

serene horizon
#

@weary panther I got this far:

#
const { Client, Interaction, ApplicationCommandOptionType } = require('discord.js');
const SCgifs = require('../../models/SCgifs');

module.exports = {
  /**
   *
   * @param {Client} client
   * @param {Interaction} interaction
   */
  callback: async (client, interaction) => {
    if (!interaction.inGuild()) {
      interaction.reply({
        content: 'You can only run this command inside a server.',
        ephemeral: true,
      });
      return;
    }

    const reply = await interaction.fetchReply();

    const SCgif = interaction.options.get('SCgiflink')?.value    
    const tag = interaction.options.get('tag')?.value

    


    interaction.editReply(
      `Gif added!`
    );
  },

  name: 'scgif_record',
  description: "Record Star Citizen gifs",
  options: [
    {
      name: 'gif',
      description: 'The link of the gif',
      type: ApplicationCommandOptionType.User,
    },
    {
      name: 'tag',
      description: 'The unique tag of the specific gif',
      type: ApplicationCommandOptionType.User,
    },
  ],
};```
#

but now what?

weary panther
#

Use getString(…) instead of get(…).value for higher type safety. And now you just need to SCgifs.create your entry in your db with those two values you got now.

#

Not 100% familiar with mongoose, it‘s either .create or new SCgifs(…)

serene horizon
#

It also did create the database, but without any entries

#

Here is the full code:

#
const { Client, Interaction, ApplicationCommandOptionType } = require('discord.js');
const SCgifs = require('../../models/scgif_collection');

module.exports = {
  /**
   *
   * @param {Client} client
   * @param {Interaction} interaction
   */
  callback: async (client, interaction) => {
    if (!interaction.inGuild()) {
      interaction.reply({
        content: 'You can only run this command inside a server.',
        ephemeral: true,
      });
      return;
    }

    const reply = await interaction.fetchReply();

    const SCgif = interaction.options.getString('scgiflink');
    const tag = interaction.options.getString('scgiftag');

    SCgifs.create({
      scgiflink: SCgif,
      scgiftag: tag
    })

    interaction.editReply(
      `${SCgif} Gif added! tag: ${tag}`
    );
  },

  name: 'scgif_record',
  description: "Record Star Citizen gifs",
  options: [
    {
      name: 'gif',
      description: 'The link of the gif',
      type: ApplicationCommandOptionType.String,
    },
    {
      name: 'tag',
      description: 'The unique tag of the specific gif',
      type: ApplicationCommandOptionType.String,
    },
  ],
};```
serene horizon
#

soo

#

uh

#

im kinda lost

weary panther
#

You try to fetchReply before replying …

serene horizon
chilly echo
# serene horizon So what should i switch up then?

First in your command your option names are gif and tag

But in your code you are searching getString("scgiflink") and getString("scgiftag").

The option name needs to be same as the one you set otherwise you won't get the value you are looking for.

2nd thing is, you did interaction.fetchReply() but there is no reply to fetch since you haven't replied to the interaction yet. You can just omit the line as you won't be using it

3rd thing is you have interaction.editReply() in your code, but you have nothing to edit. Just use interaction.reply()

serene horizon
#

I'm going to try to fix it myself first tho

serene horizon
#

I did it!!!!!!!!!!!

#

it works!

#

Now i need to figure out how to let the bot post that gif and how i can have the tags be searchable

serene horizon
#

So, how do search in that database and have a selectable table full of those scgiftag ?

weary panther
#

Consult the docs for your database package. That part‘s not djs related

serene horizon
#

hmmm

#

i have a hard time to even find them

#

one second

weary panther
#

First result searching for mongoose actually (before the animal even)