javascript 我做错了什么(Discord 上的简单播音员机器人)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34344335/
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
What am I doing wrong (Simple Announcer Bot on Discord)
提问by Nonia Buisness
Disclaimer; I've never really looked a cold beyond once or twice prior to today.
The base Bot I got is from here;
but I end up with this in cmd prompt when I try to run it:
免责声明;在今天之前,我从来没有真正感冒过一两次。我得到的基础 Bot 来自这里;
但是当我尝试运行它时,我在 cmd 提示符下结束了这个:
C:\Users\aer\Desktop\discordbotjson>node start
C:\Users\aer\Desktop\discordbotjson\start.js:9
var channel = client.channels.get("name", "general").id;
^
ReferenceError: client is not defined
at Client.<anonymous> (C:\Users\aer\Desktop\discordbotjson\start.js:9:16)
at emitNone (events.js:67:13)
at Client.emit (events.js:166:7)
at WebSocket.websocket.onmessage (C:\Users\aer\Desktop\discordbotjson\node_modules\discord.js\lib\Client\InternalClient.js:1047:13)
at WebSocket.onMessage (C:\Users\aer\Desktop\discordbotjson\node_modules\ws\lib\WebSocket.js:414:14)
at emitTwo (events.js:87:13)
at WebSocket.emit (events.js:172:7)
at Receiver.ontext (C:\Users\aer\Desktop\discordbotjson\node_modules\ws\lib\WebSocket.js:797:10)
at C:\Users\aer\Desktop\discordbotjson\node_modules\ws\lib\Receiver.js:473:18
at Receiver.applyExtensions (C:\Users\aer\Desktop\discordbotjson\node_modules\ws\lib\Receiver.js:360:5)
This is what the start.json
looks like:
这是start.json
外观:
var Discord = require("discord.js");
var schedule = require('node-schedule');
var AuthDetails = require("./auth.json");
var bot = new Discord.Client();
bot.on('ready', () => {
// change channel name to name of channel or just set to equal the channel ID if you already know it.
var channel = client.channels.get("name", "general").id;
var rule = new schedule.RecurrenceRule(); // Creates new Recurrence Rule
rule.minute = 0; // Must set to 0 or scheduled job will run every minute.
rule.hour = [14, 19, 20]; // Hours are based on your system's time.
var j = schedule.scheduleJob(rule, function() {
bot.channels.get("id", channel).sendMessage("Testing");
})
/* For different announcements:
rule.hour = 8; // Set different time. Use array like above for multiple hours.
var i = schedule.scheduleJob(rule, function() {
bot.channels.get("id", channel).sendMessage("Announcement at 8AM");
})
// Repeat for more announcements
*/
console.log("Bot is ready.");
});
//login
bot.login(AuthDetails.email, AuthDetails.password);
Help would be appreciated, thanks.
帮助将不胜感激,谢谢。
回答by Svenskig
Some code examples you might find use var client = new Discord.Client();
, some others use var bot = new Discord.Client();
. They mean the same but use a different name, and since you're using the latter, you must replace all instances of the variable name client
with bot
.
您可能会发现一些代码示例使用var client = new Discord.Client();
,其他一些使用var bot = new Discord.Client();
. 它们的意思相同但使用不同的名称,并且由于您使用的是后者,因此必须将变量名称的所有实例替换client
为bot
.
回答by Exo
var Discord = require("discord.js");
var schedule = require('node-schedule');
var AuthDetails = require("./auth.json");
var bot = new Discord.Client();
bot.on('ready', () => {
var channel = bot.channels.get("name", "general").id;
var rule = new schedule.RecurrenceRule();
rule.minute = 0;
rule.hour = [14, 19, 20];
var j = schedule.scheduleJob(rule, function() {
bot.channels.get("id", channel).sendMessage("Testing");
})
console.log("Bot is ready.");
});
bot.login(AuthDetails.email, AuthDetails.password);
回答by Occidi
Bot is your instance of a Discord Client it's normal that client.channels.()
doesn't return anything since client is not defined anywhere.
Bot 是您的 Discord 客户端实例,client.channels.()
因为客户端未在任何地方定义, 所以不返回任何内容是正常的。
Just replace client with your instance of a client which is here "bot"
只需将客户端替换为您的客户端实例,此处为“bot”
var channel = bot.channels.get("name", "general").id;
回答by popgoesme700
This code should work;
var channel = bot.channels.find("name", "general");
channel.sendMessage("Hello Owner Just Restarted Me!");
这段代码应该可以工作;
var channel = bot.channels.find("name", "general");
channel.sendMessage("Hello Owner Just Restarted Me!");
回答by user5782303
Replace var Discord = require("discord.js");
with var bot = require("discord.js");
替换var Discord = require("discord.js");
为var bot = require("discord.js");
Hope this helps!
希望这可以帮助!