Skip to content

Commit

Permalink
feat: use only guild user model
Browse files Browse the repository at this point in the history
feat: use guild user instead of silenced model

feat: use guild user instead of balance model
  • Loading branch information
Guilhermeasper committed Mar 31, 2024
1 parent f9d40f5 commit c8a3acb
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 131 deletions.
46 changes: 34 additions & 12 deletions src/bot/commands/slashCommands/arrest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildMember, SlashCommandBuilder } from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import ArrestedModel from '@schemas/arrested';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

export const arrest: SlashCommand = {
command: new SlashCommandBuilder()
Expand All @@ -15,31 +15,53 @@ export const arrest: SlashCommand = {
),
execute: async (interaction) => {
const arrested = interaction.options.get('preso')?.member as GuildMember;
if (arrested.user.id === process.env.BOT_ID) {
arrestMember(interaction.member as GuildMember);
interaction.reply({ content: 'Tu realmente tentou essa?' });
if (arrested.user.id === interaction.client.user?.id) {
await arrestMember(interaction.member as GuildMember);
await interaction.reply({ content: 'Tu realmente tentou essa?' });
return;
}

if (arrested.user.bot) {
interaction.reply({ content: 'Não pode prender meus irmãos bots.' });
await interaction.reply({
content: 'Não pode prender meus irmãos bots.',
});
return;
}

arrestMember(arrested);
const arrestResult = await arrestMember(arrested);
if (!arrestResult) {
await interaction.reply({ content: `${arrested} já está preso!` });
return;
}
interaction.reply({ content: `${arrested} você está PRESO!` });
},
cooldown: 10,
};

function arrestMember(member: GuildMember) {
async function arrestMember(member: GuildMember) {
const memberChannelId = member.voice.channelId;
const newArrested = new ArrestedModel({
id: member.id,
user: member.user.username,
});
newArrested.save();
if (memberChannelId && memberChannelId != member.guild.afkChannelId) {
member.voice.setChannel(member.guild.afkChannelId);
}

const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});

if (!guildUser) {
const newGuildUser = new GuildUserModel({
guildId: member.guild.id,
userId: member.id,
arrested: true,
});
return await newGuildUser.save();
}

if (guildUser.arrested) {
return null;
}

guildUser.arrested = true;
return await guildUser.save();
}
39 changes: 23 additions & 16 deletions src/bot/commands/slashCommands/arrested.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import ArrestedModel from '@schemas/arrested';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

export const arrested: SlashCommand = {
command: new SlashCommandBuilder()
.setName('encarcerados')
.setDescription('Te dou uma lista de quem tá preso'),
execute: async (interaction) => {
const arrested = await (await getArrested()).toArray();
interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
arrested
.map((a) => a.user)
.toString()
.replace(',', '\n')
)
.setTitle('Segue lista dos criminosos:'),
],
const arrested = await getArrested(
(interaction.member as GuildMember).guild.id
);
const listOfArresteds: string = arrested
.map((prisioner) => `:locked: <@${prisioner.userId}>`)
.join('\n');

const arrestedsEmbed = interaction.client
.baseEmbed()
.setTitle(':rotating_light: Lista dos criminosos :rotating_light:')
.setTimestamp();

if (listOfArresteds) {
arrestedsEmbed.setDescription(`${listOfArresteds}`);
} else {
arrestedsEmbed.setDescription('**Ninguém está preso!**');
}
return await interaction.reply({
embeds: [arrestedsEmbed],
});
},
cooldown: 10,
};

async function getArrested() {
return ArrestedModel.collection.find();
async function getArrested(guildId: string) {
return await GuildUserModel.find({ guildId, arrested: true });
}
44 changes: 29 additions & 15 deletions src/bot/commands/slashCommands/release.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildMember, SlashCommandBuilder } from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import ArrestedModel from '@schemas/arrested';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

export const release: SlashCommand = {
command: new SlashCommandBuilder()
Expand All @@ -14,36 +14,50 @@ export const release: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
// Gets the member chosen to arrest
const arrested = interaction.options.get('preso')?.member as GuildMember;

// User cannot releat themselves
if (interaction.member === arrested) {
interaction.reply({
content: `Cara, e desde quando os presos têm a chave da cela?`,
});
return;
}

// Search for the member in the BD
const result = await findAndReleaseMember(
arrested.id,
arrested.user.username
);
const releaseResult = await findAndReleaseMember(arrested);

if (result?.value) {
interaction.reply({ content: `Abrindo a cela do ${arrested}.` });
return;
if (releaseResult) {
return await interaction.reply({
content: `Abrindo a cela do ${arrested}.`,
});
} else if (releaseResult === null) {
return await interaction.reply({
content: `O ${arrested} nunca foi preso.`,
});
}

interaction.reply({
content: `Não acho que o ${arrested.nickname} tava preso não.`,
content: `Não acho que o ${arrested} tava preso não.`,
});
},
cooldown: 10,
};

function findAndReleaseMember(id: string, user: string) {
// Deletes user from DB. If not found, return object with value property null
return ArrestedModel.collection.findOneAndDelete({ id, user });
async function findAndReleaseMember(member: GuildMember) {
const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});

if (!guildUser) {
return null;
}

if (!guildUser.arrested) {
return false;
}

guildUser.arrested = false;
await guildUser.save();

return true;
}
52 changes: 38 additions & 14 deletions src/bot/commands/slashCommands/silence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildMember, SlashCommandBuilder } from 'discord.js';
import { Guild, GuildMember, SlashCommandBuilder } from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import SilencedModel from '@schemas/silenced';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

export const silence: SlashCommand = {
command: new SlashCommandBuilder()
Expand All @@ -16,28 +16,52 @@ export const silence: SlashCommand = {
execute: async (interaction) => {
const silenced = interaction.options.get('silenciado')
?.member as GuildMember;
if (silenced.user.id === process.env.BOT_ID) {
silenceMember(interaction.member as GuildMember);
interaction.reply({ content: 'Po, vei, seja inteligente' });
if (silenced.user.id === interaction.client.user?.id) {
await silenceMember(interaction.member as GuildMember);
await interaction.reply({ content: 'Po, vei, seja inteligente' });
return;
}

if (silenced.user.bot) {
interaction.reply({ content: 'Não pode fazer isso com meus os bots.' });
await interaction.reply({
content: 'Não pode fazer isso com meus amigos bots.',
});
return;
}

silenceMember(silenced);
interaction.reply({ content: `${silenced} SILÊNCIO!` });
const userSilenced = silenceMember(silenced);

if (!userSilenced) {
await interaction.reply({ content: `${silenced} já está caladinho!` });
return;
}
await interaction.reply({ content: `${silenced} SILÊNCIO!` });
},
cooldown: 10,
};

// TODO => Segregate model by guildId
function silenceMember(member: GuildMember) {
const newSilenced = new SilencedModel({
id: member.id,
user: member.user.username,
async function silenceMember(member: GuildMember) {
const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});
newSilenced.save();

if (!guildUser) {
const newGuildUser = new GuildUserModel({
guildId: member.guild.id,
userId: member.id,
silenced: true,
});
await newGuildUser.save();
return true;
}

if (guildUser.silenced) {
return null;
}

guildUser.silenced = true;
await guildUser.save();

return true;
}
34 changes: 25 additions & 9 deletions src/bot/commands/slashCommands/unsilence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GuildMember, SlashCommandBuilder } from 'discord.js';
import { SlashCommand } from '@marquinhos/types';
import SilencedModel from '@schemas/silenced';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

export const unsilence: SlashCommand = {
command: new SlashCommandBuilder()
Expand All @@ -24,12 +24,14 @@ export const unsilence: SlashCommand = {
}

// Search for the member in the BD
const result = await findAndUnsilenceMember(
unsilenced.id,
unsilenced.user.username
);
const unsilencedGuildUser = await findAndUnsilenceMember(unsilenced);

if (result?.value) {
if (unsilencedGuildUser === null) {
interaction.reply({ content: `O ${unsilenced} nunca foi silenciado.` });
return;
}

if (unsilencedGuildUser) {
interaction.reply({ content: `${unsilenced} pode falar novamente.` });
return;
}
Expand All @@ -41,7 +43,21 @@ export const unsilence: SlashCommand = {
cooldown: 10,
};

function findAndUnsilenceMember(id: string, user: string) {
// Deletes user from DB. If not found, return object with value property null
return SilencedModel.collection.findOneAndDelete({ id, user });
async function findAndUnsilenceMember(member: GuildMember) {
const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});

if (!guildUser) {
return null;
}

if (!guildUser.silenced) {
return false;
}

guildUser.silenced = false;
await guildUser.save();
return true;
}
14 changes: 10 additions & 4 deletions src/bot/events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { BotEvent } from '@marquinhos/types';
import { logger } from '@utils/logger';
import { safeExecute } from '@utils/errorHandling';
import { musicBotMessageHandler } from '@utils/lastfm';
import SilencedModel from '@schemas/silenced';
import BotError from '@utils/botError';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

dotenv.config();

Expand Down Expand Up @@ -148,8 +148,14 @@ async function silencedUserHandler(message: Message) {
}

async function isUserSilenced(member: GuildMember) {
return await SilencedModel.collection.findOne({
id: member.id,
user: member.user.username,
const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});

if (!guildUser) {
return false;
}

return guildUser.silenced;
}
14 changes: 10 additions & 4 deletions src/bot/events/voiceStateUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildMember, VoiceState } from 'discord.js';

import { BotEvent } from '@marquinhos/types';
import ArrestedModel from '@schemas/arrested';
import GuildUserModel from '@marquinhos/database/schemas/guildUser';

// WIP
export const voiceStateUpdate: BotEvent = {
Expand Down Expand Up @@ -47,10 +47,16 @@ async function userChangedVoiceState(member: GuildMember) {
}

async function isUserArrested(member: GuildMember) {
return ArrestedModel.collection.findOne({
id: member.id,
user: member.user.username,
const guildUser = await GuildUserModel.findOne({
guildId: member.guild.id,
userId: member.id,
});

if (!guildUser) {
return false;
}

return guildUser.arrested;
}

async function arrestUser(member: GuildMember) {
Expand Down
11 changes: 0 additions & 11 deletions src/database/schemas/arrested.ts

This file was deleted.

Loading

0 comments on commit c8a3acb

Please sign in to comment.