【问题标题】:How do I set a variable to a specific voice channel discord.js?如何将变量设置为特定的语音通道 discord.js?
【发布时间】:2021-07-15 07:00:49
【问题描述】:

我正在尝试将 variable 设置为特定的 voice channel,以便始终让机器人在 same 频道而不是用户所在的任何频道中工作。我'我是 js 新手并编写了不和谐的机器人。我的大部分代码来自here

到目前为止,我已经尝试了所有我能想到的

  const voiceChannel = channel_ID;

  const voiceChannel = channel.id(channel_ID);

(其中channel_ID是discord分配的实际ID号)

我查看了 discord.js.org,但找不到解决方案。

我可能正在做一些非常明显的错误,但我无法弄清楚它是什么。有什么想法吗?

【问题讨论】:

  • 那么你想要一个只包含IDvariable 还是实际的GuildChannel object
  • 我猜是使用 GuildChannel 对象?我试图让它达到一个基本上可以使用 voiceChannel.join() 并让它加入指定语音通道的地步。

标签: javascript discord.js


【解决方案1】:

因此,为了在特定的 channeljoin 您的机器人,您首先必须实际获得 channel

如何获得(你的IDchannel

嗯,你有两种选择:

  • 在您的Discord 中激活developer mode > 右键单击​​target channel 并选择Copy ID > 将其粘贴到您的代码中
const channelId = '1234567890';
const targetChannel = client.channels.cache.get(channelId);
  • 按名称获取channel
const targetChannel = guild.channels.cache.find(channel => channel.name === "Name of the channel");

如果这样做了,你现在应该有你的channel,但这还不是全部!

现在你想检查一下,如果你的机器人真的得到了channel,如果你想能够使用.join(),你必须确保它是VoiceChannel

// Check if the channel is undefined or not
if(!targetChannel) return message.channel.send('Channel not found!');

// Check if the channel is a voice channel
if(targetChannel.type !== 'voice') return message.channel.send('This is not a voice channel!');

// If it passed the code above you can now let your bot join the channel
targetChannel.join().then(connection => {

    console.log("Successfully connected.");
  }).catch(e => {
    
    console.error(e);

【讨论】:

  • 复制粘贴答案时至少可以参考原作者
  • @JoeMoore 我没有复制粘贴那个答案哈哈
  • 我在这个标签上看到过代码,如果你愿意,我可以找到原帖吗? - 编辑:stackoverflow.com/questions/49844393/…
  • @JoeMoore 啊,你的意思是最后几行,对,我刚才在我的机器人中复制了它,现在在这里使用它。不记得是来自这个问题
  • 别担心,只是觉得我应该指出来
【解决方案2】:

您可以将变量存储为任何内容,因为 ID 是一组整数,它可以在没有 quotes ("")template literals (``) 的情况下存储强>等
虽然由于使用 parseInt() 等方法可能会发生冲突,但我们会将其存储为字符串。因此,假设我们现在有一个 ID,我们将其存储为:

const Channel = ('ChannelID');
var Channel = ('ChannelID');
let Channel = ('ChannelID'); // of course only one of these 

ChannelID 是实际 ID,现在我们可以稍后访问它以获取/获取频道,如下所示:

<client>.channels.cache.get(Channel) || <client>.channels.fetch(Channel);

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2020-02-19
    • 2020-11-21
    • 2021-07-04
    • 2017-09-11
    • 2018-02-26
    相关资源
    最近更新 更多