Home > Back-end >  message event updating discord.js from v12 to v13
message event updating discord.js from v12 to v13

Time:11-15

I have a discord bot, and I was using v12. When the new v13 version came out, I tried to update my bot but it was too much for me. Now, I tried again and it went a little better. Now at least it gets on, but doesn´t reply! When I execute the help command, it returns me this error:

TypeError: message.channel.startTyping is not a function
at Client.module.exports (/app/events/messageCreate.js:172:21)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

This is my bot.js file:

const { Client, Intents } = require('discord.js');
const Discord = require("discord.js");
const { I18n } = require("locale-parser");
const Mongoose = require("mongoose");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
//client.errors = require("./modules/errors");
client.config = require("./config.json");
client.i18n = new I18n({ defaultLocale: "en" });
Mongoose.connect(client.config.mongo_uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

client.database = Mongoose.connection;
client.database.on("error", err => {
  throw err;
});
client.database.once("open", async () => {
  require("./models");
  require("./handlers/eventHandler")(client);
  require("./handlers/moduleHandler")(client);
  client.login(process.env.BOT_TOKEN);
});

And this one, is app/events/messageCreate.js:

const Discord = require("discord.js");
const embeds = require("../modules/embeds.js");
const cooldown = {};
const Uses = require("../models/uses.js");
const Enmap = require("enmap");
const db = require("megadb"); //aca definimos db para lo que necesitemos mas adelante
let desactivadodb = new db.crearDB("ComandosDesactivados");
const fs = require("fs");
const guildModel = require("../models/guildModel");
var language = "";
module.exports = async message => {
  try {
    const bot = message.client;
    const funcs = require("../modules/functions.js");
    bot.color = funcs.color(message);
    bot.lang = funcs.lang(message) || "en";
    bot.langs = message.guild.language || language;

    const msg = message;

    if (msg.author.bot || !msg.guild) return;


    let prefix = bot.config.prefixes[0];

    let argsSlice = prefix.length;

    if (!msg.content.toLowerCase().startsWith(prefix.toLowerCase())) {
      let content = msg.content.trim();
      let mention1 = "<@!"   bot.user.id   ">";
      let mention2 = "<@"   bot.user.id   ">";

      if (content == mention1 || content == mention2)
        return embeds.mention(msg, prefix, bot);

      if (content.startsWith(mention1)) argsSlice = mention1.length;
      else if (content.startsWith(mention2)) argsSlice = mention2.length;
      else return;
    }

    let args = msg.content
      .slice(argsSlice)
      .trim()
      .split(/  /g);
    let command = args.shift().toLowerCase();
    let cmdFile =
      bot.commands.get(command) ||
      bot.commands.find(
        cmdFile => cmdFile.aliases && cmdFile.aliases.includes(command)
      );

    if (!cmdFile) return;

    if (message.guild && !message.guild.language) {
      let language = "en";
      let guildDocument = await guildModel.findOne({
        guildID: message.guild.id
      });
      if (guildDocument && guildDocument.language)
        message.guild.language = guildDocument.language;
    }
    if (!message.guild) {
      let language = "en"
    };
  
    if (cmdFile.guildOnly && !message.guild)
      return await message.channel.send(
        message.client.i18n.get(language, "errors", "command_guild_only")
      );

    if (
      !cmdFile.enabled ||
      desactivadodb.tiene(
        `ComandosDesactivados_${command}_${message.guild.id}`,
        `g${message.guild.id}`
      )
    )
      return await message.channel.send(
        message.client.i18n.get(
          message.guild.language,
          "errors",
          "command_disabled"
        )
      );

    if (
      cmdFile.ownerOnly &&
      !message.client.config.owners.includes(message.author.id)
    )
      return await message.channel.send(
        message.client.i18n.get(
          language,
          "errors",
          "command_owner_only",
          { command: cmdFile.name }
        )
      );
    if (cmdFile.nsfwOnly && !message.channel.nsfw)
      return await message.channel.send(
        message.client.i18n.get(language, "errors", "nsfw_only", {
          command: cmdFile.name
        })
      );

    if (
      cmdFile.permissions &&
      !//   message.client.config.owners.includes(message.author.id) ||
      message.member.permissions.has(cmdFile.permissions || "ADMINISTRATOR")
    )
      return await message.channel.send(
        message.client.i18n.get(
          language,
          "errors",
          "not_enough_permission",
          { permissions: cmdFile.permissions.join(", ") }
        )
      );
    if (
      cmdFile.botpermissions &&
      !message.guild.me.permissions.has(
        cmdFile.botpermissions || "ADMINISTRATOR"
      )
    )
      return await message.channel.send(
        message.client.i18n.get(
          language,
          "errors",
          "not_bot_enough_permission",
          { permissions: cmdFile.botpermissions.join(", ") }
        )
      );

    let numuses = 1;
    Uses.findOne(
      {
        command: cmdFile.name
      },
      (err, res) => {
        if (err) console.log(err);

        if (!res) {
          const newDoc = new Uses({
            command: cmdFile.name,
            uses: 0
          });
          newDoc.save().catch(err => console.log(err));
        } else {
          res.uses = res.uses   numuses;
          res.save().catch(err => console.log(err));
        }
      }
    );
    if (cmdFile.cooldown) {
      if (!cooldown[msg.author.id]) cooldown[msg.author.id] = {};

      let time = cooldown[msg.author.id][cmdFile.name] || 0;
      if (time && time > Date.now()) {
        let wait = ((time - Date.now()) / 1000).toFixed(2);
        return message.channel.send(
          message.client.i18n.get(
            language,
            "errors",
            "wait_cooldown",
            { cooldown: wait, command: cmdFile.name }
          )
        );
      }
      cooldown[msg.author.id][cmdFile.name] =
        Date.now()   cmdFile.cooldown * 1000;
    }

    message.channel.startTyping();
    cmdFile.exec(bot, msg, args).then(message.channel.stopTyping());
  } catch (err) {
    console.error(err);
  }
};

I just changed the Client thing of the bot.js, but I think I don´t understand the actual module.exports and I am very sure that there is where the error is.

CodePudding user response:

In discord.js v13 TextChannel.startTyping() method was replaced by TextChannel.sendTyping() which you have to use now! As moving to v13 guide says: "This method automatically stops typing after 10 seconds, or when a message is sent"!

  • Related