Skip to content

Commit

Permalink
feat(bot): context menu commands + prettier configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasVinicius314 committed Jan 24, 2025
1 parent fb1d97a commit b1b55cd
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "none"
}
68 changes: 24 additions & 44 deletions bot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
// @ts-nocheck
import 'dotenv/config';
import * as yup from 'yup';
import {
ActionRowBuilder,
ButtonBuilder,
ButtonInteraction,
Client,
GatewayIntentBits,
} from 'discord.js';
import { isYoutubeOrSpotify } from './utils/isYoutubeOrSpotify';
import { convertUrl } from './api/convertUrl';
import {
deleteAndReportHandler,
deleteHandler,
interactionButtons
} from './lib/interactions';
import { ButtonInteraction, Client, GatewayIntentBits, REST } from 'discord.js';
import { deleteAndReportHandler, deleteHandler } from './lib/interactions';
import { setupCommands } from './lib/rest';
import { commands } from './lib/commands';
import { processUrl } from './lib/core/processUrl';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
Expand All @@ -25,52 +15,42 @@ const client = new Client({

const token = process.env.TOKEN;

const rest = new REST({ version: '10' }).setToken(token);

console.log('Bot is starting...');
client.on('ready', () => {
console.log('Bot is running!');

setupCommands(rest, client.application!.id);

client.on('messageCreate', async (message) => {
try {
if (message.author.bot) return;

if (!message.guildId) return;

const url = message.content;

const isValid = yup
.object()
.shape({
url: yup.string().required().url()
})
.isValidSync({
url
});

if (!isValid) return;

const urlMatchResult = isYoutubeOrSpotify(url);

if (!urlMatchResult) return;
const res = await processUrl(message.content);

const convertedUrl = await convertUrl(url);
if (res === undefined) return;

const type = urlMatchResult.type === 'spotify' ? 'youtube' : 'spotify';
const adjective = type === 'youtube' ? 'plebs' : 'gentlemen';

const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
...interactionButtons
);

await message.reply({
content: `Here's a ${type} link for the ${adjective}:\n\n${convertedUrl}`,
components: [buttons]
});
await message.reply(res);
} catch (e) {
console.error(e);
}
});

client.on('interactionCreate', async (interaction) => {
if (interaction.isMessageContextMenuCommand()) {
if (interaction.commandName === commands.convertUrl.name) {
const res = await processUrl(interaction.targetMessage.content);

if (res === undefined) return;

await interaction.reply(res);
}

return;
}
if (!(interaction instanceof ButtonInteraction)) {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions bot/src/lib/commands/convertUrlContextCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js';

export const convertUrlContextCommand = new ContextMenuCommandBuilder()
.setName('Convert URL')
.setType(ApplicationCommandType.Message);
5 changes: 5 additions & 0 deletions bot/src/lib/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convertUrlContextCommand } from './convertUrlContextCommand';

export const commands = {
convertUrl: convertUrlContextCommand
};
36 changes: 36 additions & 0 deletions bot/src/lib/core/processUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as yup from 'yup';
import { isYoutubeOrSpotify } from '../../utils/isYoutubeOrSpotify';
import { convertUrl } from '../../api/convertUrl';
import { ActionRowBuilder, ButtonBuilder } from 'discord.js';
import { interactionButtons } from '../interactions';

export const processUrl = async (url: string) => {
const isValid = yup
.object()
.shape({
url: yup.string().required().url()
})
.isValidSync({
url
});

if (!isValid) return;

const urlMatchResult = isYoutubeOrSpotify(url);

if (!urlMatchResult) return;

const convertedUrl = await convertUrl(url);

const type = urlMatchResult.type === 'spotify' ? 'youtube' : 'spotify';
const adjective = type === 'youtube' ? 'plebs' : 'gentlemen';

const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
...interactionButtons
);

return {
content: `Here's a ${type} link for the ${adjective}:\n\n${convertedUrl}`,
components: [buttons]
};
};
16 changes: 16 additions & 0 deletions bot/src/lib/rest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { REST, Routes } from 'discord.js';
import { commands } from '../commands';

export const setupCommands = async (rest: REST, applicationId: string) => {
console.log('Started refreshing application (/) commands.');

try {
await rest.put(Routes.applicationCommands(applicationId), {
body: Object.values(commands)
});

console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
};

0 comments on commit b1b55cd

Please sign in to comment.