【问题标题】:How do you use regex With a Discord.js Counting system如何在 Discord.js 计数系统中使用正则表达式
【发布时间】:2022-12-08 01:46:42
【问题描述】:

所以我想使用正则表达式来检测在不和谐频道中发送的最后一个数字,然后机器人将只允许发送下一个数字所以我不必在机器人重新启动时继续更新代码。我添加了一些行以帮助更好地查看代码的计数部分

我的代码

const Discord = require(`discord.js`);

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

require('dotenv').config();

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

//-------------------------------------------------------------

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

let count = 481;
let timeout;

client.on('message', (message) => {
    let { channel, content, member } = message
  if (channel.id === '951944076641591356') {
    if (member.user.bot) return;

    if (Number(content) === count + 1) {
      count++;

      if (timeout) clearTimeout(timeout);

      timeout = setTimeout(
        () => channel.send(++count).catch(console.error),

        1800000
      );
    } else if (member.id !== client.user.id) {
      
      channel.send(`${member} messed up!`).then(msg => msg.delete({timeout: 1000}));
      message.delete({timeout: 1000})


      if (timeout) clearTimeout(timeout);
      
    }
  }
});


//-------------------------------------------------------------

['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord);
})

client.on("ready", () => {
    
    client.user.setActivity('HMMM', { type: "WATCHING"}).catch(console.error)
});

client.login(process.env.TOKEN);

【问题讨论】:

    标签: node.js regex discord.js


    【解决方案1】:

    您需要执行此操作的方法是查看计数是否已更新为当前计数,如果没有,则在机器人刚刚启动时缓存最后 50 条消息并获取其内容。

    首先,将计数设置为-1

    let count = -1;
    

    这将使我们能够查看计数是否已更新。

    接下来在行之间:if (member.user.bot) return;if (Number(content) === count + 1) { 添加以下内容:

    if(count === -1){
        // Get the last 50 messages
        const channelMessages = await message.channel.messages.fetch();
        
        // Get the second last messages content, the last message is the one the user just sent    
        const lastCount = channelMessages.at(1)?.content;
        
        // If it doesnt exist, or its not a number return (you could do something such as delte it)
        if (!lastCount || !/^[0-9]+$/.test(lastCount)) return;
        
        //Finally update the current count
        count = Number(lastCount);
    }
    

    该代码应该只运行一次,在发送一条消息并且机器人刚刚启动之后,因为计数随后将更新为 -1 以外的其他内容,因此 if 语句中的代码块将不会运行。

    最终代码将类似于:

    let count = -1;
    let timeout;
    
    client.on('message', async (message) => {
        let { channel, content, member } = message
      if (channel.id === '951944076641591356') {
        if (member.user.bot) return;
    
    if(count === -1){
        // Get the last 50 messages
        const channelMessages = await message.channel.messages.fetch();
        
        //Get the second last messages content    
        const lastCount = channelMessages.at(1)?.content;
        
        // If it doesnt exist, or its not a number return (you could do something such as delte it)
        if (!lastCount || !/^[0-9]+$/.test(lastCount)) return;
        
        //Finally update the current count
        count = Number(lastCount);
        }
    
        if (Number(content) === count + 1) {
          count++;
    
          if (timeout) clearTimeout(timeout);
    
          timeout = setTimeout(
            () => channel.send(++count).catch(console.error),
    
            1800000
          );
        } else if (member.id !== client.user.id) {
          
          channel.send(`${member} messed up!`).then(msg => msg.delete({timeout: 1000}));
          message.delete({timeout: 1000})
    
    
          if (timeout) clearTimeout(timeout);
          
        }
      }
    });
    

    【讨论】:

    • 对不起,我忘了回复,但这对我不起作用我收到错误const channelMessages = await message.channel.messages.fetch(); ^^^^^ SyntaxError: await is only valid in async functions and the top level bodies of modules
    • 固定的!我忘了将函数声明为async
    猜你喜欢
    • 2010-10-29
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多