简体   繁体   English

使用 discord.js 的音乐机器人问题

[英]Issues with music bot using discord.js

I am running into some errors while creating a basic discord bot (for playing music).我在创建基本的 discord 机器人(用于播放音乐)时遇到了一些错误。 I've been relying on the documentation mostly - I think I copied and pasted certain lines of code from the discord.js documentation examples that were "out of context".我一直主要依赖文档 - 我想我从 discord.js 文档示例中复制并粘贴了“脱离上下文”的某些代码行。 But I don't know what they could be since I'm still very new to programming in general.但我不知道它们可能是什么,因为我对编程还是很陌生。

I installed and imported all of the necessary dependencies so that's not the issue.我安装并导入了所有必要的依赖项,所以这不是问题。 My bot is working fine (listening and replying to msgs).我的机器人工作正常(收听和回复消息)。

here's the code related to the music function这是与音乐相关的代码 function

  client.on("message", async message => {
  const prefix = '?'
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  if (command === 'play') {
    let track = await client.player.play(message.member.voice.channel, args[0], 
    message.member.user.tag);
    
    message.channel.send(`Currently playing ${track.name} as requested by 
    ${track.requestedBy}`);

  }

  if (command === 'stop') {
    let track = await client.player.stop(message.guild.id);
    message.channel.send('Stopped.')
  }


})

This is the error I got:这是我得到的错误:

    (node:436) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'play' of undefined
    (node:436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated 
    either by throwing inside of an async function without a catch block, or by rejecting a promise 
    which was not handled with .catch(). To terminate the node process on unhandled promise 
    rejection, use the CLI flag `--unhandled-rejections=strict` (see 
    https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the 
    future, promise rejections that are not handled will terminate the Node.js process with a non- 
    zero exit code.

Could it be:会不会是:

  1. The bot needs to be in a channel first?机器人需要先在频道中? (which means i need to write a join channel function?) (这意味着我需要编写一个加入频道 function?)
  2. An issue with the prefix?前缀有问题? The defined prefix in my config.json file is "prefix": ";;ntek ", i did try changing the prefix to that, removing it completely, and a bunch of other things but nothing seemed to make the bot work.在我的 config.json 文件中定义的前缀是"prefix": ";;ntek ",我确实尝试将前缀更改为该前缀,将其完全删除,以及一堆其他的东西,但似乎没有任何东西可以使机器人工作。
  3. a syntax error (or something is missing) in the async function?异步 function 中的语法错误(或缺少某些内容)?

Try it with the code that is explained and showed on this side:尝试使用在这一侧解释和显示的代码:
https://discordjs.guide/voice/ https://discordjs.guide/voice/
First, you need to create a voice channel connection and then you can play things...首先,你需要创建一个语音通道连接,然后你可以播放东西......
Everything should be explained on the website.一切都应该在网站上进行解释。
Hope I could help;)希望我能帮上忙;)
ShadowLp174 ShadowLp174

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM