Skip to content

Commit

Permalink
refactor: enable typescript strict rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhermeasper committed Mar 23, 2024
1 parent 14d50d2 commit 5699b60
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 70 deletions.
14 changes: 9 additions & 5 deletions src/bot/commands/slashCommands/addToRoulette.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
CommandInteraction,
GuildMember,
PermissionsBitField,
SlashCommandBuilder,
} from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import { Nullable, SlashCommand } from '@marquinhos/types';
import GuildModel from '@schemas/guild';

export const addToRoulette: SlashCommand = {
Expand All @@ -20,17 +21,20 @@ export const addToRoulette: SlashCommand = {
.setDescription('Essa é a pessoa que vou adicionar pra a roleta.')
.setRequired(true)
),
execute: async (interaction) => {
const newMember = interaction.options.get('usuario').member as GuildMember;
addMemberToRoulette(interaction.guild.id, newMember.id);
execute: async (interaction: CommandInteraction) => {
const newMember = interaction.options.get('usuario')?.member as GuildMember;
addMemberToRoulette(interaction.guild?.id, newMember.id);
interaction.reply(
`Agora adicionei ${newMember} na roleta de admins. Parabéns e tals.`
);
},
};

// TODO => Check if member already exists in collection's array
async function addMemberToRoulette(guildID: string, memberId: string) {
async function addMemberToRoulette(
guildID: Nullable<string>,
memberId: string
) {
GuildModel.collection.updateOne(
{
guildID,
Expand Down
43 changes: 26 additions & 17 deletions src/bot/commands/slashCommands/allowUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GuildMemberRoleManager,
} from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import { Nullable, SlashCommand } from '@marquinhos/types';
import GuildModel from '@schemas/guild';

export const allowUser: SlashCommand = {
Expand All @@ -18,19 +18,28 @@ export const allowUser: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
const userId = interaction.options.get('usuário').value as string;
let externalRoleId = '';
let baseRoleId = '';
let vipRoleId = '';
const userId = interaction.options.get('usuário')
?.value as Nullable<string>;
const roles = {
externalRoleId: null,
baseRoleId: null,
vipRoleId: null,
} as {
externalRoleId: Nullable<string>;
baseRoleId: Nullable<string>;
vipRoleId: Nullable<string>;
};

try {
const guildRoles = await findRoles(interaction.guild?.id as string);
const guildRoles = await findRoles(
interaction.guild?.id ?? ('' as string)
);

externalRoleId = guildRoles.externalRoleId;
baseRoleId = guildRoles.baseRoleId;
vipRoleId = guildRoles.vipRoleId;
roles.externalRoleId = guildRoles.externalRoleId;
roles.baseRoleId = guildRoles.baseRoleId;
roles.vipRoleId = guildRoles.vipRoleId;

if (!externalRoleId || !baseRoleId || !vipRoleId) {
if (!roles.externalRoleId || !roles.baseRoleId || !roles.vipRoleId) {
throw new Error('Roles not configured');
}
} catch (error: unknown) {
Expand All @@ -48,7 +57,7 @@ export const allowUser: SlashCommand = {

if (
!(interaction.member?.roles as GuildMemberRoleManager).cache.has(
vipRoleId
roles.vipRoleId
)
) {
await interaction.reply({
Expand All @@ -61,7 +70,7 @@ export const allowUser: SlashCommand = {
throw new Error('User not allowed');
}

const member = await interaction.guild?.members.fetch(userId);
const member = await interaction.guild?.members.fetch(userId ?? '');

if (!member) {
await interaction.reply({
Expand All @@ -77,9 +86,9 @@ export const allowUser: SlashCommand = {
const memberRoles = member.roles.cache;

try {
if (memberRoles.has(externalRoleId)) {
await member.roles.remove(externalRoleId);
} else if (memberRoles.has(baseRoleId)) {
if (memberRoles.has(roles.externalRoleId)) {
await member.roles.remove(roles.externalRoleId);
} else if (memberRoles.has(roles.baseRoleId)) {
await interaction.reply({
embeds: [
new EmbedBuilder()
Expand All @@ -90,8 +99,8 @@ export const allowUser: SlashCommand = {
throw new Error('User already allowed');
}

if (!memberRoles.has(baseRoleId)) {
await member.roles.add(baseRoleId);
if (!memberRoles.has(roles.baseRoleId)) {
await member.roles.add(roles.baseRoleId);
}
} catch (error) {
await interaction.reply({
Expand Down
4 changes: 2 additions & 2 deletions src/bot/commands/slashCommands/anom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export const anom: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
const channel = interaction.options.get('canal').channel as TextChannel;
const message = interaction.options.get('mensagem').value as string;
const channel = interaction.options.get('canal')?.channel as TextChannel;
const message = interaction.options.get('mensagem')?.value as string;
channel.send({
embeds: [
new EmbedBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/slashCommands/arrest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const arrest: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
const arrested = interaction.options.get('preso').member as GuildMember;
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?' });
Expand Down
4 changes: 2 additions & 2 deletions src/bot/commands/slashCommands/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export const audio: SlashCommand = {
execute: (interaction) => {
const channel = voiceChannelPresence(interaction);
const file = interaction.options.get('audio');
playAudio(interaction, channel, file.value as string);
playAudio(interaction, channel, file?.value as string);
interaction.reply({
embeds: [
new EmbedBuilder().setDescription(
`Reproduzindo ${file.value as string}`
`Reproduzindo ${file?.value as string}`
),
],
});
Expand Down
3 changes: 2 additions & 1 deletion src/bot/commands/slashCommands/checkIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const checkIn: SlashCommand = {
};

export const checkInReply = (member: GuildMember, guildName: string) => {
const memberJoinedDate = new Date(member.joinedTimestamp);
if (!member) return 'Você não está em um servidor';
const memberJoinedDate = new Date(member?.joinedTimestamp as number);
const formatedMemberJoinedTimestamp = memberJoinedDate.toLocaleString(
'pt-BR',
DATE_LOCALE_CONFIG
Expand Down
3 changes: 2 additions & 1 deletion src/bot/commands/slashCommands/importunate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const importunate: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
const member = interaction.options.get('importunado').member as GuildMember;
const member = interaction.options.get('importunado')
?.member as GuildMember;

if (member.user.bot) {
interaction.reply({ content: 'Nunca vou conseguir irritar um bot' });
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/slashCommands/lastfmCharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const lastfmCharts: SlashCommand = {
period.value as string
);

await interaction.channel.send({
await interaction.channel?.send({
files: [image],
content: `${profileName} aqui estão ${
type.value == 'tracks' ? 'as suas' : 'os seus'
Expand Down
27 changes: 21 additions & 6 deletions src/bot/commands/slashCommands/minecraftStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChannelType,
PermissionsBitField,
TextChannel,
Snowflake,
} from 'discord.js';
import * as util from 'minecraft-server-util';

Expand Down Expand Up @@ -34,8 +35,22 @@ export const minecraftStatus: SlashCommand = {

const minecraftServer = MinecraftServerStatus.getInstance();

const ip = interaction.options.get('ip').value as string;
const port = interaction.options.get('port').value as number;
const ip = interaction.options.get('ip')?.value as string;
const port = interaction.options.get('port')?.value as number;
const guildID = interaction.guildId as string;

if (!guildID) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle(`Falha ao criar o monitoramento!`)
.setDescription(
`Não foi possível encontrar o ID do servidor de discord!`
),
],
});
return;
}

try {
status = await util.queryFull(ip, port);
Expand All @@ -59,20 +74,20 @@ export const minecraftStatus: SlashCommand = {

const statusChannel =
minecraftStatusChannel ??
(await interaction.guild.channels.create({
(await interaction.guild?.channels.create({
name: 'minecraft-status',
type: ChannelType.GuildText,
topic: 'Status dos servidores de minecraft',
permissionOverwrites: [
{
id: interaction.guildId,
id: interaction.guildId as Snowflake,
deny: [PermissionsBitField.Flags.SendMessages],
},
],
}));

const existingServer = await MinecraftServerModel.findOne({
guildID: interaction.guildId,
guildID,
channelID: statusChannel.id,
host: ip,
port,
Expand All @@ -99,7 +114,7 @@ export const minecraftStatus: SlashCommand = {
});

await minecraftServer.addNewServer({
guildID: interaction.guildId,
guildID,
channelID: statusChannel.id,
messageID: messageEmbed.id,
host: ip,
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/slashCommands/moveAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const moveAll: SlashCommand = {
const member = interaction.member as GuildMember;
const voiceChannel = member.voice.channel;
const newVoiceChannel = interaction.options.get('canal')
.channel as VoiceChannel;
?.channel as VoiceChannel;

if (!voiceChannel) {
await interaction.reply('Mas tu nem tá num canal de voz vei :(');
Expand Down
4 changes: 2 additions & 2 deletions src/bot/commands/slashCommands/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const release: SlashCommand = {
),
execute: async (interaction) => {
// Gets the member chosen to arrest
const arrested = interaction.options.get('preso').member as GuildMember;
const arrested = interaction.options.get('preso')?.member as GuildMember;

// User cannot releat themselves
if (interaction.member === arrested) {
Expand All @@ -31,7 +31,7 @@ export const release: SlashCommand = {
arrested.user.username
);

if (result.value) {
if (result?.value) {
interaction.reply({ content: `Abrindo a cela do ${arrested}.` });
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/slashCommands/silence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const silence: SlashCommand = {
),
execute: async (interaction) => {
const silenced = interaction.options.get('silenciado')
.member as GuildMember;
?.member as GuildMember;
if (silenced.user.id === process.env.BOT_ID) {
silenceMember(interaction.member as GuildMember);
interaction.reply({ content: 'Po, vei, seja inteligente' });
Expand Down
20 changes: 13 additions & 7 deletions src/bot/commands/slashCommands/toggleAdminRoulette.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PermissionsBitField, SlashCommandBuilder } from 'discord.js';

import { SlashCommand } from '@marquinhos/types';
import { Nullable, SlashCommand } from '@marquinhos/types';
import GuildModel from '@schemas/guild';

export const toggleAdminRoulette: SlashCommand = {
Expand All @@ -15,14 +15,14 @@ export const toggleAdminRoulette: SlashCommand = {
.setRequired(true)
),
execute: async (interaction) => {
const isRouletteOn = await getAdminRouletteValue(interaction.guild.id);
const optionValue = interaction.options.get('ligado').value as boolean;
const isRouletteOn = await getAdminRouletteValue(interaction.guild?.id);
const optionValue = interaction.options.get('ligado')?.value as boolean;
if (isRouletteOn === optionValue) {
interaction.reply('Mas já tava assim... Eu não fiz nada de novo..');
return;
}

setAdminRouletteValue(interaction.guild.id, optionValue);
setAdminRouletteValue(interaction.guild?.id, optionValue);
if (!optionValue) {
interaction.reply('Roleta de admins desligada.');
return;
Expand All @@ -34,12 +34,18 @@ export const toggleAdminRoulette: SlashCommand = {
cooldown: 10,
};

async function getAdminRouletteValue(guildID: string) {
async function getAdminRouletteValue(guildID: Nullable<string>) {
const guild = await GuildModel.collection.findOne({ guildID });
return guild.roulette.isRouletteOn;
return guild?.roulette.isRouletteOn;
}

async function setAdminRouletteValue(guildID: string, value: boolean) {
async function setAdminRouletteValue(
guildID: Nullable<string>,
value: boolean
) {
if (!guildID) {
return;
}
await GuildModel.updateOne(
{
guildID,
Expand Down
4 changes: 2 additions & 2 deletions src/bot/commands/slashCommands/unsilence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const unsilence: SlashCommand = {
execute: async (interaction) => {
// Gets the member chosen to unsilence
const unsilenced = interaction.options.get('liberado')
.member as GuildMember;
?.member as GuildMember;

// Users cannot unsilence themselves
if (interaction.member === unsilenced) {
Expand All @@ -29,7 +29,7 @@ export const unsilence: SlashCommand = {
unsilenced.user.username
);

if (result.value) {
if (result?.value) {
interaction.reply({ content: `${unsilenced} pode falar novamente.` });
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/bot/events/guildMemberAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const guildMemberAdd: BotEvent = {
}

const role = member.guild.roles.cache.find((r) => r.id === externalRoleId);

if (!role) {
throw new Error('Role not found');
}
await member.roles.add(role);
try {
member.send(
Expand Down
Loading

0 comments on commit 5699b60

Please sign in to comment.