Skip to content

Commit

Permalink
added indexes and better newacc alert reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mirdukkkkk committed Feb 19, 2024
1 parent eb1c5bd commit 6777c57
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/commands/BaninfoCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaninfoCommand extends PixelCommand {

const ban = (await message.client.database.collection('users').findOne(
{ userID: user.id },
{ projection: { _id: 0, banned: 1 } }
{ projection: { _id: 0, banned: 1 }, hint: { userID: 1 } }
))?.banned;
if(!ban)
return msg.edit({ content: 'Указанный вами игрок не находится в бане' });
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ClearCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ClearCommand extends PixelCommand {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await message.client.database.collection('users').findOne(
{ userID: message.author.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify({
Expand Down
6 changes: 3 additions & 3 deletions src/commands/GameCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class GameCommand extends PixelCommand {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await message.client.database.collection('users').findOne(
{ userID: message.author.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify({ ended })
}).then(res => res?.json()).catch(() => {});

if(request?.error ?? !request) return message.reply({ content: request ? codeBlock('json', JSON.stringify(request)) : 'От API поступил пустой ответ, возможно, стоит проверить его состояние' });
if(request?.error ?? !request) return msg.edit({ content: request ? codeBlock('json', JSON.stringify(request)) : 'От API поступил пустой ответ, возможно, стоит проверить его состояние' });

return msg.edit({ content: `Игра была успешно ${ended ? 'завершена' : 'запущена'}` });
} else {
Expand All @@ -69,7 +69,7 @@ class GameCommand extends PixelCommand {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await message.client.database.collection('users').findOne(
{ userID: message.author.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify({ cooldown })
Expand Down
4 changes: 2 additions & 2 deletions src/commands/ModCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ModeratorCommand extends PixelCommand {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await message.client.database.collection('users').findOne(
{ userID: message.author.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify({
Expand All @@ -56,7 +56,7 @@ class ModeratorCommand extends PixelCommand {
.updateOne(
{ userID: user },
{ $set: { role: Number(action) } },
{ upsert: true }
{ upsert: true, hint: { userID: 1 } }
);

message.guild.members.cache.get(user).roles[action ? 'add' : 'remove']('969950074874515476').catch(() => {});
Expand Down
4 changes: 2 additions & 2 deletions src/commands/PointsCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PointsCommand extends PixelCommand {
case 'leaders':
case 'leader': {
const msg = await message.reply({ content: 'Идёт построение таблицы лидеров...' });
let users = await message.client.database.collection('users').find({}, { userID: 1, points: 1 }).toArray();
let users = await message.client.database.collection('users').find({}, { projection: { userID: 1, points: 1 }, hint: { userID: 1 } }).toArray();
users = users.filter(u => u.points !== 0);

let i = 1;
Expand All @@ -58,7 +58,7 @@ class PointsCommand extends PixelCommand {

default: {
const target = message.mentions.members?.first() || message.guild.members.cache.get(args[0]) || message.member;
const data = await message.client.database.collection('users').findOne({ userID: target.id });
const data = await message.client.database.collection('users').findOne({ userID: target.id }, { hint: { userID: 1 } });

message.reply({
embeds: [
Expand Down
2 changes: 1 addition & 1 deletion src/commands/TagCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TagCommand extends PixelCommand {
return message.reply({ content: 'Укажите тег, участников которого хотите посмотреть' });

const data = await message.client.database.collection('users')
.find({ tag })
.find({ tag }, { hint: { userID: 1 } })
.toArray();
if(data.length <= 0)
return message.reply({ content: `Игроков, использующих тег \`${tag}\` не найдено!` });
Expand Down
22 changes: 12 additions & 10 deletions src/commands/TokenCommand.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const PixelCommand = require('../structures/PixelCommand');
//const BansManager = require('../managers/BansManager');
const { EmbedBuilder } = require('discord.js');
const { EmbedBuilder, codeBlock} = require('discord.js');
const { ms } = require('../utils/PixelFunctions');
const fetch = require('node-fetch');

Expand Down Expand Up @@ -44,25 +44,25 @@ class TokenCommand extends PixelCommand {

const msg = await message.reply({ content: 'Производятся записи в базе данных и API Pixel Battle...' });

fetch(`${message.client.config.api_domain}/users/${user.id}/${args[0]}`, {
const request = await fetch(`${message.client.config.api_domain}/users/${user.id}/${args[0]}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await message.client.database.collection('users').findOne(
{ userID: message.author.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify({
timeout,
reason
})
});
}).then(res => res.json()).catch(() => {});

await message.client.database.collection('users').updateOne(
{
userID: user.id
},
if(request?.error ?? !request) return msg.reply({ content: request ? `Произошла ошибка при бане игрока\n${codeBlock('json', JSON.stringify(request))}` : 'От API поступил пустой ответ, возможно, стоит проверить его состояние' });

message.client.database.collection('users').updateOne(
{ userID: user.id },
{
$set: {
banned: action ? {
Expand All @@ -71,7 +71,8 @@ class TokenCommand extends PixelCommand {
reason
} : null
}
}
},
{ hint: { userID: 1 } }
)

msg.edit({
Expand Down Expand Up @@ -118,7 +119,8 @@ class TokenCommand extends PixelCommand {
$set: {
token: message.client.functions.generateToken(parseInt(data.token.split('.')[2], 36)),
}
}
},
{ hint: { userID: 1 } }
);

message.reply({ content: `Токен игрока ${`${user.globalName ?? user.username ?? user.tag} (**${user.id}**)`} был успешно перегенерирован!` });
Expand Down
2 changes: 1 addition & 1 deletion src/commands/UserCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UserCommand extends PixelCommand {
const msg = await message.reply({ content: 'Производится сбор данных о игроке...' });

const information = await message.client.database.collection('users')
.findOne({ userID: member.id }, { projection: { _id: 0, token: 1, tag: 1, points: 1, badges: 1, banned: 1 } });
.findOne({ userID: member.id }, { projection: { _id: 0, token: 1, tag: 1, points: 1, badges: 1, banned: 1 }, hint: { userID: 1 } });

return msg.edit({
content: null,
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/PointsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class PointsHelper {
return this.client.database.collection('users').updateOne({ userID }, {
$inc: {
points: changing
}
});
},

}, { hint: { userID: 1 } });
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/listeners/interactions/InteractionCreateListener.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const PixelListener = require('../../structures/PixelListener');
const fetch = require("node-fetch");
const { EmbedBuilder, codeBlock } = require('discord.js');
const { ObjectId } = require('mongodb');
const { EmbedBuilder, codeBlock, ActionRowBuilder, ButtonBuilder } = require('discord.js');

class InteractionCreateListener extends PixelListener {
constructor() {
Expand All @@ -24,11 +23,11 @@ class InteractionCreateListener extends PixelListener {

await interaction.deferReply({ ephemeral: true });

const _id = new ObjectId(interaction.customId.slice(8));
const user = await client.database.collection('users').findOne({ _id });
const userID = interaction.customId.slice(8);
const user = await client.database.collection('users').findOne({ userID }, { hint: { userID: 1 } });

if(!user)
return interaction.editReply('Игрок отсутствует в базе данных PixelBattle').catch(() => {});
return interaction.editReply('Игрок ещё не авторизовался в PixelBattle').catch(() => {});
if(user.banned)
return interaction.editReply('Игрок уже забанен').catch(() => {});

Expand All @@ -43,7 +42,7 @@ class InteractionCreateListener extends PixelListener {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await client.database.collection('users').findOne(
{ userID: interaction.user.id },
{ projection: { _id: 0, token: 1 } }
{ projection: { _id: 0, token: 1 }, hint: { userID: 1 } }
))?.token
},
body: JSON.stringify(ban_data)
Expand All @@ -52,15 +51,16 @@ class InteractionCreateListener extends PixelListener {
return interaction.editReply('Попытка бана прошла неудачно\n' + response ? codeBlock('json', JSON.stringify(response)) : '');

await client.database.collection('users').updateOne(
{ _id },
{ userID },
{
$set: {
banned: {
moderatorID: interaction.user.id,
...ban_data
}
}
}
},
{ hint: { userID: 1 } }
);

interaction.message.edit({
Expand All @@ -75,6 +75,13 @@ class InteractionCreateListener extends PixelListener {
`> Игрок был заблокирован (MOD: \`${interaction.user.id}\`)`
}
])
],
components: [
new ActionRowBuilder()
.addComponents([
new ButtonBuilder(interaction.message.components[0].components[0].data)
.setDisabled(true)
])
]
}).catch(() => {});

Expand Down
2 changes: 1 addition & 1 deletion src/services/AntiNewAccountService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AntiNewAccountService {
.setLabel('Забанить')
.setStyle(ButtonStyle.Danger)
.setEmoji('⚠️')
.setCustomId(`ban_acc_${(await this.client.database.collection('users').findOne({ userID: member.id }))?._id ?? member.id}`)
.setCustomId(`ban_acc_${member.id}`)
])
]
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/BanRemoverService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BanRemoverService {
}

clear(id) {
return this.client.database.collection('users').updateOne({ userID: id }, { $set: { banned: null } });
return this.client.database.collection('users').updateOne({ userID: id }, { $set: { banned: null } }, { hint: { userID: 1 } });
}

runClearance(checkInterval = 15000) {
Expand Down

0 comments on commit 6777c57

Please sign in to comment.