Paste your logs.

Built for Minecraft & Hytale

Unknown Log

248 lines
Raw
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token, sftp, pterodactyl } = require('./config.json');
const fetch = require('node-fetch');
const SFTPClient = require('ssh2-sftp-client');
const axios = require('axios');
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]
});
client.on(Events.MessageCreate, async message => {
if (!message.content.startsWith('!whitelist') || message.author.bot) return;
try {
const args = message.content.slice('!whitelist'.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'help') {
const helpMessage = `**Whitelist Bot Commands:**\n\`\`\`
1. !whitelist <username>
→ Add a player to the whitelist
2. !whitelist remove <username>
→ Remove a player from the whitelist
3. !whitelist list
→ Show all whitelisted players
4. !whitelist check <username/uuid>
→ Look up a player's UUID or username
5. !whitelist help
→ Show this help message\`\`\``;
await message.channel.send(helpMessage);
return;
}
if (!message.member.roles.cache.has('REDACTED')) {
return message.channel.send('You do not have permission to use this command!');
}
if (command === 'check') {
const query = args[0];
if (!query) {
return message.channel.send('Please provide a username or UUID!');
}
const reply = await message.channel.send('Processing lookup request...');
try {
const isUUID = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i.test(query);
if (isUUID) {
const formattedUUID = query.replace(/-/g, '');
const response = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${formattedUUID}`);
if (!response.ok) {
return await reply.edit('Invalid UUID or player not found.');
}
const data = await response.json();
await reply.edit(`**UUID Lookup Result:**\n\`\`\`\nUsername: ${data.name}\nUUID: ${query}\`\`\``);
} else {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${query}`);
if (!response.ok) {
return await reply.edit('Invalid username or player not found.');
}
const data = await response.json();
const formattedUUID = data.id.replace(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/, '$1-$2-$3-$4-$5');
await reply.edit(`**Username Lookup Result:**\n\`\`\`\nUsername: ${data.name}\nUUID: ${formattedUUID}\`\`\``);
}
} catch (error) {
console.error('Lookup Error:', error);
await reply.edit('An error occurred while looking up the player.');
}
return;
}
if (command === 'list') {
const sftpClient = new SFTPClient();
try {
await sftpClient.connect({
host: sftp.host,
port: sftp.port,
username: sftp.username,
password: sftp.password
});
const whitelistData = await sftpClient.get(sftp.whitelistPath);
const whitelist = JSON.parse(whitelistData.toString());
const playerList = whitelist.map((player, index) => `${index + 1}. ${player.name}`).join('\n');
const formattedMessage = `**Whitelisted Players (${whitelist.length}):**\n\`\`\`\n${playerList}\n\`\`\``;
await message.channel.send(formattedMessage);
} catch (error) {
console.error('SFTP Error:', error);
await message.channel.send('Failed to retrieve whitelist.');
} finally {
await sftpClient.end();
}
} else if (command === 'remove') {
const ign = args[0];
if (!ign) {
return message.channel.send('Please provide a Minecraft username!');
}
const reply = await message.channel.send('Processing whitelist removal request...');
const sftpClient = new SFTPClient();
try {
await sftpClient.connect({
host: sftp.host,
port: sftp.port,
username: sftp.username,
password: sftp.password
});
const whitelistData = await sftpClient.get(sftp.whitelistPath);
let whitelist = JSON.parse(whitelistData.toString());
const initialLength = whitelist.length;
whitelist = whitelist.filter(player => player.name.toLowerCase() !== ign.toLowerCase());
if (whitelist.length === initialLength) {
await sftpClient.end();
return await reply.edit(`${ign} is not whitelisted!`);
}
await sftpClient.put(Buffer.from(JSON.stringify(whitelist, null, 2)), sftp.whitelistPath);
} catch (error) {
console.error('SFTP Error:', error);
return await reply.edit('Failed to update whitelist file');
} finally {
await sftpClient.end();
}
try {
await axios.post(
`${pterodactyl.apiUrl}/api/client/servers/${pterodactyl.serverId}/command`,
{ command: 'whitelist reload' },
{
headers: { 'Authorization': `Bearer ${pterodactyl.apiKey}` },
timeout: 5000
}
);
} catch (error) {
console.error('Pterodactyl API Error:', error);
return await reply.edit('Whitelist updated but failed to reload server whitelist');
}
await reply.edit(`Successfully removed ${ign} from the whitelist!`);
} else {
const ign = command;
if (!ign) {
return message.channel.send('Please provide a Minecraft username!');
}
const reply = await message.channel.send('Processing whitelist request...');
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`);
if (!response.ok) {
return await reply.edit('Failed to fetch player data from Mojang API');
}
const data = await response.json();
if (!data?.id) {
return await reply.edit('Invalid username!');
}
const sftpClient = new SFTPClient();
try {
await sftpClient.connect({
host: sftp.host,
port: sftp.port,
username: sftp.username,
password: sftp.password
});
let whitelist = [];
try {
const whitelistData = await sftpClient.get(sftp.whitelistPath);
whitelist = JSON.parse(whitelistData.toString());
} catch (error) {
console.log('Creating new whitelist file');
whitelist = [];
}
const isWhitelisted = whitelist.some(player =>
player.uuid === data.id ||
player.name.toLowerCase() === data.name.toLowerCase()
);
if (isWhitelisted) {
await sftpClient.end();
return await reply.edit(`${data.name} is already whitelisted!`);
}
whitelist.push({
uuid: data.id,
name: data.name
});
await sftpClient.put(Buffer.from(JSON.stringify(whitelist, null, 2)), sftp.whitelistPath);
} catch (error) {
console.error('SFTP Error:', error);
return await reply.edit('Failed to update whitelist file');
} finally {
await sftpClient.end();
}
try {
await axios.post(
`${pterodactyl.apiUrl}/api/client/servers/${pterodactyl.serverId}/command`,
{ command: 'whitelist reload' },
{
headers: { 'Authorization': `Bearer ${pterodactyl.apiKey}` },
timeout: 5000
}
);
} catch (error) {
console.error('Pterodactyl API Error:', error);
return await reply.edit('Whitelist updated but failed to reload server whitelist');
}
await reply.edit(`Successfully whitelisted ${data.name}!`);
}
} catch (error) {
console.error('Command Error:', error);
try {
await message.channel.send('An error occurred while processing the whitelist command.');
} catch (e) {
console.error('Failed to send error message:', e);
}
}
});
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
client.user.setActivity('!whitelist help', { type: 0 });
});
client.on('error', error => {
console.error('Discord client error:', error);
});
client.login(token);