GeneralDiscord Bot using javascript
Posted:

GeneralDiscord Bot using javascriptPosted:

hoardinqs
  • Junior Member
Status: Offline
Joined: Jun 09, 20167Year Member
Posts: 56
Reputation Power: 2
Status: Offline
Joined: Jun 09, 20167Year Member
Posts: 56
Reputation Power: 2
I tried to configure my discord bot through java script and I was able to accomplish the discord bot going online but does not interact with the discord I am in. I own the discord and I set the discord bot to admin but it is unable to recognize any commands I programmed. i type /play "youtube url link" and nothing. if someone can help much appreciated

code

const { Client } = require("discord.js");
const ytdl = require('ytdl-core');
const { createWriteStream } = require('fs');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, NoSubscriberBehavior, getVoiceConnection } = require('@discordjs/voice');

const client = new Client({ intents: 1 });
const prefix = '/';

client.once('ready', () => {
console.log('Bot is online! should be able to play music');
});

client.on('messageCreate', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;

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

if (command === 'play') {
if (!args[0]) {
return message.channel.send('Please provide a YouTube URL.');
}

const voiceChannel = message.member.voice.channel;

if (!voiceChannel) {
return message.channel.send('You need to be in a voice channel to play music.');
}

try {
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

const stream = ytdl(args[0], { filter: 'audioonly' });
const resource = createAudioResource(stream, { inlineVolume: true });
const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } });

player.play(resource);

connection.subscribe(player);

message.channel.send(`Now playing: ${args[0]}`);

player.on('error', (err) => {
console.error(err);
voiceChannel.leave();
message.channel.send('Error during playback.');
});

player.on('stateChange', (oldState, newState) => {
if (newState.status === 'stopped') {
voiceChannel.leave();
}
});
} catch (e) {
console.error(e);
message.channel.send('Error connecting to the voice channel.');
}
}
else if (command === 'restart')
{
// Check if the message author has the necessary permissions to restart the bot
if (!message.member.hasPermission('ADMINISTRATOR')) {
return message.channel.send('You do not have permission to restart the bot.');
}

message.channel.send('Restarting the bot...');

// Destroy the client instance
client.destroy();

// Log in again with the bot token
client.login('my token');
}
});

client.login('my token');
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS