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:
要访问消息内容,您需要在 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();
});
}
});