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

feat(bot): context menu commands + prettier configuration #39

Merged
merged 2 commits into from
Jan 29, 2025
Merged
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
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);
}
};
5 changes: 4 additions & 1 deletion bot/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"outDir": "build/",
"strict": true
},
"include": ["src"]
"include": ["src"],
"ts-node": {
"files": true
}
}
Loading