我正在VSC中使用discord.js编码一个不一致机器人,所有命令都在响应,只有一个命令例外,这是我试图创建的票据命令 [英] I'm coding a discord bot using discord.js in VSC and all commands are responding except one, which is a ticket command I'm trying to create

查看:17
本文介绍了我正在VSC中使用discord.js编码一个不一致机器人,所有命令都在响应,只有一个命令例外,这是我试图创建的票据命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个完整的AIO Discord Bot,例如您看到的&dyno Bot";或";Carl Bot";,我已经完成了pingavatar等基本命令的编写。

我将转到一个更复杂的命令,如票证系统命令。我已经完成了所有代码,终端登录到机器人上很好,但由于某种原因,命令没有响应。我检查了其他命令以确保只有这个特定的命令。我相信这个解决方案很可能是个愚蠢的解决方案,但任何帮助都将不胜感激。

index.js

// Require the necessary discord.js classes
const fs = require('fs');
const { Client, Collection, Intents, DiscordAPIError } = require('discord.js');
const { token } = require('./config.json');
const prefex = '/';

const memberCounter = require('./counters/member-counter');

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

client.commands = new Collection();
client.events = new Collection();
const commandFiles = fs
  .readdirSync('./commands')
  .filter((file) => file.endsWith('.js'));

for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  client.commands.set(command.name, command);
}

// When the client is ready, run this code (only once)
client.once('ready', () => {
  console.log("You are now connected to Boombap's Cookout!");
  memberCounter(client);
});

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;

  const command = client.commands.get(interaction.commandName);

  if (!command) return;

  try {
    await command.execute(interaction);
  } catch (error) {
    console.error(error);
    return interaction.reply({
      content: 'There was an error while executing this command!',
      ephemeral: true,
    });
  }
});

// Login to Discord with your client's token
client.login(token);

ticket.js

module.exports = {
  name: 'ticket',
  aliases: [],
  permissions: [],
  description: 'open a ticket!',
  async execute(message, args, cmd, client, discord) {
    const channel = await message.guild.channels.create(
      `ticket: ${message.author.tag}`,
    );

    channel.setParent('932184344011898890');

    channel.updateOverwrite(message.guild.id, {
      SEND_MESSAGE: false,
      VIEW_CHANNEL: false,
    });
    channel.updateOverwrite(message.author, {
      SEND_MESSAGE: true,
      VIEW_CHANNEL: true,
    });

    const reactionMessage = await channel.send(
      'Thank you for contacting support!',
    );

    try {
      await reactionMessage.react('🔒');
      await reactionMessage.react('⛔');
    } catch (err) {
      channel.send('Error sending emojis!');
      throw err;
    }

    const collector = reactionMessage.createReactionCollector(
      (reaction, user) =>
        message.guild.members.cache
          .find((member) => member.id === user.id)
          .hasPermission('ADMINISTRATOR'),
      { dispose: true },
    );

    collector.on('collect', (reaction, user) => {
      switch (reaction.emoji.name) {
        case '🔒':
          channel.updateOverwrite(message.author, { SEND_MESSAGES: false });
          break;
        case '⛔':
          channel.send('Deleting this channel in 5 seconds!');
          setTimeout(() => channel.delete(), 5000);
          break;
      }
    });

    message.channel
      .send(`We will be right with you! ${channel}`)
      .then((msg) => {
        setTimeout(() => msg.delete(), 7000);
        setTimeout(() => message.delete(), 3000);
      })
      .catch((err) => {
        throw err;
      });
  },
};

推荐答案

您似乎正在使用discord.js的V13,但仍使用一些V12语法。

首先,channel.updateOverwrite()现在是channel.permissionOverwrites.edit()。其次,member.hasPermission()通过最新的重大更新成为member.permissions.has()。您可以在discordjs.guide上阅读有关这些更改的更多信息。

另一个错误是您使用的是SEND_MESSAGE标志,但它是SEND_MESSAGES。还有一个,您错误地使用了createReactionCollectorfilter

查看下面的代码,它对我来说很好:

module.exports = {
  name: 'ticket',
  aliases: [],
  permissions: [],
  description: 'open a ticket!',
  async execute(message, args, cmd, client, discord) {
    const channel = await message.guild.channels.create(
      `ticket: ${message.author.tag}`,
    );

    channel.setParent('932184344011898890');

    channel.permissionOverwrites.edit(message.guild.id, {
      SEND_MESSAGES: false,
      VIEW_CHANNEL: false,
    });
    channel.permissionOverwrites.edit(message.author, {
      SEND_MESSAGES: true,
      VIEW_CHANNEL: true,
    });

    const reactionMessage = await channel.send(
      'Thank you for contacting support!',
    );

    try {
      await reactionMessage.react('🔒');
      await reactionMessage.react('⛔');
    } catch (err) {
      channel.send('Error sending emojis!');
      throw err;
    }

    const filter = (reaction, user) =>
      message.guild.members.cache
        .find((member) => member.id === user.id)
        .permissions.has('ADMINISTRATOR');

    const collector = reactionMessage.createReactionCollector({
      dispose: true,
      filter,
    });

    collector.on('collect', (reaction, user) => {
      switch (reaction.emoji.name) {
        case '🔒':
          channel.permissionOverwrites.edit(message.author, { SEND_MESSAGES: false });
          break;
        case '⛔':
          channel.send('Deleting this channel in 5 seconds!');
          setTimeout(() => channel.delete(), 5000);
          break;
      }
    });

    message.channel
      .send(`We will be right with you! ${channel}`)
      .then((msg) => {
        setTimeout(() => msg.delete(), 7000);
        setTimeout(() => message.delete(), 3000);
      })
      .catch((err) => {
        throw err;
      });
  },
};

清理原始代码后,我注意到您正在使用斜杠命令。这意味着你必须做出很多小的改变。而接收多个参数(async execute(message, args, cmd, client, discord))的原始execute()方法只接收一个参数;intereaction

以下是用作斜杠命令的代码,我添加了一些注释来解释这些更改:

module.exports = {
  name: 'ticket',
  aliases: [],
  permissions: [],
  description: 'open a ticket!',
  async execute(interaction) {
    // message.guild is now interaction.guild
    const channel = await interaction.guild.channels.create(
      // message.author.tag is now interaction.user.tag
      `ticket: ${interaction.user.tag}`,
    );

    channel.setParent('932184344011898890');

    // message.guild is now interaction.guild
    channel.permissionOverwrites.edit(interaction.guild.id, {
      SEND_MESSAGES: false,
      VIEW_CHANNEL: false,
    });

    // message.author is now interaction.member
    channel.permissionOverwrites.edit(interaction.member, {
      SEND_MESSAGES: true,
      VIEW_CHANNEL: true,
    });

    // we're sending a reply with interaction.reply
    // and use fetchReply so we can delete it after 7 seconds
    const reply = await interaction.reply({
      content: `We will be right with you! ${channel}`,
      fetchReply: true,
    });

    setTimeout(() => {
      reply.delete();
    }, 7000);

    const reactionMessage = await channel.send(
      'Thank you for contacting support!',
    );

    try {
      await reactionMessage.react('🔒');
      await reactionMessage.react('⛔');
    } catch (err) {
      channel.send('Error sending emojis!');
      throw err;
    }
    // you should also check if the user reacted is a bot
    const filter = (reaction, user) =>
      !user.bot &&
      interaction.guild.members.cache
        .get(user.id)
        .permissions.has('ADMINISTRATOR');

    const collector = reactionMessage.createReactionCollector({
      dispose: true,
      filter,
    });

    collector.on('collect', (reaction, user) => {
      switch (reaction.emoji.name) {
        case '🔒':
          channel.permissionOverwrites.edit(interaction.member, {
            SEND_MESSAGES: false,
          });
          break;
        case '⛔':
          channel.send('Deleting this channel in 5 seconds!');
          setTimeout(() => channel.delete(), 5000);
          break;
      }
    });
  },
};

这篇关于我正在VSC中使用discord.js编码一个不一致机器人,所有命令都在响应,只有一个命令例外,这是我试图创建的票据命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆