Discord 机器人/为什么我不能使用命令?

问题描述 投票:0回答:1
const { Client, GatewayIntentBits } = require('discord.js');

const ytdl = require('ytdl-core');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });

const prefix = '*oynat';

client.once('ready', () => {

    console.log('Bot aktif!');
});

client.on('messageCreate', async (message) => {

    console.log('Message received:', message.content);

    if (message.content.startsWith(prefix) || message.author.bot) {

        console.log('Message does not start with prefix or is sent by a bot.');
        return;
    }

    console.log('Message starts with prefix.');

    const args = message.content.slice(prefix.length).trim().split(' ');

    const command = args.shift().toLowerCase();

    console.log('Command:', command);

    if (command === 'youtube') {

        console.log('YouTube command detected.');

        if (args.length === 0) {

            return message.reply('Youtube linki gir');
        }

        const voiceChannel = message.member.voice.channel;

        if (!voiceChannel) {

            return message.reply('Bir ses kanalına katıl');
        }

        const connection = await voiceChannel.join();

        const stream = ytdl(args[0], { filter: 'audioonly' });

        const dispatcher = connection.play(stream);


        dispatcher.on('finish', () => {

            voiceChannel.leave();
        });
    }
});

client.login('*******');
log:
Bot aktif!
Message received: 
Message starts with prefix.
Command:
javascript node.js discord
1个回答
0
投票

要访问消息内容,您需要在 Discord 开发者门户中激活此意图并添加以下内容: (https://discord.com/developers/applications/[botid]/bot) “GatewayIntentBits.MessageContent”

const { Client, GatewayIntentBits } = require('discord.js');

const ytdl = require('ytdl-core');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

const prefix = '*oynat';

client.once('ready', () => {

    console.log('Bot aktif!');
});

client.on('messageCreate', async (message) => {

    console.log('Message received:', message.content);

    if (message.content.startsWith(prefix) || message.author.bot) {

        console.log('Message does not start with prefix or is sent by a bot.');
        return;
    }

    console.log('Message starts with prefix.');

    const args = message.content.slice(prefix.length).trim().split(' ');

    const command = args.shift().toLowerCase();

    console.log('Command:', command);

    if (command === 'youtube') {

        console.log('YouTube command detected.');

        if (args.length === 0) {

            return message.reply('Youtube linki gir');
        }

        const voiceChannel = message.member.voice.channel;

        if (!voiceChannel) {

            return message.reply('Bir ses kanalına katıl');
        }

        const connection = await voiceChannel.join();

        const stream = ytdl(args[0], { filter: 'audioonly' });

        const dispatcher = connection.play(stream);


        dispatcher.on('finish', () => {

            voiceChannel.leave();
        });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.