1

我试图在我的代码中使用 .setSelfDeaf() 但我无法弄清楚。我尝试的每一种方法最终都会在执行时使机器人崩溃。

有人可以帮我使用它吗?我想要的是机器人每次加入语音频道时都聋了。

编辑:根据要求添加了我收到的错误和更新的代码。

我得到的错误:

2021-03-04T06:25:06.238627+00:00 app[worker.1]: /app/code.js:53
2021-03-04T06:25:06.238628+00:00 app[worker.1]:     connection.voice.setSelfDeaf(true);
2021-03-04T06:25:06.238629+00:00 app[worker.1]:     ^
2021-03-04T06:25:06.238629+00:00 app[worker.1]: 
2021-03-04T06:25:06.238629+00:00 app[worker.1]: ReferenceError: connection is not defined
2021-03-04T06:25:06.238630+00:00 app[worker.1]:     at Client.<anonymous> (/app/code.js:53:5)
2021-03-04T06:25:06.238630+00:00 app[worker.1]:     at Client.emit (events.js:327:22)
2021-03-04T06:25:06.238631+00:00 app[worker.1]:     at VoiceStateUpdate.handle (/app/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:40:14)
2021-03-04T06:25:06.238632+00:00 app[worker.1]:     at Object.module.exports [as VOICE_STATE_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
2021-03-04T06:25:06.238632+00:00 app[worker.1]:     at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
2021-03-04T06:25:06.238633+00:00 app[worker.1]:     at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
2021-03-04T06:25:06.238633+00:00 app[worker.1]:     at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
2021-03-04T06:25:06.238634+00:00 app[worker.1]:     at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:132:16)
2021-03-04T06:25:06.238634+00:00 app[worker.1]:     at WebSocket.emit (events.js:315:20)
2021-03-04T06:25:06.238634+00:00 app[worker.1]:     at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:825:20)
2021-03-04T06:25:06.279643+00:00 heroku[worker.1]: Process exited with status 1
2021-03-04T06:25:06.340294+00:00 heroku[worker.1]: State changed from up to crashed

更新后的代码:

const Discord = require('discord.js'),
    DisTube = require('distube'),
    client = new Discord.Client(),
    config = {
        prefix: "em!",
        token: process.env.TOKEN || "[insert discord bot token]"
    };


const distube = new DisTube(client, { searchSongs: true, emitNewSongOnly: true });

client.on('ready', () => {
    console.log(`e-Music is up and running`);
    client.user.setActivity(`em!play`)
});

client.on("message", async (message) => {
    if (message.author.bot) return;
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();

    if (command == "play")
        distube.play(message, args.join(" "));

    if (["repeat", "loop"].includes(command))
        distube.setRepeatMode(message, parseInt(args[0]));

    if (command == "stop") {
        distube.stop(message);
        message.channel.send("Stopped the music!");
    }

    if (command == "skip")
        distube.skip(message);

    if (command == "queue") {
        let queue = distube.getQueue(message);
        message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
            `**${id + 1}**. ${song.name} - \`${song.formattedDuration}\``
        ).slice(0, 10).join("\n"));
    }

    if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
        let filter = distube.setFilter(message, command);
        message.channel.send("Current queue filter: " + (filter || "Off"));
    }
});

client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
    if (newVoiceState.id == client.user.id) {
        connection.voice.setSelfDeaf(true);
    };
});

const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;

distube
    .on("playSong", (message, queue, song) => message.channel.send(
        `Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
    ))
    .on("addSong", (message, queue, song) => message.channel.send(
        `Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
    ))
    .on("playList", (message, queue, playlist, song) => message.channel.send(
        `Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`
    ))
    .on("addList", (message, queue, playlist) => message.channel.send(
        `Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
    ))

    .on("searchResult", (message, result) => {
        let i = 0;
        message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
    })

    .on("searchCancel", (message) => message.channel.send(`Searching canceled`))
    .on("error", (message, e) => {
        console.error(e)
        message.channel.send("An error encountered: " + e);
    });

client.login(config.token);
4

1 回答 1

1

如果您阅读错误ReferenceError: connection is not defined,它会告诉您哪里出了问题。connection在以下代码中不是变量:

client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
    if (newVoiceState.id == client.user.id) {
        connection.voice.setSelfDeaf(true);
    };
});

虽然有一个connection属性,newVoiceState但在这种情况下这不是您想要的。您可能希望使用以下.setSelfDeaf方法VoiceState

client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
    if (newVoiceState.id == client.user.id) {
       newVoiceState.setSelfDeaf(true);
    };
});
于 2021-03-04T06:51:20.030 回答