From eef2acf8e714d70b06be8d6190170674480ed4f0 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Tue, 5 Apr 2022 20:55:58 -0500 Subject: [PATCH] Add types for variables in commands --- src/commands/about.ts | 4 ++-- src/commands/help.ts | 4 ++-- src/commands/score.ts | 6 +++--- src/commands/top.ts | 12 ++++++------ src/commands/train.ts | 33 +++++++++++++++++---------------- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/commands/about.ts b/src/commands/about.ts index c990ad0..2dda5cd 100644 --- a/src/commands/about.ts +++ b/src/commands/about.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { MessageEmbed } from 'discord.js'; +import { MessageEmbed, CommandInteraction } from 'discord.js'; import gitlog from 'gitlog'; @@ -27,7 +27,7 @@ export const data = new SlashCommandBuilder() return subcommand; }); -export async function execute(interaction) { +export async function execute(interaction : CommandInteraction) { await interaction.deferReply(); const client = interaction.client; diff --git a/src/commands/help.ts b/src/commands/help.ts index 38d5887..017e27c 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,11 +1,11 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { MessageEmbed } from 'discord.js'; +import { MessageEmbed, CommandInteraction } from 'discord.js'; export const data = new SlashCommandBuilder() .setName('help') .setDescription('Replies with a help message explaining what the bot can do'); -export async function execute(interaction) { +export async function execute(interaction : CommandInteraction) { await interaction.deferReply(); const helpEmbed = new MessageEmbed() diff --git a/src/commands/score.ts b/src/commands/score.ts index c8f3db1..4428ec6 100644 --- a/src/commands/score.ts +++ b/src/commands/score.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { MessageEmbed } from 'discord.js'; +import { CommandInteraction, MessageEmbed } from 'discord.js'; import log from '../helpers/log'; import userScore from '../models/userScore'; @@ -16,7 +16,7 @@ export const data = new SlashCommandBuilder() return option; }); -export async function execute(interaction) { +export async function execute(interaction : CommandInteraction) { const scoreEmbed = new MessageEmbed() .setColor('#ffffff'); @@ -32,7 +32,7 @@ export async function execute(interaction) { } scoreEmbed - .setAuthor({ name: user.tag, iconURL: user.displayAvatarURL(true) }) + .setAuthor({ name: user.tag, iconURL: user.displayAvatarURL() }) .setDescription(`Score: \`${score.score}\``); await interaction.reply({ embeds: [scoreEmbed] }); diff --git a/src/commands/top.ts b/src/commands/top.ts index c9e1245..aa55825 100644 --- a/src/commands/top.ts +++ b/src/commands/top.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { MessageEmbed } from 'discord.js'; +import { CommandInteraction, MessageEmbed } from 'discord.js'; import log from '../helpers/log'; import userScore from '../models/userScore'; @@ -8,7 +8,7 @@ export const data = new SlashCommandBuilder() .setName('top') .setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'); -export async function execute(interaction) { +export async function execute(interaction : CommandInteraction) { await interaction.deferReply(); userScore @@ -27,7 +27,7 @@ export async function execute(interaction) { ); } - const embeds : unknown[] = []; + const embeds : MessageEmbed[] = []; let lbMessageContent = ''; for (let i = 0; i < 10; i++) { @@ -42,16 +42,16 @@ export async function execute(interaction) { embeds.push(leaderboardEmbed); let sMessageContent = ''; - const members = await interaction.guild.members.fetch(); + const members = await interaction.guild?.members.fetch(); - const serverLeaderBoardArray = await obj.filter(o => members.some(m => m.user.id === o.authorID)); + const serverLeaderBoardArray = await obj.filter(o => members?.some(m => m.user.id === o.authorID)); if (serverLeaderBoardArray.length > 10) { for (let i = 0; i < 10; i++) { sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`; } const sLeaderboardEmbed = new MessageEmbed() - .setTitle(`Top Ten in ${interaction.guild.name}!`) + .setTitle(`Top Ten in ${interaction.guild?.name}!`) .setDescription(sMessageContent) .setColor('#ffffff'); diff --git a/src/commands/train.ts b/src/commands/train.ts index ff1227b..3661895 100644 --- a/src/commands/train.ts +++ b/src/commands/train.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from '@discordjs/builders'; -import { MessageEmbed, MessageActionRow, MessageButton } from 'discord.js'; +import { MessageEmbed, MessageActionRow, MessageButton, CommandInteraction, Message } from 'discord.js'; import { decode } from 'html-entities'; import axios from 'axios'; @@ -28,10 +28,10 @@ export const data = new SlashCommandBuilder() return option; }); -export async function execute(interaction) { +export async function execute(interaction : CommandInteraction) { await interaction.deferReply(); - const subject = interaction.options.get('subject') ? interaction.options.get('subject').value : null; + const subject = interaction.options.get('subject') ? interaction.options.get('subject')?.value : null; const authorId = interaction.user.id; let score; userScore @@ -83,11 +83,11 @@ export async function execute(interaction) { categoryArray = ['ENERGY']; break; default: - interaction.followUp( - new MessageEmbed() + interaction.followUp({ + embeds: [new MessageEmbed() .setDescription('<:red_x:816791117671825409> Not a valid subject!') - .setColor('#ffffff'), - ); + .setColor('#ffffff')], + }); return; } @@ -103,8 +103,9 @@ export async function execute(interaction) { answers[1] = answers[1].slice(0, answers[1].length - 1); // If there are multiple elements, it means there was an 'accept' and therefore a trailing ')' which should be removed answers = [answers[0], ...answers[1].split(new RegExp(' OR ', 'i'))]; // Use the first element plus the last element split by 'OR' case insensitive } - interaction.followUp({ content: decode(tossupQuestion), fetchMessage: true }) - .then(questionMessage => { + interaction.followUp({ content: decode(tossupQuestion), fetchReply: true }) + .then(q => { + const questionMessage = q as Message; const sourceButton = new MessageActionRow() .addComponents( new MessageButton() @@ -115,18 +116,18 @@ export async function execute(interaction) { switch (tossupFormat) { case 'Short Answer': { // eslint-disable-next-line no-case-declarations - const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user.id; - interaction.channel.awaitMessages({ + const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user?.id; + interaction.channel?.awaitMessages({ filter: messageFilter, max: 1, }) .then(collected => { const answerMsg = collected.first(); - if (answerMsg.author.id === interaction.client.user.id) return; + if (answerMsg?.author.id === interaction.client.user?.id) return; let predicted = ''; - if (answerMsg.content.toLowerCase() === tossupAnswer.toLowerCase() || answers.includes(answerMsg.content.toUpperCase())) { + if (answerMsg?.content.toLowerCase() === tossupAnswer.toLowerCase() || answers.includes(answerMsg?.content.toUpperCase())) { predicted = 'correct'; } else { @@ -135,12 +136,12 @@ export async function execute(interaction) { if (predicted === 'correct') { updateScore(true, score, authorId).then((msgToReply) => - answerMsg.reply(msgToReply), + answerMsg?.reply(msgToReply), ); } else { const overrideEmbed = new MessageEmbed() - .setAuthor({ name: answerMsg.author.tag, iconURL: answerMsg.author.displayAvatarURL() }) + .setAuthor({ name: answerMsg?.author.tag ? answerMsg.author.tag : '', iconURL: answerMsg?.author.displayAvatarURL() }) .addField('Correct answer', `\`${tossupAnswer}\``) .setDescription('It seems your answer was incorrect. Please react with <:override:955265585086857236> to override your answer if you think you got it right.') .setColor('#ffffff') @@ -152,7 +153,7 @@ export async function execute(interaction) { .setEmoji('<:override:955265585086857236>') .setStyle('SECONDARY'), ); - answerMsg.channel.send({ + answerMsg?.channel.send({ embeds: [overrideEmbed], components: [overrideButton], })