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 { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
import gitlog from 'gitlog'; import gitlog from 'gitlog';
@ -27,7 +27,7 @@ export const data = new SlashCommandBuilder()
return subcommand; return subcommand;
}); });
export async function execute(interaction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
const client = interaction.client; const client = interaction.client;

4
src/commands/help.ts

@ -1,11 +1,11 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('help') .setName('help')
.setDescription('Replies with a help message explaining what the bot can do'); .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(); await interaction.deferReply();
const helpEmbed = new MessageEmbed() const helpEmbed = new MessageEmbed()

6
src/commands/score.ts

@ -1,5 +1,5 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js'; import { CommandInteraction, MessageEmbed } from 'discord.js';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
@ -16,7 +16,7 @@ export const data = new SlashCommandBuilder()
return option; return option;
}); });
export async function execute(interaction) { export async function execute(interaction : CommandInteraction) {
const scoreEmbed = new MessageEmbed() const scoreEmbed = new MessageEmbed()
.setColor('#ffffff'); .setColor('#ffffff');
@ -32,7 +32,7 @@ export async function execute(interaction) {
} }
scoreEmbed scoreEmbed
.setAuthor({ name: user.tag, iconURL: user.displayAvatarURL(true) }) .setAuthor({ name: user.tag, iconURL: user.displayAvatarURL() })
.setDescription(`Score: \`${score.score}\``); .setDescription(`Score: \`${score.score}\``);
await interaction.reply({ embeds: [scoreEmbed] }); await interaction.reply({ embeds: [scoreEmbed] });

12
src/commands/top.ts

@ -1,5 +1,5 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js'; import { CommandInteraction, MessageEmbed } from 'discord.js';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
@ -8,7 +8,7 @@ export const data = new SlashCommandBuilder()
.setName('top') .setName('top')
.setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'); .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(); await interaction.deferReply();
userScore userScore
@ -27,7 +27,7 @@ export async function execute(interaction) {
); );
} }
const embeds : unknown[] = []; const embeds : MessageEmbed[] = [];
let lbMessageContent = ''; let lbMessageContent = '';
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
@ -42,16 +42,16 @@ export async function execute(interaction) {
embeds.push(leaderboardEmbed); embeds.push(leaderboardEmbed);
let sMessageContent = ''; 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) { if (serverLeaderBoardArray.length > 10) {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`; sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`;
} }
const sLeaderboardEmbed = new MessageEmbed() const sLeaderboardEmbed = new MessageEmbed()
.setTitle(`Top Ten in ${interaction.guild.name}!`) .setTitle(`Top Ten in ${interaction.guild?.name}!`)
.setDescription(sMessageContent) .setDescription(sMessageContent)
.setColor('#ffffff'); .setColor('#ffffff');

33
src/commands/train.ts

@ -1,5 +1,5 @@
import { SlashCommandBuilder } from '@discordjs/builders'; 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 { decode } from 'html-entities';
import axios from 'axios'; import axios from 'axios';
@ -28,10 +28,10 @@ export const data = new SlashCommandBuilder()
return option; return option;
}); });
export async function execute(interaction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); 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; const authorId = interaction.user.id;
let score; let score;
userScore userScore
@ -83,11 +83,11 @@ export async function execute(interaction) {
categoryArray = ['ENERGY']; categoryArray = ['ENERGY'];
break; break;
default: default:
interaction.followUp( interaction.followUp({
new MessageEmbed() embeds: [new MessageEmbed()
.setDescription('<:red_x:816791117671825409> Not a valid subject!') .setDescription('<:red_x:816791117671825409> Not a valid subject!')
.setColor('#ffffff'), .setColor('#ffffff')],
); });
return; 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[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 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 }) interaction.followUp({ content: decode(tossupQuestion), fetchReply: true })
.then(questionMessage => { .then(q => {
const questionMessage = q as Message;
const sourceButton = new MessageActionRow() const sourceButton = new MessageActionRow()
.addComponents( .addComponents(
new MessageButton() new MessageButton()
@ -115,18 +116,18 @@ export async function execute(interaction) {
switch (tossupFormat) { switch (tossupFormat) {
case 'Short Answer': { case 'Short Answer': {
// eslint-disable-next-line no-case-declarations // eslint-disable-next-line no-case-declarations
const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user.id; const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user?.id;
interaction.channel.awaitMessages({ interaction.channel?.awaitMessages({
filter: messageFilter, filter: messageFilter,
max: 1, max: 1,
}) })
.then(collected => { .then(collected => {
const answerMsg = collected.first(); const answerMsg = collected.first();
if (answerMsg.author.id === interaction.client.user.id) return; if (answerMsg?.author.id === interaction.client.user?.id) return;
let predicted = ''; 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'; predicted = 'correct';
} }
else { else {
@ -135,12 +136,12 @@ export async function execute(interaction) {
if (predicted === 'correct') { if (predicted === 'correct') {
updateScore(true, score, authorId).then((msgToReply) => updateScore(true, score, authorId).then((msgToReply) =>
answerMsg.reply(msgToReply), answerMsg?.reply(msgToReply),
); );
} }
else { else {
const overrideEmbed = new MessageEmbed() 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}\``) .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.') .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') .setColor('#ffffff')
@ -152,7 +153,7 @@ export async function execute(interaction) {
.setEmoji('<:override:955265585086857236>') .setEmoji('<:override:955265585086857236>')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
); );
answerMsg.channel.send({ answerMsg?.channel.send({
embeds: [overrideEmbed], embeds: [overrideEmbed],
components: [overrideButton], components: [overrideButton],
}) })

Loading…
Cancel
Save