mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-05-10 10:43:06 +00:00
Added Launch.json and Tasks.json sources for run and build
This commit is contained in:
parent
10e32adfe6
commit
3a6a2763f8
24 changed files with 980 additions and 567 deletions
|
@ -1,9 +0,0 @@
|
|||
import { SlashCommandBuilder, CommandInteraction } from 'discord.js';
|
||||
|
||||
export const data = new SlashCommandBuilder()
|
||||
.setName('ping')
|
||||
.setDescription('Replies with Pong!');
|
||||
|
||||
export async function execute(interaction: CommandInteraction) {
|
||||
return interaction.reply('Pong!');
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"token": "BOT-TOKEN",
|
||||
"clientId": "BOT-ID",
|
||||
"guildId": "SERVER-ID"
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
import * as fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { REST } from '@discordjs/rest';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.resolve();
|
||||
|
||||
// Define the path to the directory containing your command files
|
||||
const foldersPath = path.join(__dirname, '\\target\\commands'); // Change 'commands' to your actual directory name
|
||||
|
||||
export async function registerCommands(clientId: any, guildId: any, token: any) {
|
||||
const commands = [];
|
||||
|
||||
try {
|
||||
const commandFolders = await fs.readdir(foldersPath);
|
||||
|
||||
for (const folder of commandFolders) {
|
||||
const commandsPath = path.join(foldersPath, folder);
|
||||
const commandFiles = (await fs.readdir(commandsPath)).filter(file => file.endsWith('.js'));
|
||||
|
||||
for (const file of commandFiles) {
|
||||
const filePath = path.join('file://', commandsPath, file);
|
||||
|
||||
// Import the module and handle any potential errors
|
||||
let commandModule;
|
||||
try {
|
||||
commandModule = await import(filePath);
|
||||
} catch (error) {
|
||||
console.error(`Error importing module from ${filePath}: ${error}`);
|
||||
continue; // Continue to the next module on error
|
||||
}
|
||||
|
||||
// Check if the imported module has 'data' and 'execute' properties
|
||||
if ('data' in commandModule && 'execute' in commandModule) {
|
||||
commands.push(commandModule.data.toJSON());
|
||||
} else {
|
||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rest = new REST({ version: '10' }).setToken(token);
|
||||
|
||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
||||
|
||||
const data: any = await rest.put(
|
||||
`/applications/${clientId}/guilds/${guildId}/commands`,
|
||||
{ body: commands }
|
||||
);
|
||||
|
||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
67
src/index.ts
67
src/index.ts
|
@ -1,67 +0,0 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { Client, Collection, GatewayIntentBits, Interaction } from 'discord.js';
|
||||
import { registerCommands } from './deploy-command.js';
|
||||
import config from './config.json' assert { type: 'json' };
|
||||
|
||||
const { clientId, guildId, token } = config;
|
||||
|
||||
class CustomClient extends Client {
|
||||
commands: Collection<any, any> = new Collection();
|
||||
}
|
||||
|
||||
const client = new CustomClient({ intents: [GatewayIntentBits.Guilds] });
|
||||
|
||||
function initializeCommands() {
|
||||
const __dirname = path.resolve();
|
||||
const foldersPath = path.join(__dirname, '\\target\\commands');
|
||||
|
||||
console.log(`Folder Path Received: ${foldersPath}`);
|
||||
|
||||
try {
|
||||
const commandFiles = fs.readdirSync(foldersPath);
|
||||
for (const file of commandFiles) {
|
||||
if (file.endsWith('.js')) {
|
||||
const command = require(path.join(foldersPath, file));
|
||||
console.log(command);
|
||||
client.commands.set(command.name, command);
|
||||
}
|
||||
}
|
||||
console.log('Commands initialized successfully!');
|
||||
} catch (error) {
|
||||
console.error('Error initializing commands:', error);
|
||||
}
|
||||
}
|
||||
|
||||
initializeCommands();
|
||||
|
||||
try {
|
||||
await registerCommands(clientId, guildId, token);
|
||||
} catch (error) {
|
||||
console.error("Error registering slash commands:", error);
|
||||
}
|
||||
|
||||
client.once('ready', () => {
|
||||
console.log('Ready!');
|
||||
});
|
||||
|
||||
client.on('interactionCreate', async (interaction: Interaction) => {
|
||||
if (!interaction.isCommand()) return;
|
||||
|
||||
const command = client.commands.get(interaction.commandName);
|
||||
|
||||
if (!command) return;
|
||||
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (interaction.replied || interaction.deferred) {
|
||||
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||
} else {
|
||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
client.login(token);
|
Loading…
Add table
Add a link
Reference in a new issue