【问题标题】:Is there a way to make the channel name the content of the message?有没有办法让频道名称成为消息的内容?
【发布时间】:2020-12-28 22:01:09
【问题描述】:

我需要将频道名称设为最后一条消息的内容。我知道 API 限制(10 分钟内更改 2 次),所以我只希望它每 5 分钟运行一次。

这是我尝试过的:

  const prefix = " ";
    
  function myfunction () {
    if(!message.content.startsWith(prefix) || message.author.bot) return;{
      var var1 = message.content();
      channel.setName(`-counting`+ var1);
    } 
}
      
client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
    
client.on('message', message =>{
  setInterval(myfunction,);
});

【问题讨论】:

  • 您的问题是什么?错误,不工作?如果是错误代码,请将其添加到您的代码中。你到底想达到什么目的?
  • 您正在为每条消息 myFunction() 创建一个 setInterval,每 50 毫秒 重复一次。我建议只执行一次myFunction(),因为你实际上是在毫无理由地向 Discord 的 API 发送垃圾邮件。
  • 到目前为止。没有错误代码。正如我在帖子中所说,我想将频道名称更改为最新消息,但由于API限制,我只能将其限制为每5分钟更改一次。
  • 你不能那样做,这是不可能的。 Discord 专门设置了速率限制来阻止人们做这类事情。它会严重减慢 Discord 的 API 甚至 Discord 本身
  • 您只能每 5 分钟更改一次,因为您的代码效率低下并且会向 API 发送垃圾邮件。我将很快写一个答案并改进您当前的代码。

标签: node.js discord discord.js


【解决方案1】:

正如@Jakye 在 cmets 中指出的那样,您当前的代码会在每条消息上创建一个setInterval,这不是您想要的我认为的。我认为最好的做法是保存最新消息,定义一个设置频道名称的函数,并在ready 事件上创建一个setInterval。看看下面的代码示例:

const prefix = '<YOUR PREFIX>';
let latestMessage = undefined;

function updateChannelName() {
    // If the latestMessage is undefined, return out of the function
    if (!latestMessage) return;
    latestMessage.channel.setName(`-counting${latestMessage.content}`);
}

client.once('ready', () => {
    // Once the client is ready, create the interval.
    // The 5 * 60 * 1000 equals to 5 minutes in milliseconds but it's more readable than writing 300000
    setInterval(updateChannelName, (5 * 60 * 1000));
});

client.on('message', message => {
    // Check for author and prefix
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    // Update the latestMessage to the latest message
    latestMessage = message;
});

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 2022-06-13
    • 2021-11-18
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多