Discord Bot 无法按名称获取频道

编程入门 行业动态 更新时间:2024-10-25 15:32:49
本文介绍了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:

  • 打开您的 Discord 设置

  • 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 无法按名称获取频道

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

    发布评论

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

    >www.elefans.com

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