Discord Bot 无法按名称获取频道

Discord Bot Can#39;t Get Channel by Name(Discord Bot 无法按名称获取频道)
本文介绍了Discord Bot 无法按名称获取频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在制作一个不和谐的机器人,并想让它向特定的欢迎"频道发送消息.不幸的是,我一直无法这样做.我试过了.

I have been making a discord bot and wanted to make it send a message to a specific "Welcome" channel. Unfortunately, I have been unable to do so. I tried this.

const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome
"+member.user.username);

但是在这个welcomeChannel 未定义"中.

However in this "welcomeChannel is undefined".

我尝试过使用

const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome
"+member.user.username);

但这仍然是未定义的,奇怪的是

but this is still undefined, strangely

推荐答案

您应该使用频道 id 而不是频道名称.

You should use the channnel id instead of it's name.

如何获取频道的频道ID:

How to get the channel id of a channel:

  1. 打开您的 Discord 设置

  1. Open up your Discord Settings

转到外观

勾选开发者模式(并关闭Discord设置)

Tick Developer Mode (And close the Discord settings)

右键点击你想要的频道

现在有一个选项Copy ID来复制频道ID

Now there's an option Copy ID to copy the channel id

还可以查看 discord.js 文档 用于(频道)收藏

Also checkout the discord.js documentation for (channel) collections

此外,您的方法将不起作用,因为 .get 需要频道 ID(请参阅上面的链接文档).如果您真的想通过名称获取频道,请改用 .find.
但是,如果您的机器人在多个服务器上运行,这是一个非常糟糕的主意,因为频道名称现在可以出现多次.

Furthermore your approach won't work because .get wants a channel id (see the linked documentation above). In case you REALLY want to get an channel by its name, use .find instead for that.
This is however a really bad idea in case your bot runs on more than one server since channel names can now occur multiple times.

这篇关于Discord Bot 无法按名称获取频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)