mirror of
https://github.com/Sneed-Group/clanrocket-2
synced 2024-12-23 11:42:48 -06:00
Update 'index.js'
This commit is contained in:
parent
4db27d0f92
commit
7ce9ad50ec
1 changed files with 148 additions and 148 deletions
296
index.js
296
index.js
|
@ -1,148 +1,148 @@
|
||||||
require('dotenv').config(); // Load environment variables from .env file
|
require('dotenv').config(); // Load environment variables from .env file
|
||||||
const { Client, Intents } = require('discord.js');
|
const { Client, Intents } = require('discord.js');
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
Intents.FLAGS.GUILDS,
|
Intents.FLAGS.GUILDS,
|
||||||
Intents.FLAGS.GUILD_MESSAGES // Add intents as needed
|
Intents.FLAGS.GUILD_MESSAGES // Add intents as needed
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
const { SlashCommandBuilder } = require('@discordjs/builders');
|
const { SlashCommandBuilder } = require('@discordjs/builders');
|
||||||
const { REST } = require('@discordjs/rest');
|
const { REST } = require('@discordjs/rest');
|
||||||
const { Routes } = require('discord-api-types/v9');
|
const { Routes } = require('discord-api-types/v9');
|
||||||
const { QuickDB } = require("quick.db");
|
const { QuickDB } = require("quick.db");
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
|
||||||
// Initialize Express.js app
|
// Initialize Express.js app
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
// Initialize the DB
|
// Initialize the DB
|
||||||
const db = new QuickDB();
|
const db = new QuickDB();
|
||||||
|
|
||||||
// Retrieve Application ID from environment variable
|
// Retrieve Application ID from environment variable
|
||||||
const applicationId = process.env.DISCORD_APPLICATION_ID;
|
const applicationId = process.env.DISCORD_APPLICATION_ID;
|
||||||
|
|
||||||
if (!applicationId) {
|
if (!applicationId) {
|
||||||
console.error('Application ID not found in environment variables.');
|
console.error('Application ID not found in environment variables.');
|
||||||
process.exit(1); // Exit the process if application ID is missing
|
process.exit(1); // Exit the process if application ID is missing
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up Discord slash commands
|
// Set up Discord slash commands
|
||||||
const commands = [
|
const commands = [
|
||||||
new SlashCommandBuilder()
|
new SlashCommandBuilder()
|
||||||
.setName('xp')
|
.setName('xp')
|
||||||
.setDescription('View your XP or someone else\'s XP')
|
.setDescription('View your XP or someone else\'s XP')
|
||||||
.addUserOption(option => option.setName('user').setDescription('The user to view XP for')),
|
.addUserOption(option => option.setName('user').setDescription('The user to view XP for')),
|
||||||
new SlashCommandBuilder()
|
new SlashCommandBuilder()
|
||||||
.setName('addxp')
|
.setName('addxp')
|
||||||
.setDescription('Add XP to yourself or someone else (admin only)')
|
.setDescription('Add XP to yourself or someone else (admin only)')
|
||||||
.addIntegerOption(option => option.setName('amount').setDescription('The amount of XP to add'))
|
.addIntegerOption(option => option.setName('amount').setDescription('The amount of XP to add'))
|
||||||
.addUserOption(option => option.setName('user').setDescription('The user to add XP to')),
|
.addUserOption(option => option.setName('user').setDescription('The user to add XP to')),
|
||||||
new SlashCommandBuilder()
|
new SlashCommandBuilder()
|
||||||
.setName('removexp')
|
.setName('removexp')
|
||||||
.setDescription('Remove XP from yourself or someone else (admin only)')
|
.setDescription('Remove XP from yourself or someone else (admin only)')
|
||||||
.addIntegerOption(option => option.setName('amount').setDescription('The amount of XP to remove'))
|
.addIntegerOption(option => option.setName('amount').setDescription('The amount of XP to remove'))
|
||||||
.addUserOption(option => option.setName('user').setDescription('The user to remove XP from')),
|
.addUserOption(option => option.setName('user').setDescription('The user to remove XP from')),
|
||||||
].map(command => command.toJSON());
|
].map(command => command.toJSON());
|
||||||
|
|
||||||
const rest = new REST({ version: '9' }).setToken(process.env.DISCORD_BOT_TOKEN);
|
const rest = new REST({ version: '9' }).setToken(process.env.DISCORD_BOT_TOKEN);
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
console.log('Started refreshing application (/) commands.');
|
console.log('Started refreshing application (/) commands.');
|
||||||
|
|
||||||
if (applicationId) {
|
if (applicationId) {
|
||||||
await rest.put(
|
await rest.put(
|
||||||
Routes.applicationCommands(applicationId),
|
Routes.applicationCommands(applicationId),
|
||||||
{ body: commands },
|
{ body: commands },
|
||||||
);
|
);
|
||||||
console.log('Successfully reloaded application (/) commands.');
|
console.log('Successfully reloaded application (/) commands.');
|
||||||
} else {
|
} else {
|
||||||
console.error('Application ID not found.');
|
console.error('Application ID not found.');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Discord.js event listener for when the bot is ready
|
// Discord.js event listener for when the bot is ready
|
||||||
client.once('ready', () => {
|
client.once('ready', () => {
|
||||||
console.log('Bot is ready!');
|
console.log('Bot is ready!');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Discord.js event listener for when a message is received
|
// Discord.js event listener for when a message is received
|
||||||
client.on('messageCreate', async message => {
|
client.on('messageCreate', async message => {
|
||||||
if (message.author.bot) return;
|
if (message.author.bot) return;
|
||||||
|
|
||||||
// Increment XP every 10 messages
|
// Increment XP every 10 messages
|
||||||
if (message.guild) {
|
if (message.guild) {
|
||||||
const guildId = message.guild.id;
|
const guildId = message.guild.id;
|
||||||
const xp = db.get(`xp.${guildId}.${message.author.id}`) || 0;
|
const xp = db.get(`xp.${guildId}.${message.author.id}`) || 0;
|
||||||
db.set(`xp.${guildId}.${message.author.id}`, xp + 1);
|
db.set(`xp.${guildId}.${message.author.id}`, xp + 1);
|
||||||
|
|
||||||
// Check if user leveled up
|
// Check if user leveled up
|
||||||
const level = Math.floor(xp / 5) + 1;
|
const level = Math.floor(xp / 5) + 1;
|
||||||
const newLevel = Math.floor((xp + 1) / 5) + 1;
|
const newLevel = Math.floor((xp + 1) / 5) + 1;
|
||||||
if (newLevel > level) {
|
if (newLevel > level) {
|
||||||
message.channel.send(`${message.author.username} leveled up to level ${newLevel}!`);
|
message.channel.send(`${message.author.username} leveled up to level ${newLevel}!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Discord.js event listener for when a slash command is used
|
// Discord.js event listener for when a slash command is used
|
||||||
client.on('interactionCreate', async interaction => {
|
client.on('interactionCreate', async interaction => {
|
||||||
if (!interaction.isCommand()) return;
|
if (!interaction.isCommand()) return;
|
||||||
|
|
||||||
const { commandName, options, guildId } = interaction;
|
const { commandName, options, guildId } = interaction;
|
||||||
|
|
||||||
// Retrieve guild ID from interaction if available
|
// Retrieve guild ID from interaction if available
|
||||||
const interactionGuildId = guildId || interaction.guildId;
|
const interactionGuildId = guildId || interaction.guildId;
|
||||||
|
|
||||||
if (!interactionGuildId) {
|
if (!interactionGuildId) {
|
||||||
console.error('Unable to retrieve guild ID.');
|
console.error('Unable to retrieve guild ID.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (commandName === 'xp') {
|
if (commandName === 'xp') {
|
||||||
const guildId = interactionGuildId;
|
const guildId = interactionGuildId;
|
||||||
const user = options.getUser('user') || interaction.user;
|
const user = options.getUser('user') || interaction.user;
|
||||||
const xp = db.get(`xp.${guildId}.${user.id}`) || 0;
|
const xp = await db.get(`xp.${guildId}.${user.id}`) || 0;
|
||||||
interaction.reply(`${user.username} has ${xp} XP.`);
|
interaction.reply(`${user.username} has ${xp} XP.`);
|
||||||
} else if (commandName === 'addxp') {
|
} else if (commandName === 'addxp') {
|
||||||
if (!interaction.member.permissions.has('ADMINISTRATOR')) {
|
if (!interaction.member.permissions.has('ADMINISTRATOR')) {
|
||||||
return interaction.reply('You do not have permission to use this command.');
|
return interaction.reply('You do not have permission to use this command.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const guildId = interactionGuildId;
|
const guildId = interactionGuildId;
|
||||||
const amount = options.getInteger('amount');
|
const amount = options.getInteger('amount');
|
||||||
const user = options.getUser('user') || interaction.user;
|
const user = options.getUser('user') || interaction.user;
|
||||||
const currentXp = db.get(`xp.${guildId}.${user.id}`) || 0;
|
const currentXp = await db.get(`xp.${guildId}.${user.id}`) || 0;
|
||||||
db.set(`xp.${guildId}.${user.id}`, currentXp + amount);
|
const newXp = await db.set(`xp.${guildId}.${user.id}`, currentXp + amount);
|
||||||
interaction.reply(`${amount} XP added to ${user.username}.`);
|
interaction.reply(`${amount} XP added to ${user.username}.`);
|
||||||
} else if (commandName === 'removexp') {
|
} else if (commandName === 'removexp') {
|
||||||
if (!interaction.member.permissions.has('ADMINISTRATOR')) {
|
if (!interaction.member.permissions.has('ADMINISTRATOR')) {
|
||||||
return interaction.reply('You do not have permission to use this command.');
|
return interaction.reply('You do not have permission to use this command.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const guildId = interactionGuildId;
|
const guildId = interactionGuildId;
|
||||||
const amount = options.getInteger('amount');
|
const amount = options.getInteger('amount');
|
||||||
const user = options.getUser('user') || interaction.user;
|
const user = options.getUser('user') || interaction.user;
|
||||||
const currentXp = db.get(`xp.${guildId}.${user.id}`) || 0;
|
const currentXp = db.get(`xp.${guildId}.${user.id}`) || 0;
|
||||||
db.set(`xp.${guildId}.${user.id}`, Math.max(0, currentXp - amount));
|
const newXp = await db.set(`xp.${guildId}.${user.id}`, Math.max(0, currentXp - amount));
|
||||||
interaction.reply(`${amount} XP removed from ${user.username}.`);
|
interaction.reply(`${amount} XP removed from ${user.username}.`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the Express.js server for the XP API
|
// Start the Express.js server for the XP API
|
||||||
app.get('/xp/:guildId/:userId', (req, res) => {
|
app.get('/xp/:guildId/:userId', (req, res) => {
|
||||||
const { guildId, userId } = req.params;
|
const { guildId, userId } = req.params;
|
||||||
const xp = db.get(`xp.${guildId}.${userId}`) || 0;
|
const xp = db.get(`xp.${guildId}.${userId}`) || 0;
|
||||||
res.json({ guildId, userId, xp });
|
res.json({ guildId, userId, xp });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`XP API server is running on http://localhost:${PORT}`);
|
console.log(`XP API server is running on http://localhost:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Login to Discord
|
// Login to Discord
|
||||||
client.login(process.env.DISCORD_BOT_TOKEN);
|
client.login(process.env.DISCORD_BOT_TOKEN);
|
||||||
|
|
Loading…
Reference in a new issue