[英]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.