Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prefix ask command and format output #207

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading