Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat: blocking users + reduce db requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Jul 10, 2023
1 parent 5b7bc7f commit 8f23939
Show file tree
Hide file tree
Showing 26 changed files with 404 additions and 205 deletions.
6 changes: 3 additions & 3 deletions src/buttons/appeals/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ module.exports = {
if(i.customId === `modal-${interaction.id}`) {
const id = i.fields.getTextInputValue(`modal-appealid-${interaction.id}`);

if(!await Appeal.exists({ _id: id })) {
const data = await Appeal.findOne({ _id: id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} That appeal does not exist!`)
Expand All @@ -42,8 +44,6 @@ module.exports = {
return;
}

const data = await Appeal.findOne({ _id: id });

if(data.id !== interaction.user.id) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
Expand Down
8 changes: 4 additions & 4 deletions src/buttons/appeals/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module.exports = {
requiredRoles: [],
async execute(interaction, client, Discord) {
try {
if(!await BannedUser.exists({ _id: interaction.user.id })) {
const banData = await BannedUser.findOne({ _id: interaction.user.id });

if(!banData) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You are not banned!`)
Expand All @@ -18,7 +20,7 @@ module.exports = {
return;
}

if(await BannedUser.exists({ _id: interaction.user.id, allowAppeal: false }) || await Appeal.exists({ id: interaction.user.id, status: "DENIED" })) {
if(!banData.allowAppeal || await Appeal.exists({ id: interaction.user.id, status: "DENIED" })) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You are allowed to submit an appeal!`)
Expand Down Expand Up @@ -74,8 +76,6 @@ module.exports = {
const banReason = i.fields.getTextInputValue(`modal-banreason-${id}`);
const unbanReason = i.fields.getTextInputValue(`modal-unbanreason-${id}`);

const banData = await BannedUser.findOne({ _id: interaction.user.id });

const embed = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.addFields (
Expand Down
6 changes: 3 additions & 3 deletions src/buttons/logs/blocked-message-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module.exports = {
try {
const id = interaction.customId.replace("blocked-message-info-", "");

if(!await BlockedMessage.exists({ _id: id })) {
const data = await BlockedMessage.findOne({ _id: id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} No message was found with that ID!`)
Expand All @@ -19,8 +21,6 @@ module.exports = {
return;
}

const data = await BlockedMessage.findOne({ _id: id });

const info = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.addFields (
Expand Down
6 changes: 3 additions & 3 deletions src/buttons/logs/delete-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = {
const id = interaction.customId.replace("delete-message-", "");
const modLogsChannel = client.channels.cache.get(client.config_channels.modLogs);

if(!await Message.exists({ _id: id })) {
const data = await Message.findOne({ _id: id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} No message was found with that ID!`)
Expand All @@ -20,8 +22,6 @@ module.exports = {
return;
}

const data = await Message.findOne({ _id: id });

const total = data.messages.length;
let deleted = 0;

Expand Down
6 changes: 3 additions & 3 deletions src/buttons/logs/message-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module.exports = {
try {
const id = interaction.customId.replace("message-info-", "");

if(!await Message.exists({ _id: id })) {
const data = await Message.findOne({ _id: id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} No message was found with that ID!`)
Expand All @@ -19,8 +21,6 @@ module.exports = {
return;
}

const data = await Message.findOne({ _id: id });

const info = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.addFields (
Expand Down
6 changes: 3 additions & 3 deletions src/buttons/sentry/capture-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ module.exports = {
if(i.customId === `modal-${interaction.id}`) {
const token = i.fields.getTextInputValue(`modal-token-${interaction.id}`);

if(!await SentryCapture.exists({ _id: token })) {
const data = await SentryCapture.findOne({ _id: token });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} That token does not exist!`)
Expand All @@ -41,8 +43,6 @@ module.exports = {
return;
}

const data = await SentryCapture.findOne({ _id: token });

const tokenInfo = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("ℹ️ Token Information")
Expand Down
12 changes: 8 additions & 4 deletions src/commands/dev/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ module.exports = {
return;
}

if(await User.exists({ _id: user.id, mod: true })) {
const userData = await User.findOne({ _id: user.id });

if(userData.mod) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is already a moderator!`)
Expand All @@ -216,7 +218,7 @@ module.exports = {
return;
}

if(!await User.exists({ _id: user.id })) {
if(!userData) {
new User({ _id: user.id, mod: true }).save();
} else {
User.findOneAndUpdate({ _id: user.id }, { mod: true }, (err, data) => {});
Expand Down Expand Up @@ -524,7 +526,9 @@ module.exports = {
return;
}

if(await User.exists({ _id: user.id, verified: true })) {
const userData = await User.findOne({ _id: user.id });

if(userData.verified) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is already verified!`)
Expand All @@ -533,7 +537,7 @@ module.exports = {
return;
}

if(!await User.exists({ _id: user.id })) {
if(!userData) {
new User({ _id: user.id, verified: true }).save();
} else {
User.findOneAndUpdate({ _id: user.id }, { verified: true }, (err, data) => {});
Expand Down
6 changes: 4 additions & 2 deletions src/commands/dev/sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ module.exports = {
if(interaction.options.getSubcommand() === "deregister") {
const token = interaction.options.getString("token");

if(!await SentryCapture.exists({ _id: token })) {
const data = await SentryCapture.findOne({ _id: token });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} That capture token does not exist!`)
Expand All @@ -122,7 +124,7 @@ module.exports = {
return;
}

await SentryCapture.findOneAndDelete({ _id: token });
await data.delete();

const deleted = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
Expand Down
95 changes: 95 additions & 0 deletions src/commands/guild/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const { PermissionFlagsBits } = require("discord.js");

const emoji = require("../../config.json").emojis;

const Guild = require("../../models/Guild");
const User = require("../../models/User");

module.exports = {
name: "block",
description: "Block a user's messages sending to this guild.",
options: [
{
type: 6,
name: "user",
description: "The user to block.",
required: true
}
],
default_member_permissions: PermissionFlagsBits.ManageGuild.toString(),
botPermissions: [],
requiredRoles: [],
cooldown: 10,
enabled: true,
hidden: false,
deferReply: true,
ephemeral: true,
async execute(interaction, client, Discord) {
try {
const user = interaction.options.getUser("user");

let data = await Guild.findOne({ _id: interaction.guild.id });
const userData = await User.findOne({ _id: user.id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This guild is not registered!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(data.blockedUsers.includes(user.id)) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} That user is already blocked!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(userData.dev || userData.mod) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You cannot block Global Chat staff!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(user.id === interaction.guild.ownerId) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You cannot block the guild owner!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(user.bot) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You cannot block bots!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(!data.blockedUsers) {
Guild.findOneAndUpdate({ _id: interaction.guild.id }, { blockedUsers: [user.id] }, (err, data) => {});
} else {
data.blockedUsers.push(user.id);
await data.save();
}

const blocked = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.tick} ${user} has been blocked!`)

await interaction.editReply({ embeds: [blocked] });
} catch(err) {
client.logCommandError(err, interaction, Discord);
}
}
}
51 changes: 51 additions & 0 deletions src/commands/guild/blocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { PermissionFlagsBits } = require("discord.js");

const emoji = require("../../config.json").emojis;

const Guild = require("../../models/Guild");

module.exports = {
name: "blocked",
description: "Get a list of all the guild's blocked users.",
options: [],
default_member_permissions: PermissionFlagsBits.ManageGuild.toString(),
botPermissions: [],
requiredRoles: [],
cooldown: 10,
enabled: true,
hidden: false,
deferReply: true,
ephemeral: true,
async execute(interaction, client, Discord) {
try {
const data = await Guild.findOne({ _id: interaction.guild.id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This guild is not registered!`)

await interaction.editReply({ embeds: [error] });
return;
}

if(!data.blockedUsers.length) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This guild has not blocked any users!`)

await interaction.editReply({ embeds: [error] });
return;
}

const blocked = new Discord.EmbedBuilder()
.setColor(client.config_embeds.red)
.setTitle("⛔ Blocked Users")
.setDescription(`<@${data.blockedUsers.join(">, <@")}>`)

await interaction.editReply({ embeds: [blocked] });
} catch(err) {
client.logCommandError(err, interaction, Discord);
}
}
}
10 changes: 5 additions & 5 deletions src/commands/guild/delete-guild-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = {
ephemeral: true,
async execute(interaction, client, Discord) {
try {
if(!await Guild.exists({ _id: interaction.guild.id })) {
const data = await Guild.findOne({ _id: interaction.guild.id });

if(!data) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} There is no data associated with this guild!`)
Expand All @@ -29,7 +31,7 @@ module.exports = {
return;
}

const embed = new Discord.EmbedBuilder()
const confirmation = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("Confirmation")
.setDescription("Are you sure you want to delete all data associated with this guild?\n\nThe global chat channel will be **deregistered** and any webhooks will be **deleted**.\n**This cannot be undone.**")
Expand All @@ -48,7 +50,7 @@ module.exports = {
.setLabel("Cancel")
)

await interaction.editReply({ embeds: [embed], components: [actions] })
await interaction.editReply({ embeds: [confirmation], components: [actions] })
const collector = interaction.channel.createMessageComponentCollector({ componentType: Discord.ComponentType.Button, time: 30000 });

collector.on("collect", async c => {
Expand All @@ -64,8 +66,6 @@ module.exports = {
if(c.customId === `delete-${interaction.id}`) {
collector.stop();

const data = await Guild.findOne({ _id: interaction.guild.id });

if(data.webhook) {
if(await checkWebhook(data.webhook)) fetch(data.webhook, { method: "DELETE" });
}
Expand Down
Loading

0 comments on commit 8f23939

Please sign in to comment.