简体   繁体   中英

Issues with music bot using discord.js

I am running into some errors while creating a basic discord bot (for playing music). 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". 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

  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?)
  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.
  3. a syntax error (or something is missing) in the async function?

Try it with the code that is explained and showed on this side:
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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