javascript 更改 Discord 机器人的时间和日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51316585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the time and date format for Discord bot
提问by Brandon
So, the title briefly explains my question. I'm creating a kick feature for my bot and it's been working very well. The only problem (not major) I have is with the section where it sends an embed with the kick information to the #incidentschannel which is only accessible to Bots and Staff. I've made a new Discord user account that I've created and joined it to the server. I tested my remove command and it works and does what I intend.
所以,标题简要解释了我的问题。我正在为我的机器人创建踢球功能,并且运行良好。我遇到的唯一问题(不是主要的)是它向#incidents频道发送带有踢球信息的嵌入内容的部分,该频道只有机器人和工作人员可以访问。我创建了一个新的 Discord 用户帐户并将其加入服务器。我测试了我的删除命令,它可以正常工作并按我的意愿行事。
var incidentembed = new discord.RichEmbed()
.setTitle("User removed")
.setDescription("**" + ruser + "** was successfully removed from the server.") /** ruser declares the user that was removed. **/
.setColor("#3937a5")
.addField("Time assigned", message.createdAt, true)
.addField("Assigned by", `<@${message.author.id}>`, true)
.addField("Reason", rreason, false); /** rreason declares the reason that the staff member inputted. **/
bot.channels.get("466904829550264322").send(incidentembed); /** The ID points out the #incidents channel. **/
(yes, ClassyAss is my Discord username.)
(是的,ClassyAss 是我的 Discord 用户名。)
As you can tell by the screenshot, there is something evidently wrong with the Time assignedfield. I am using message.createdAtto generate this date and time, however, how it is formatted is cluttered and confusing.
从屏幕截图中可以看出,“时间分配”字段显然有问题。我正在使用message.createdAt来生成这个日期和时间,但是,它的格式是混乱和混乱的。
I want the time and date format to be set out as DD/MM/YYYY HH:MM AM/PMbased on Australian Eastern Standard Time (AEST). How could I accomplish this?
我希望时间和日期格式根据澳大利亚东部标准时间 (AEST)设置为DD/MM/YYYY HH:MM AM/PM。我怎么能做到这一点?
采纳答案by André
As you're using the embed you can use Discord to display the time and date (it would also convert to the timestamp of the user).embed.setTimestamp().
当您使用嵌入时,您可以使用 Discord 来显示时间和日期(它也会转换为用户的时间戳)。embed.setTimestamp().
var incidentembed = new discord.RichEmbed()
.setTitle("User removed")
.setDescription("**" + ruser + "** was successfully removed from the server.") /** ruser declares the user that was removed. **/
.setColor("#3937a5")
.setTimestamp(message.createdAt)
.addField("Assigned by", `<@${message.author.id}>`, true)
.addField("Reason", rreason, false); /** rreason declares the reason that the staff member inputted. **/
bot.channels.get("466904829550264322").send(incidentembed); /** The ID points out the #incidents channel. **/
Or if you still want to format the date you could do something like this.
或者,如果您仍然想格式化日期,您可以执行以下操作。
var d = new Date,
dformat = [d.getMonth()+1,
d.getDate(),
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
Credit goes to KooiInc.
幸得KooiInc。


