我有一种情况,我正在编码不和谐机器人,并且我正在尝试打印一个制作十六进制颜色代码的函数。
我有什么想法可以解决它?
在发送消息时,我试图删除""它只是发送代码。
请帮忙!
谢谢!
代码 :
const Discord = require("discord.js");
const TOKEN = "TOKEN";
const PREFIX = "~"
var bot = new Discord.Client();
var servers = {};
bot.on("ready", function() {
console.log("Ready! ");
bot.user.setActivity("Do ~help for help!");
});
function generateHex() {
return "#" + Math.floor(Math.random() * 16777215).toString(16);
}
bot.on("message", function(message) {
if (message.author.equals(bot.user)) return;
if (!message.content.startsWith(PREFIX)) return;
var args = message.content.substring(PREFIX.length).split(" ");
switch (args[0].toLowerCase()) {
case "genrandomhex":
message.channel.send("generateHex")
}
});
bot.login(TOKEN);

答案 0 :(得分:1)
我很清楚这篇文章的年代,以及我对此的徒劳。我将其用作锻炼技能的练习。谁知道,也许它仍然可以帮助某个人。我也不声称自己是某种专家。可能会有更好的方法来执行任何甚至全部操作。
//START OF FILE: index.js
const Discord = require('discord.js');
/* Move these out of your main program file.
const TOKEN = "TOKEN";
const PREFIX = "~" */
const {TOKEN, PREFIX} = require('./config.json');
//See below for config.json file.
/* var , in this case should be*/ const bot = new Discord.Client();
var servers = {};
/* Note: Every time you launch this application it will set 'servers' to be empty. Also, you don't use this variable anywhere in this code. */
/* There are a few errors here...
bot.on("ready", function() { */
bot.once ('ready', () => {
console.log('Ready! ?');
bot.user.setActivity('for you to do "~help", for help!', {type: 'Watching'}); //Made an artistic change xP
});
function generateHex() {
/* Break this up a bit...
return "#" + Math.floor(Math.random() * 16777215).toString(16); */
let hexMath = Math.floor(Math.random() * 16777215);
let randHex = hexMath.toString (16);
return(randHex);
} /* I have not checked your method here. If this doesn't return a valid HEX value, then check your math. */
// Several changes from here on...
bot.on('message', message => {
if (message.author.id === bot.user.id) { return; }
if (!message.content.startsWith(PREFIX)) { return; }
/* Your commands are worth hard-coding...
var args = message.content.substring(PREFIX.length).split(" ");
switch (args[0].toLowerCase()) {
case "genrandomhex":
message.channel.send("generateHex")
} */
let msg = message.content.toLowerCase();
If (msg === PREFIX + 'genrandomhex') {
message.delete(1000);
message.channel.send('#' + generateHex());
}
})
bot.login(TOKEN);
//END OF FILE
//START OF FILE: config.json
// Replace ### with your token.
{
"TOKEN": "###",
"PREFIX": "~"
}
//END OF FILE
答案 1 :(得分:0)
这不是你如何调用函数,你可以通过输入函数名称并给它参数来调用函数。
message.channel.send(generateHex())
如果这是您第一次使用javascript,请先查看一些基本的javascript教程