我正在尝试使用node.js打造一个不和谐的机器人我只想在特定频道发布讯息我尝试这样做是为了将频道保存到变量中,但这是行不通的:
I'm trying to make a discord bot with node.js I want to post message only in specific channel I tried to do that to save a channel in a variable but that doesn't work :
const Discord = require('discord.js'); const fs = require("fs"); var bot = new Discord.Client(); var myToken = 'NDQ2OTQ1...................................................'; //client.msg = require ("./char.json"); var prefix = ("/"); //let preset = JSON.parse(fs.readFileSync('preset.json', 'utf8')); // This calls the JSON file. var offTopic = bot.channels.find("id","448400100591403024"); //.get("448392061415325697"); console.log(offTopic);
每当我运行我的机器人时,它会使用find返回"null",并使用get返回"undefined".我在Internet上寻求帮助,即使我遵循了这篇文章,我的代码也不起作用: Discord Bot无法按名称获取频道我也尝试按名称查找频道,但我也得到了未定义":/
Whenever i run my bot it returns 'null' with find and 'undefined' with get. I search for help on internet and even if i follow this post my code doesn't work : Discord Bot Can't Get Channel by Name I also tried to find my channel by name but i got 'undefined' too :/
推荐答案确保已将查找"行放在事件侦听器中.例如,假设您想在漫游器成功连接后找到特定的频道:
Make sure you've placed your "find" line within an event listener. For example, let's say you wanted to find the specific channel when the bot connects successfully:
bot.on('ready', () => { console.log(`Logged in as ${bot.user.tag}!`); var offTopic = bot.channels.get("448400100591403024"); console.log(offTopic); });当然,有大量的事件侦听器可用,这将适合您要查找频道的情况.您可以在DiscordJS的客户端类的事件"部分中找到事件侦听器文档.试试看,让我知道它是否有效.
Of course, there are a huge range of event listeners available, which will suit the scenario in which you want to find the channel. You'll find event listeners in the Events section of the Client Class in the DiscordJS docs. Try this out, and let me know if it works.
更多推荐
Discord Bot无法按名称或ID查找频道
发布评论