【问题标题】:awaitMessages from the user on discord.jsdiscord.js 上来自用户的 awaitMessages
【发布时间】:2022-03-06 23:00:41
【问题描述】:

我正在尝试制作一个系统,让用户在执行操作之前输入代码(例如发送到 dms 的验证码)。我试图了解我的代码如何等待输入代码,我遇到了 awaitMessages 标记。我试图了解如何在我的代码中正确使用它。这是我的代码。

const Discord = require("discord.js");
const config = require("./config.json");
const { MessageAttachment, MessageEmbed } = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS", "DIRECT_MESSAGE_TYPING"], partials: ['CHANNEL',] })
const RichEmbed = require("discord.js");
const prefix = "!";



client.on("messageCreate", function(message) {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const commandBody = message.content.slice(prefix.length);
  const args = commandBody.split(' ');
  const command = args.shift().toLowerCase();
  
  if (command === "news") {
    if (message.channel.type == "DM") {
      message.author.send(" ");
    }

  }
  if (command === "help") {
    message.author.send("The help desk is avaliable at this website: https://example.com/");
  }
});

client.on("messageCreate", function(message) {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const commandBody = message.content.slice(prefix.length);
  const args = commandBody.split(' ');
  const command = args.shift().toLowerCase();

  if (command === "ping") {
    const timeTaken = Date.now() - message.createdTimestamp;
    message.channel.send(`Pong! This message had a latency of ${timeTaken}ms.`);
  }
  if (command === "delete all messages") {
    const timeTaken = Date.now() - message.createdTimestamp;
    const code = Math.random(1,1000)
    message.channel.send(`Executing command. Verification Required.`);
    message.author.send(`Your code is the number ${code}.`)
    message.channel.send(`Please confirm your code. The code has been sent to your dms. Confirm it in here.`)
    message.channel.awaitMessage(code) {
      message.channel.send('confirmed. Doing the action.')
    }
  }
});

client.login(config.BOT_TOKEN)

【问题讨论】:

标签: node.js discord.js


【解决方案1】:

要等待用户的响应,您可以使用收集器。 您可以创建一个过滤器来查看发送代码的用户是否与执行命令的用户相同,以及发送的代码是否正确

const filter = (msg) => msg.author.id === message.author.id && msg.content === code;
const collector = message.channel.createMessageCollector({
  filter,
  time: 60000,
});

collector.on('collect', (collectedMessage) => {
  collector.stop('success');
  message.channel.send('confirmed. Doing the action.')
});

collector.on('end', (collected, reason) => {
  if (reason === 'success') return;
  else message.channel.send('Timeout or error');
});

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 2020-08-16
    • 2020-09-03
    • 2019-10-20
    • 2021-10-29
    • 1970-01-01
    • 2020-11-11
    • 2021-11-27
    • 2021-10-23
    相关资源
    最近更新 更多