feat: spotify link cleaner, bye scam links

This commit is contained in:
2023-11-04 22:53:10 +01:00
parent 43c0b40703
commit 116f0be0b2
7 changed files with 78 additions and 6 deletions

2
dependencies.d.ts vendored
View File

@@ -1,9 +1,11 @@
import { SernEmitter, Logging, CoreModuleStore, ModuleManager, ErrorHandling, CoreDependencies, Singleton } from '@sern/handler'
import { Client } from 'discord.js'
import Spotify from 'spotify-api.js'
declare global {
interface Dependencies extends CoreDependencies {
'@sern/client': Singleton<Client>
'spotify-api-client': Singleton<Spotify.Client>
}
}

View File

@@ -0,0 +1,29 @@
import { Service, discordEvent } from '@sern/handler';
import removeURLParameter from '../util/removeUrlParameter.js';
export default discordEvent({
name: 'messageCreate',
async execute(message) {
const spotify = Service('spotify-api-client')
if (message.author.bot) return;
if (!message.content.includes('https://open.spotify.com/intl-es/track')) return;
const index = message.content.indexOf("https://open.spotify.com/intl-es/track");
let link: string
if (index !== -1) {
let endIndex = message.content.indexOf(" ", index);
if (endIndex === -1) {
endIndex = message.content.length;
}
link = message.content.substring(index, endIndex);
}
const croppedUrl = removeURLParameter(link.replace('intl-es/', '').replaceAll(/([^:]\/)\/+/g, "$1"), 'si');
const song = await spotify.tracks.get(croppedUrl.split('/').pop());
await message.delete();
message.channel.send({
content: `Oye <@${message.author.id}>, tu canción \`${song.name}\` de \`${song.artists.map(a => a.name).join(', ')}\` es muy buena, pero si quitas \`intl-es\` es mejor.\n${croppedUrl}`,
})
}
})

View File

@@ -8,7 +8,7 @@ import youtubenotifications from './util/youtubenotifications.js';
import { setIntervalAsync } from 'set-interval-async';
import birthdays from './util/birthdays.js';
import minecraftstatus from './util/minecraftstatus.js';
import axios from 'axios';
import Spotify from 'spotify-api.js';
// import giveawaychecker from './util/giveawaychecker.js';
let devMode: boolean
@@ -36,13 +36,19 @@ mongoose.connect(process.env.MONGODB!).then(() => {
console.log('Connected to MongoDB');
});
const spotifyClient = await Spotify.Client.create({
token: { clientID: process.env.SPOTIFY_CLIENT!, clientSecret: process.env.SPOTIFY_SECRET! },
})
interface MyDependencies extends Dependencies {
'@sern/client' : Singleton<Client>;
'@sern/logger' : Singleton<DefaultLogging>
}
await makeDependencies<MyDependencies>({
build: (root) => root.add({ '@sern/client': single(() => client) }),
build: (root) => root
.add({ '@sern/client': single(() => client) })
.add({ 'spotify-api-client': single(() => spotifyClient) })
});
Sern.init({
@@ -85,6 +91,6 @@ client.on('ready', async () => {
}
});
export const scamLinks = await axios.get('https://api.hyperphish.com/gimme-domains').then(res => res.data as Array<string>)
// export const scamLinks = await axios.get('https://api.hyperphish.com/gimme-domains').then(res => res.data as Array<string>)
client.login(process.env.TOKEN);

View File

@@ -54,6 +54,7 @@
"pretty-seconds-spanish": "^2.1.1",
"rockpaperscissors-checker": "^1.2.0",
"set-interval-async": "^3.0.2",
"spotify-api.js": "^9.2.5",
"stringify-safe": "^1.0.3",
"systeminformation": "^5.21.7"
},

View File

@@ -0,0 +1,20 @@
export default function removeURLParameter(url: string, parameter: string) {
//prefer to use l.search if you have a location/link object
var urlparts = url.split('?');
if (urlparts.length >= 2) {
var prefix = encodeURIComponent(parameter) + '=';
var pars = urlparts[1].split(/[&;]/g);
//reverse iteration as may be destructive
for (var i = pars.length; i-- > 0;) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}
return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');
}
return url;
}

View File

@@ -2405,6 +2405,19 @@ split@0.3:
dependencies:
through "2"
spotify-api.js@^9.2.5:
version "9.2.5"
resolved "https://registry.yarnpkg.com/spotify-api.js/-/spotify-api.js-9.2.5.tgz#c76ec8ca7dd9e677b26319785c2673657c322c28"
integrity sha512-Q+196XA6zUDvqQ5mMNgPN4l5j8vDF18DVqlwFnn0TX0MqLfOhrKfoeMR5OYNhpa99LVjEvwNF/WPaTAy0MqBJQ==
dependencies:
axios "^1.1.3"
spotify-types "^1.0.0"
spotify-types@^1.0.0:
version "1.0.7"
resolved "https://registry.yarnpkg.com/spotify-types/-/spotify-types-1.0.7.tgz#59c2e16da2ae18b8adb97e07a9e378d8567f4464"
integrity sha512-X2n0MYNUpgakGDU7h/vzTvqVJh2f8kAWE9SG5TkJtf3lJcorndK24/8/6+D7Uww8tqUD1VPHg5Lqv4usdVJUvg==
statuses@2.0.1:
version "2.0.1"
resolved "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz"
@@ -2462,9 +2475,10 @@ strtok3@^7.0.0:
"@tokenizer/token" "^0.3.0"
peek-readable "^5.0.0"
systeminformation@^5.12.6:
version "5.17.12"
resolved "https://registry.npmjs.org/systeminformation/-/systeminformation-5.17.12.tgz"
systeminformation@^5.21.7:
version "5.21.11"
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-5.21.11.tgz#c1a0b2f0f338903bb7bc1759db9fcd34b7312360"
integrity sha512-dIJEGoP5W7k4JJGje/b+inJrOL5hV9LPsUi5ndBvJydI80CVEcu2DZYgt6prdRErDi2SA4SqYd/WMR4b+u34mA==
tar@^6.1.11:
version "6.1.13"