Browse Source

Add types for variables in commands

pull/41/head
Abheek Dhawan 3 years ago
parent
commit
eef2acf8e7
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 4
      src/commands/about.ts
  2. 4
      src/commands/help.ts
  3. 6
      src/commands/score.ts
  4. 12
      src/commands/top.ts
  5. 33
      src/commands/train.ts

4
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;

4
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()

6
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] });

12
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');

33
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],
})

Loading…
Cancel
Save