AlexzanderFlores / WOKCommands

132 stars 61 forks source link

Might WOK Commands Handler is got a new issue when try to create collections in MongoDB. #205

Open estrng opened 2 years ago

estrng commented 2 years ago

Hellos guys, sorry in advance if this is no issue.

The scenarios is using the follow: "axios": "^0.25.0", "discord.js": "^13.6.0", "dotenv": "^14.2.0", "mongoose": "^6.1.8", "wokcommands": "^1.5.3" "@types/node": "^17.0.10", "rimraf": "^3.0.2", "typescript": "^4.5.5"

I followed the video How to connect your Discord bot to a MongoDB Database - Discord.JS v13.

It`s look like is everything good with the connection and the default collections were made:

index.ts

import DiscordJS, { Intents } from 'discord.js';
import dotenv from 'dotenv';
import path from 'path';
import WOKCommands from 'wokcommands';

dotenv.config();

const client = new DiscordJS.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MEMBERS,
    Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
    Intents.FLAGS.GUILD_PRESENCES,
  ],
});

client.on('ready', async () => {
  /* await mongoose.connect(process.env.MONGO_URI!, {
    keepAlive: true,
  }); */

  const wok = new WOKCommands(client, {
    commandsDir: path.join(__dirname, 'commands'),
    featuresDir: path.join(__dirname, 'features'),
    testServers: process.env.GUILD_ID,
    botOwners: process.env.BOT_OWNER,
    mongoUri: process.env.MONGO_URI,
  });

  wok.on('databaseConnected', (connection, state) => {
    console.log(`The connection state is "${state}"`);
  });
});

client.login(process.env.TOKEN);

Running bash:

image

warn-schema.ts

import mongoose, { Schema } from 'mongoose';

const reqString = {
  type: String,
  required: true,
};

const schema = new Schema(
  {
    userId: reqString,
    guildId: reqString,
    staffId: reqString,
    reason: reqString,
  },
  {
    timestamps: true,
  }
);

const name = 'warns';

export default mongoose.models[name] || mongoose.model(name, schema);

Defaults Collections on Atlas:

image

But the new schema warn-schema that i created weren't made i noticed that after i made the follow video Discord.JS v13 - Warn System with Slash Commands (Add, Remove, List), because i was very confident that everything would work properly because it all quite simple.

warn.ts

import { ICommand } from 'wokcommands';
import { MessageEmbed } from 'discord.js';
import warnSchema from '../../models/warn-schema';

export default {
  category: 'Moderation',
  description: 'Warns a user',

  permissions: ['ADMINISTRATOR'],
  requireRoles: true,

  slash: true, //Lagacy command and slash command
  testOnly: true, //False to turn off test only
  guildOnly: true,

  options: [
    {
      type: 'SUB_COMMAND',
      name: 'add',
      description: 'Adds a warning to a user',
      options: [
        {
          name: 'user',
          type: 'USER',
          description: 'The user to add a warn to',
          required: true,
        },
        {
          name: 'reason',
          type: 'STRING',
          description: 'The reason for the warning',
          required: true,
        },
      ],
    },
    {
      type: 'SUB_COMMAND',
      name: 'remove',
      description: 'Removes a warning from a user',
      options: [
        {
          name: 'user',
          type: 'USER',
          description: 'The user to remove a warn from',
          required: true,
        },
        {
          name: 'id',
          type: 'STRING',
          description: 'The id of the warning to remove',
          required: true,
        },
      ],
    },
    {
      type: 'SUB_COMMAND',
      name: 'list',
      description: 'Lists all warnings for a user',
      options: [
        {
          name: 'user',
          type: 'USER',
          description: 'The user to list warnings for',
          required: true,
        },
      ],
    },
  ],

  callback: async ({ guild, member: staff, interaction }) => {
    const subCommand = interaction.options.getSubcommand();
    const user = interaction.options.getUser('user');
    const reason = interaction.options.getString('reason');
    const id = interaction.options.getString('id');
    console.log('comando escolhido: ', { subCommand, user });

    if (subCommand === 'add') {
      const warning = await warnSchema.create({
        userId: user?.id,
        guildId: guild?.id,
        staffId: staff.id,
        reason,
      });

      console.log('o que aconteceu com o attempt: ', warning);

      return {
        custom: true,
        content: `Added warning ${warning.id} to <@${user?.tag}>`,
        allowedMentions: {
          users: [],
        },
      };
    } else if (subCommand === 'remove') {
      await warnSchema.findByIdAndDelete(id);

      return {
        custom: true,
        content: `Removed warning ${id} from <@${user?.id}>`,
        allowedMentions: {
          users: [],
        },
      };
    } else if (subCommand === 'list') {
      const warnings = await warnSchema.find({
        userId: user?.id,
        guildId: guild?.id,
      });

      let description = `Warnings for: <@${user?.id}>\n\n`;

      for (const warning of warnings) {
        description += `**ID:** ${warning._id}\n`;
        description += `**Date:** ${warning.createdAt.toLocaleString()}\n`; //.toUTCString
        description += `**Staff:** <@${warning.staffId}>\n`;
        description += `**Reason:** ${warning.reason}\n\n`;
      }

      const embed = new MessageEmbed().setDescription(description);

      return embed;
    }
  },
} as ICommand;

But when try to create new schema in the Mongo using the /warn command it throws the following error:

image

I had tried some the follows comments on the internet:

GitHub Issue 9732 Stackoverflow

Its look like is something new, i found this comments in the mongo official community: mongooseerror-operation-users-insertone-buffering-timed-out-after-10000ms

If anyone helps me would be very appreciated, i'll keep eye in the oficial mongo community if get something and let you know.

If this is no issue please let me know then i erase the thread.

Thank you!

EXA-Hub commented 2 years ago

i think it will work if you added dbOpthions

new WOKCommands(client, {
    debug: true,
    dbOptions: { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true }
  });

and then make sure ti whitelist your IP here image

estrng commented 2 years ago

i think it will work if you added dbOpthions

new WOKCommands(client, {
    debug: true,
    dbOptions: { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true }
  });

and then make sure ti whitelist your IP here image

I tried many things to solve. But i made my own command handler to use with db connection. So i'm not using WOK anymore.

Thanks for your reply.