Skip to content

Commit

Permalink
Merge pull request #207 from RikyTales/ask_command_fixes
Browse files Browse the repository at this point in the history
Remove prefix ask command and format output
  • Loading branch information
zuzuvelas authored Apr 26, 2024
2 parents 6d37d64 + 490a15a commit b41101d
Showing 1 changed file with 15 additions and 46 deletions.
61 changes: 15 additions & 46 deletions src/commands/ask.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ApplicationCommandOptionType, CommandInteraction, InteractionResponse, escapeMarkdown } from 'discord.js'
import {
Discord,
SimpleCommand,
SimpleCommandMessage,
SimpleCommandOption,
SimpleCommandOptionType,
Slash,
SlashChoice,
SlashOption,
} from 'discordx'
ApplicationCommandOptionType,
CommandInteraction,
EmbedBuilder,
InteractionResponse,
escapeMarkdown,
} from 'discord.js'
import { Discord, Slash, SlashChoice, SlashOption } from 'discordx'

const COMMAND_NAME = 'ask'
const COMMAND_DESC = 'Ask a question to Wolfram Alpha'
Expand All @@ -29,37 +26,6 @@ class Ask {
}
}

@SimpleCommand({ name: COMMAND_NAME, description: COMMAND_DESC, argSplitter: '\n' })
async simple(
@SimpleCommandOption({ name: 'question', type: SimpleCommandOptionType.String })
question: string | undefined,
@SimpleCommandOption({ name: 'units of measurement', type: SimpleCommandOptionType.Boolean })
units: string | undefined,
command: SimpleCommandMessage
) {
if (!question) {
return await command.message.reply({
content: 'Usage: >ask <question>\n<units?> (metric or imperial)',
allowedMentions: { repliedUser: false },
})
}

if (this.isOnCooldown()) {
return
}

try {
const answer = await this.fetchAnswer(question, units)
return command.message.reply({ content: answer, allowedMentions: { repliedUser: false } })
} catch (err) {
console.error(err)
return command.message.reply({
content: 'There was a problem communicating with Wolfram Alpha.',
allowedMentions: { repliedUser: false },
})
}
}

@Slash({ name: COMMAND_NAME, description: COMMAND_DESC })
private async slash(
@SlashOption({
Expand Down Expand Up @@ -87,10 +53,16 @@ class Ask {

try {
const answer = await this.fetchAnswer(question, units)
return interaction.reply(`[${escapeMarkdown(question)}] ${answer}`)
const capitalizedAnswer = answer.charAt(0).toUpperCase() + answer.slice(1)
const embed = new EmbedBuilder()
.setColor('#FBAB00') // MasterMind's color
.setTitle(escapeMarkdown(question))
.setDescription(capitalizedAnswer)

return await interaction.reply({ embeds: [embed] })
} catch (err) {
console.error(err)
return interaction.reply('There was a problem communicating with Wolfram Alpha.')
return interaction.reply({ content: 'There was a problem communicating with Wolfram Alpha.', ephemeral: true })
}
}

Expand All @@ -104,10 +76,7 @@ class Ask {
url.searchParams.append('i', question)
url.searchParams.append('units', units.toLowerCase())

console.log(url)

const response = await fetch(url)

if (response.ok) {
return escapeMarkdown(await response.text())
}
Expand Down

0 comments on commit b41101d

Please sign in to comment.