mbot/main.js

93 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-07-09 02:54:01 +00:00
const { Client, Collection, Events, EmbedBuilder, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
2024-05-10 07:19:26 +00:00
const { token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
2024-07-04 09:14:01 +00:00
const { Player } = require('discord-player');
2024-05-10 07:19:26 +00:00
2024-05-10 10:39:09 +00:00
const informationStatus = "[INFO]: ";
2024-05-10 07:19:26 +00:00
const warningStatus = "[WARNING]: ";
const errorStatus = "[ERROR]: ";
2024-05-10 22:02:01 +00:00
const client = new Client({
2024-07-04 09:14:01 +00:00
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, 'GuildVoiceStates'],
2024-05-10 22:02:01 +00:00
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
2024-07-09 02:54:01 +00:00
presence: {
activities: [{
name: 'Use /help - MBot',
type: ActivityType.Playing
}],
status: 'idle'
}
2024-05-10 22:02:01 +00:00
});
2024-05-10 07:19:26 +00:00
2024-07-04 09:14:01 +00:00
const player = new Player(client);
player.extractors.loadDefault();
2024-05-10 07:19:26 +00:00
client.commands = new Collection();
2024-05-10 10:39:09 +00:00
const foldersPath = path.join(__dirname, 'commands');
2024-05-10 07:19:26 +00:00
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
2024-05-10 10:39:09 +00:00
console.log(informationStatus + `'${folder}' folder has been added! All js files will be called.`);
2024-05-10 07:19:26 +00:00
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
2024-05-10 10:39:09 +00:00
2024-05-10 07:19:26 +00:00
if('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command)
} else {
console.log(warningStatus + `This file at ${filePath} does not contain data or execute properties, will be ignored.`);
}
2024-05-10 10:39:09 +00:00
console.log(informationStatus + `'${file}' has been added into command pool.`);
2024-05-10 07:19:26 +00:00
}
}
client.once(Events.ClientReady, readyClient => {
2024-07-09 02:54:01 +00:00
try {
2024-05-10 16:22:38 +00:00
console.log(informationStatus + `Activity has been set to '${client.user.presence.activities}'`);
console.log(informationStatus + `Status has been set to ${client.user.presence.status}`);
} catch (error) {
console.log(errorStatus + "Something went wrong with setting the status! Debug information below.");
console.error(error);
}
2024-05-10 07:19:26 +00:00
console.log(informationStatus + "Client is online!");
});
client.on(Events.InteractionCreate, async interaction => {
if(!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if(!command) {
console.log(errorStatus + "No commands matching ${interaction.commandName} was not found! (Repair!)")
console.error(error);
}
try {
await command.execute(interaction);
} catch (error) {
2024-05-11 06:55:44 +00:00
console.log(errorStatus + "Something went wrong while executing this command! Please use the information below and sudmit to the author of the issue!");
2024-05-10 07:19:26 +00:00
console.error(error);
if(interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Error executing this command! Something went wrong, try again later.', ephemeral: true });
} else {
await interaction.reply({ content: 'Error executing this command! Something went wrong, try again later.', ephemeral: true });
}
}
});
2024-07-04 09:14:01 +00:00
player.events.on('playerStart', (queue, track) => {
queue.metadata.channel.send(`Now playing: **${track.title}** from **${track.author}**`);
console.log(`${informationStatus}${track.title} from ${track.author} in ${queue.metadata.channel.guildId} is playing!`);
2024-05-11 05:39:11 +00:00
});
2024-05-10 22:02:01 +00:00
2024-05-10 07:19:26 +00:00
client.login(token);