Dcom 🎈
Dcom 🎈
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 12/13/2024 in #djs-questions
How to create something like this?
K
4 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
no, it just says "this interaction has failed"
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
i hope not, postgre gives me a headache
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
Myself and 2 other devs worked on it.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
total bot has 55,000 lines of code, spent 3 months, 12 hours+ a day
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
How am I doing so far otherwise?
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
Ah I see, that's good to know.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
aren't I supposed to?
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
Hmm, so if I follow the instructions in that link, it will allow simultaenous commands?
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
So that's why it's not working.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
If you don't release the client your application will leak them and eventually your pool will be empty forever and all future requests to check out a client from the pool will wait forever.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
Ohhh
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
const fs = require("fs");
const path = require("path");
const {
Client,
GatewayIntentBits,
Collection,
EmbedBuilder,
} = require("discord.js");
const dotenv = require("dotenv");
dotenv.config();
const { Pool } = require("pg");

// Logging setup
const logFile = fs.createWriteStream("./bot.log", { flags: "a" });
const logChannelId = "1299771372058443776"; // Logging channel ID

// Centralized error handler with logging
function handleError(error, context) {
console.error(`Error in ${context}:`, error);
logFile.write(
`[${new Date().toISOString()}] Error in ${context}: ${error}\n`,
);
}

// Initialize the PostgreSQL connection pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});

pool
.connect()
.then(() => {
console.log("Connected to PostgreSQL database!");
logFile.write(
`[${new Date().toISOString()}] Connected to PostgreSQL database!\n`,
);
})
.catch((err) => handleError(err, "PostgreSQL Connection"));

// Initialize the Discord.js client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
],
});
console.log(`Initialized Discord.js Client`);

// Log successful login
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
logFile.write(
`[${new Date().toISOString()}] Logged in as ${client.user.tag}!\n`,
);
});

// Load commands from the commands directory
client.commands = new Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}

// Handle interactions (Slash commands)
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const commandName = interaction.commandName;
const userId = interaction.user.id;

const command = client.commands.get(commandName);
if (!command) return;

try {
await command.execute(interaction, pool, logChannelId, userId); // Pass necessary parameters to the command
console.log(`Command executed: ${commandName}`);
} catch (error) {
handleError(error, `interactionCreate - ${commandName}`);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});

// Error handling
process.on("uncaughtExceptionMonitor", (error) => {
console.error("Uncaught exception monitor:", error);
});

process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});

process.on("uncaughtException", (error) => {
console.error("Uncaught exception:", error);
});

// Graceful shutdown process
process.on("SIGINT", async () => {
console.log("Bot is shutting down...");
await pool.end(); // Close the database connection
client.destroy(); // Disconnect the bot from Discord
process.exit(0);
});

// Login the bot
client.login(process.env.BOT_TOKEN);
const fs = require("fs");
const path = require("path");
const {
Client,
GatewayIntentBits,
Collection,
EmbedBuilder,
} = require("discord.js");
const dotenv = require("dotenv");
dotenv.config();
const { Pool } = require("pg");

// Logging setup
const logFile = fs.createWriteStream("./bot.log", { flags: "a" });
const logChannelId = "1299771372058443776"; // Logging channel ID

// Centralized error handler with logging
function handleError(error, context) {
console.error(`Error in ${context}:`, error);
logFile.write(
`[${new Date().toISOString()}] Error in ${context}: ${error}\n`,
);
}

// Initialize the PostgreSQL connection pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});

pool
.connect()
.then(() => {
console.log("Connected to PostgreSQL database!");
logFile.write(
`[${new Date().toISOString()}] Connected to PostgreSQL database!\n`,
);
})
.catch((err) => handleError(err, "PostgreSQL Connection"));

// Initialize the Discord.js client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
],
});
console.log(`Initialized Discord.js Client`);

// Log successful login
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
logFile.write(
`[${new Date().toISOString()}] Logged in as ${client.user.tag}!\n`,
);
});

// Load commands from the commands directory
client.commands = new Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}

// Handle interactions (Slash commands)
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const commandName = interaction.commandName;
const userId = interaction.user.id;

const command = client.commands.get(commandName);
if (!command) return;

try {
await command.execute(interaction, pool, logChannelId, userId); // Pass necessary parameters to the command
console.log(`Command executed: ${commandName}`);
} catch (error) {
handleError(error, `interactionCreate - ${commandName}`);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});

// Error handling
process.on("uncaughtExceptionMonitor", (error) => {
console.error("Uncaught exception monitor:", error);
});

process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});

process.on("uncaughtException", (error) => {
console.error("Uncaught exception:", error);
});

// Graceful shutdown process
process.on("SIGINT", async () => {
console.log("Bot is shutting down...");
await pool.end(); // Close the database connection
client.destroy(); // Disconnect the bot from Discord
process.exit(0);
});

// Login the bot
client.login(process.env.BOT_TOKEN);
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
here's the full bot.js file
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
const { Pool } = require("pg");
require("dotenv").config();

// Initialize the PostgreSQL connection pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});

pool
.connect()
.then(() => {
console.log("Connected to PostgreSQL database!");
})
.catch((err) => handleError(err, "PostgreSQL Connection"));
const { Pool } = require("pg");
require("dotenv").config();

// Initialize the PostgreSQL connection pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});

pool
.connect()
.then(() => {
console.log("Connected to PostgreSQL database!");
})
.catch((err) => handleError(err, "PostgreSQL Connection"));
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const commandName = interaction.commandName;
const userId = interaction.user.id;

const command = client.commands.get(commandName);
if (!command) return;

try {
await command.execute(interaction, pool, logChannelId, userId); // Pass necessary parameters to the command
console.log(`Command executed: ${commandName}`);
} catch (error) {
handleError(error, `interactionCreate - ${commandName}`);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const commandName = interaction.commandName;
const userId = interaction.user.id;

const command = client.commands.get(commandName);
if (!command) return;

try {
await command.execute(interaction, pool, logChannelId, userId); // Pass necessary parameters to the command
console.log(`Command executed: ${commandName}`);
} catch (error) {
handleError(error, `interactionCreate - ${commandName}`);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
I have to wait for the command to fully process before I can run it. Otherwise, it times out. No console errors though, so i'm confused.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
I can't run this while someone in the server has used another slash command.
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
const { Pool } = require('pg'); // PostgreSQL client for database operations

// Set up a PostgreSQL connection pool with SSL
const pool = new Pool({
connectionString: process.env.DATABASE_URL, // Ensure you're using your connection string from an env variable
ssl: {
rejectUnauthorized: false, // Required for some remote databases like Heroku
},
});

const organizerUserIDs = [
"1099081303963476032",
"144942127355265024",
"943917480789803069",
"395397368872304642",
"678727362229305344",
];

module.exports = {
name: 'levi',
description: 'Add or remove points for a player by setting the exact number of points.',
async execute(interaction) {
// Check if the user is authorized
if (!organizerUserIDs.includes(interaction.user.id)) {
return interaction.reply({ content: 'You are not authorized to use this command.', ephemeral: true });
}

// Get user input (discord_id and points)
const discordId = interaction.options.getString('discord_id'); // Discord ID of the player
const points = interaction.options.getInteger('points'); // Exact points value to set

if (!discordId || points === null) {
return interaction.reply({ content: 'Please provide both a valid Discord ID and the number of points to set.', ephemeral: true });
}

// SQL query to update the player's points to the exact number
const query = `
UPDATE players
SET points = $1
WHERE discord_id = $2;
`;

const values = [points, discordId];

try {
// Execute the database query
const result = await pool.query(query, values);

if (result.rowCount === 0) {
return interaction.reply({ content: `No player found with Discord ID ${discordId}.`, ephemeral: true });
}

// Send confirmation message
await interaction.reply({
content: `Successfully updated the points for Discord ID ${discordId} to ${points}.`,
ephemeral: true,
});
} catch (err) {
console.error('Database error:', err);

// Send error message
await interaction.reply({ content: 'There was an error updating the player points.', ephemeral: true });
}
},
};
const { Pool } = require('pg'); // PostgreSQL client for database operations

// Set up a PostgreSQL connection pool with SSL
const pool = new Pool({
connectionString: process.env.DATABASE_URL, // Ensure you're using your connection string from an env variable
ssl: {
rejectUnauthorized: false, // Required for some remote databases like Heroku
},
});

const organizerUserIDs = [
"1099081303963476032",
"144942127355265024",
"943917480789803069",
"395397368872304642",
"678727362229305344",
];

module.exports = {
name: 'levi',
description: 'Add or remove points for a player by setting the exact number of points.',
async execute(interaction) {
// Check if the user is authorized
if (!organizerUserIDs.includes(interaction.user.id)) {
return interaction.reply({ content: 'You are not authorized to use this command.', ephemeral: true });
}

// Get user input (discord_id and points)
const discordId = interaction.options.getString('discord_id'); // Discord ID of the player
const points = interaction.options.getInteger('points'); // Exact points value to set

if (!discordId || points === null) {
return interaction.reply({ content: 'Please provide both a valid Discord ID and the number of points to set.', ephemeral: true });
}

// SQL query to update the player's points to the exact number
const query = `
UPDATE players
SET points = $1
WHERE discord_id = $2;
`;

const values = [points, discordId];

try {
// Execute the database query
const result = await pool.query(query, values);

if (result.rowCount === 0) {
return interaction.reply({ content: `No player found with Discord ID ${discordId}.`, ephemeral: true });
}

// Send confirmation message
await interaction.reply({
content: `Successfully updated the points for Discord ID ${discordId} to ${points}.`,
ephemeral: true,
});
} catch (err) {
console.error('Database error:', err);

// Send error message
await interaction.reply({ content: 'There was an error updating the player points.', ephemeral: true });
}
},
};
58 replies
DIAdiscord.js - Imagine an app
Created by Dcom 🎈 on 11/25/2024 in #djs-questions
How to handle multiple slash commands simultaenously?
For example, this one
58 replies