Discord Bot无法按名称或ID查找频道

编程入门 行业动态 更新时间:2024-10-25 23:33:48
本文介绍了Discord Bot无法按名称或ID查找频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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查找频道

本文发布于:2023-10-28 23:44:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   频道   Discord   Bot   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!