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

Update vibe command to use new Button API #19

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/commands/cat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from '@knighthacks/dispatch';
import axios from 'axios';
import { CommandInteraction, InteractionReplyOptions, Message, MessageEmbed } from 'discord.js';
import { InteractionReplyOptions, Message, MessageEmbed } from 'discord.js';
import Colors from '../colors';
import { singleButtonRow } from '../util/button';

Expand Down Expand Up @@ -39,7 +39,7 @@ async function getMessage(): Promise<InteractionReplyOptions> {
const CatCommand: Command = {
name: 'cat',
description: 'Gets a random image of a cat',
async run(interaction: CommandInteraction) {
async run({ interaction }) {
// Defer while we fetch the image.
await interaction.defer();

Expand Down
4 changes: 2 additions & 2 deletions src/commands/coinflip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandInteraction, Message } from 'discord.js';
import { Message } from 'discord.js';
import { Command, inChannelNames } from '@knighthacks/dispatch';
import { Channels } from '../channels';
import { singleButtonRow } from '../util/button';
Expand All @@ -18,7 +18,7 @@ const CoinFlipCommand: Command = {
name: 'coinflip',
description: 'Performs a coin flip',
permissionHandler: inChannelNames(Channels.bot),
async run(interaction: CommandInteraction): Promise<void> {
async run({ interaction }): Promise<void> {
const message = await interaction.reply({
content: `${interaction.user.username}, you got ${getFlip()}`,
fetchReply: true,
Expand Down
37 changes: 37 additions & 0 deletions src/commands/crumbl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Command } from '@knighthacks/dispatch';
import axios from 'axios';
import { MessageEmbed } from 'discord.js';

interface Cookie {
name: string;
description: string;
image: string;
}

async function fetchCookiesData(): Promise<Cookie[]> {
return (
await axios.get(
'https://jpswqfm3od.execute-api.us-east-1.amazonaws.com/default/crumbl-api'
)
).data;
}

const crumbl: Command = {
name: 'crumbl',
description: 'View the current weekly specialty cookies at Crumbl Cookies!',
async run({ interaction }) {
await interaction.defer();
const cookiesData = await fetchCookiesData();
await interaction.followUp({
content: 'Here are the weekly specialty cookies!',
embeds: cookiesData.map((c) =>
new MessageEmbed()
.setTitle(c.name)
.setDescription(c.description)
.setThumbnail(c.image)
),
});
},
};

export default crumbl;
4 changes: 2 additions & 2 deletions src/commands/dice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandInteraction, Message, MessageActionRow, MessageButton } from 'discord.js';
import { Message, MessageActionRow, MessageButton } from 'discord.js';
import { Command } from '@knighthacks/dispatch';

const numberToString: Record<number, string> = {
Expand All @@ -22,7 +22,7 @@ const randNumber = () => Math.floor(Math.random() * 6) + 1; // +1 to exclude 0 a
const DiceCommand: Command = {
name: 'dice',
description: 'Roll a die to get a random number between 1 and 6',
async run(interaction: CommandInteraction) {
async run({ interaction }) {
// Send message.
const message = await interaction.reply({
content: `You rolled a :${numberToString[randNumber()]}:`,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/dog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from '@knighthacks/dispatch';
import axios from 'axios';
import { CommandInteraction, InteractionReplyOptions, Message, MessageEmbed } from 'discord.js';
import { InteractionReplyOptions, Message, MessageEmbed } from 'discord.js';
import Colors from '../colors';
import { singleButtonRow } from '../util/button';

Expand Down Expand Up @@ -39,7 +39,7 @@ async function getMessage(): Promise<InteractionReplyOptions> {
const DogCommand: Command = {
name: 'dog',
description: 'Downloads an image of a dog from the internet',
async run(interaction: CommandInteraction) {
async run({ interaction }) {

// Defer interaction while we fetch the image.
await interaction.defer();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/eightball.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from '@knighthacks/dispatch';
import { ApplicationCommandOption, CommandInteraction } from 'discord.js';
import { ApplicationCommandOption } from 'discord.js';

const options: ApplicationCommandOption[] = [
{
Expand Down Expand Up @@ -27,7 +27,7 @@ const eightball: Command = {
name: 'eightball',
description : 'Ask a question, and you shall recieve an answer',
options,
async run(interaction: CommandInteraction) {
async run({ interaction }) {
const randIndex = Math.floor(Math.random() * responses.length);
await interaction.reply(responses[randIndex] ?? '');
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/fact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from '@knighthacks/dispatch';
import axios from 'axios';
import { CommandInteraction, Message } from 'discord.js';
import { Message } from 'discord.js';
import { singleButtonRow } from '../util/button';

const url = 'https://uselessfacts.jsph.pl/random.json?language=en';
Expand Down Expand Up @@ -28,7 +28,7 @@ async function getFact(): Promise<string | null> {
const FactCommand: Command = {
name: 'fact',
description: 'Get a random fact',
async run(interaction: CommandInteraction) {
async run({ interaction }) {
const fact = await getFact();

if (fact) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const row = new MessageActionRow().addComponents(buttons);
const LinksCommand: Command = {
name: 'links',
description: 'Gets helpful links for KnightHacks.',
async run(interaction) {
async run({ interaction }) {
await interaction.reply({ content: '**Here\'s some helpful KnightHacks links!**', components: [row] });
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { CommandInteraction } from 'discord.js';
import { Command, inChannelNames } from '@knighthacks/dispatch';
import { Channels } from '../channels';

const PingCommand: Command = {
name: 'ping',
description: 'Displays bot ping latency',
permissionHandler: inChannelNames(Channels.bot),
async run(interaction: CommandInteraction) {
async run({ interaction }) {
await interaction.reply(`Pong (${interaction.client.ws.ping}ms)`);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/commands/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { MessageEmbed } from 'discord.js';
import { Command, inChannelNames } from '@knighthacks/dispatch';
import { Channels } from '../channels';
import Colors from '../colors';
Expand All @@ -7,7 +7,7 @@ const StatsCommand: Command = {
name: 'stats',
description: 'Displays statistics for this guild',
permissionHandler: inChannelNames(Channels.bot),
async run(interaction: CommandInteraction) {
async run({ interaction }) {

const members = interaction.guild?.members.cache;
const { guild } = interaction;
Expand Down
3 changes: 1 addition & 2 deletions src/commands/testCommand.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { CommandInteraction } from 'discord.js';
import { Command, inChannelNames } from '@knighthacks/dispatch';
import { Channels } from '../channels';

const command: Command = {
name: 'test',
description: 'a test command',
permissionHandler: inChannelNames(Channels.bot),
async run(interaction: CommandInteraction): Promise<void> {
async run({ interaction }): Promise<void> {
await interaction.reply('Hello from dispatch');
}
};
Expand Down
64 changes: 39 additions & 25 deletions src/commands/vibe.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { ApplicationCommandOption, CommandInteraction, EmbedFieldData, Message, MessageEmbed, User } from 'discord.js';
import { Command, inChannelNames } from '@knighthacks/dispatch';
import {
Command,
DispatchButton,
inChannelNames,
UIComponent,
} from '@knighthacks/dispatch';
import {
ApplicationCommandOption,
EmbedFieldData,
MessageEmbed,
User,
} from 'discord.js';
import { Channels } from '../channels';
import Colors from '../colors';
import { singleButtonRow } from '../util/button';

const options: ApplicationCommandOption[] = [
{
name: 'user',
type: 'USER',
description: 'The user to vibe check',
}
},
];

const row = singleButtonRow({
label: 'Recheck',
customId: 'vibeButton',
style: 'PRIMARY',
});

const categories = [
{ name: 'Royalty', emoji: '👑' },
{ name: 'Artsy', emoji: '👩‍🎨' },
Expand All @@ -39,9 +42,10 @@ type Result = {

function generateVibeEmbed(sender: User, recipient: User): MessageEmbed {
// Generate a random percentage per each category
const results: Result[] = categories.map(category => (
{ category, score: Math.floor(Math.random() * 101) }
));
const results: Result[] = categories.map((category) => ({
category,
score: Math.floor(Math.random() * 101),
}));

const fields = results.map((result): EmbedFieldData => {
// Dexponetiate the score.
Expand All @@ -62,7 +66,9 @@ function generateVibeEmbed(sender: User, recipient: User): MessageEmbed {
value += '|';
return { name, value };
});
const status = Math.round(Math.random()) ? '**✅ | Vibe Check Passed**' : '**❌ | Vibe Check failed**';
const status = Math.round(Math.random())
? '**✅ | Vibe Check Passed**'
: '**❌ | Vibe Check failed**';

// Create embed and add fields, and return.
return new MessageEmbed()
Expand All @@ -71,35 +77,43 @@ function generateVibeEmbed(sender: User, recipient: User): MessageEmbed {
.setTitle('Vibe Check')
.addFields(fields)
.setColor(Colors.embedColor)
.setFooter(`checked by ${sender.username}`, sender.avatarURL() ?? undefined);
.setFooter(
`checked by ${sender.username}`,
sender.avatarURL() ?? undefined
);
}

const VibeCommand: Command = {
name: 'vibe',
description: 'Performs a vibe check on the given user.',
options,
permissionHandler: inChannelNames(Channels.bot),
async run(interaction: CommandInteraction) {
async run({ interaction, registerUI }) {
const user = interaction.options.get('user')?.user;
const sender = interaction.user;

// Show that the bot is thinking.
await interaction.defer();

const ui: UIComponent = new DispatchButton({
style: 'PRIMARY',
label: 'Recheck',
async onClick(i) {
await i.deferUpdate();
await i.editReply({
embeds: [generateVibeEmbed(sender, user ?? sender)],
});
},
});

// If there's no user, that means it's just a sender check.
// Defer because the bot is thinking.
const message = await interaction.followUp({
await interaction.followUp({
embeds: [generateVibeEmbed(sender, user ?? sender)],
fetchReply: true,
components: [row],
}) as Message;

const collector = message.createMessageComponentCollector({ 'componentType': 'BUTTON' });
collector.on('collect', async (i) => {
await i.deferUpdate();
await i.editReply({ embeds: [generateVibeEmbed(sender, user ?? sender)] });
components: registerUI(ui),
});
}
},
};

export default VibeCommand;
4 changes: 2 additions & 2 deletions src/commands/whois.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationCommandOption, CommandInteraction, MessageEmbed } from 'discord.js';
import { ApplicationCommandOption, MessageEmbed } from 'discord.js';
import { Command } from '@knighthacks/dispatch';
import Colors from '../colors';

Expand All @@ -14,7 +14,7 @@ const WhoIs: Command = {
name: 'whois',
description: 'Displays info about a given user',
options,
async run(interaction: CommandInteraction) {
async run({ interaction }) {
const user = interaction.options.get('user')?.user ?? interaction.user;
await interaction.defer();
if(!user || !user.id)
Expand Down