简体   繁体   English

我正在尝试制作一个简单的 Discord 机器人来告诉时间

[英]I am trying to make a simple Discord bot that tells the time

I am attempting to make a very simple Discord bot with JavaScript on Mac that tells the time in "2:54" format.我正在尝试在 Mac 上使用 JavaScript 制作一个非常简单的 Discord 机器人,以“2:54”格式显示时间。 I programmed it to answer to time?我把它编程为响应time? but the console logs error messages such as "cannot send empty messages" or simply the bot doesn't answer.但控制台会记录错误消息,例如“无法发送空消息”或机器人不回答。

The connection to the discord client works fine, but the messages don't.与 discord 客户端的连接工作正常,但消息没有。

Here is the full code:这是完整的代码:

var discord = require('discord.js')
var client = new discord.Client

client.login("")
var clientlogin = ("Time bot")

client.on("ready", function() {
  console.log("Logged in as 'Time bot'")
});

client.on('message', msg => {
  if (msg.content === 'time?') {


    var date = new Date();

    var Hour = date.getHours();
    var Minute = date.getMinutes();
    var Second = date.getSeconds();

    var Time = (Hour, Minute, Second);

    client.on('message', msg => {
      msg.channel.send(Time);
      console.log(Time);
    });
  }
});

The solution to this problem is probably simple, but I cannot get ahold of it.这个问题的解决方案可能很简单,但我无法掌握它。

You're facing this issue because of this part in your code:由于代码中的这一部分,您正面临此问题:

client.on('message', msg => {
  if (msg.content === 'time?') {


    var date = new Date();

    var Hour = date.getHours();
    var Minute = date.getMinutes();
    var Second = date.getSeconds();

    var Time = (Hour, Minute, Second);

    // This is the badly behaving code!
    client.on('message', msg => {
      msg.channel.send(Time);
      console.log(Time);
    });
  }
});

The code inside the client.on('message', msg =>... will be subscribed to the message received event, so the next time your bot receives a message it will fire. Since this function does not check for the time? content, it will actually keep sending the message in an infinite loop and hit Discord's rate limiting. client.on('message', msg =>...中的代码将订阅消息接收事件,因此下次您的机器人收到消息时它将触发。既然这个 function 不检查time?内容,它实际上会在无限循环中继续发送消息并达到 Discord 的速率限制。

Another problem is your time formatting.另一个问题是您的时间格式。 You could replace var Time = (Hour, Minute, Second);您可以替换var Time = (Hour, Minute, Second); with var Time = `${Hour}:${Minute}`; with var Time = `${Hour}:${Minute}`; to make the message display in "hh:mm" format.使消息以“hh:mm”格式显示。 You can append :${Second} after the ${Minute} part to also display seconds in your messages: (hh:mm:ss)您可以 append :${Second}${Minute}部分之后也显示您的消息中的秒数: (hh:mm:ss)

If you want to make your numbers padded with zeros, you could do var Time = `${String(Hour).padStart(2, '0')}:${String(Minute).padStart(2, '0')}`;如果你想让你的数字用零填充,你可以做var Time = `${String(Hour).padStart(2, '0')}:${String(Minute).padStart(2, '0')}`;

You don't need that second client.on('message'... as you're already listening for the incoming messages.你不需要第二个client.on('message'...因为你已经在监听传入的消息了。

Time in var Time = (Hour, Minute, Second); var Time = (Hour, Minute, Second); will only store the seconds, not the formatted time.只会存储秒数,而不是格式化的时间。 However, this bot can be easily made using the toLocaleTimeString() to format the current date.但是,可以使用toLocaleTimeString()轻松制作此机器人来格式化当前日期。

You can check the incoming message, if it's time?您可以检查传入的消息,如果time? , you can create a new date, format it, and send it: ,您可以创建一个新日期,对其进行格式化并发送:

const { Client } = require('discord.js');

const client = new Client();

client.login(TOKEN);

client.on("ready", function() {
  console.log("Logged in as 'Time bot'")
});

client.on('message', (msg) => {
  if (msg.author.bot) return;
  if (msg.content === 'time?') {
    const formattedTime = new Date().toLocaleTimeString([], { timeStyle: 'short' });

    msg.channel.send(formattedTime);
  }
});

PS: I'm not sure what you mean by that "2:54" format . PS:我不确定您所说的“2:54”格式是什么意思。

You don't need the second client.on('message' msg => {您不需要第二个client.on('message' msg => {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图制作一个不和谐的音乐机器人,但每次我运行代码时,我都会收到消息未定义错误 - Im trying to make a discord music bot, but every time i run the code i get message is not defined error 我在做什么错(Discord上的简单播音员Bot) - What am I doing wrong (Simple Announcer Bot on Discord) 我正在尝试让 discord 机器人根据有人加入服务器后的时间赋予角色 - I'm trying to make a discord bot give roles base on the time since somebody has joined the server 我正在尝试用 JavaScript 编写一个 Discord 机器人,但遇到了问题 - I am trying to write a Discord bot in JavaScript and have encountered a problem 我正在尝试打开我的 discord 机器人。 但它向我展示了这条信息。 我安装了 discord.js - I am trying to open my discord bot . But it shows me this message . I have discord.js installed Discord.js - 我正在尝试创建一个不和谐的机器人,使语音通道中的所有连接成员静音 - Discord.js - I'am trying to create a discord bot that mutes all connected members in a voicechannel 我正在尝试制作一个发送图像的 JS 机器人 - I am trying to make A JS bot who sends images 尝试制作会员计数 Discord 机器人 - Trying to make a Member Count Discord bot 尝试制作 Discord 机器人,但找不到模块 - Trying to make a Discord bot, but module cannot be found 尝试制作带有嵌入的 discord 日志记录机器人 - Trying to make a discord logging bot with embeds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM