From c15aa8ed26edb979dca265b92deb1137709c92d8 Mon Sep 17 00:00:00 2001 From: Papaia <43409674+ItsPapaia@users.noreply.github.com> Date: Fri, 10 Jul 2020 11:54:57 +0300 Subject: [PATCH 1/4] chore: RadioInfoKpop extends RadioInfo --- src/bot/client/ListenClient.ts | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/bot/client/ListenClient.ts b/src/bot/client/ListenClient.ts index 279863e..409319e 100644 --- a/src/bot/client/ListenClient.ts +++ b/src/bot/client/ListenClient.ts @@ -41,20 +41,7 @@ interface RadioInfo { eventCover?: string; } -interface RadioInfoKpop { - songName: string; - artistName?: string; - artistList?: string; - artistCount: number; - sourceName: string; - albumName: string; - albumCover: string; - listeners: number; - requestedBy: string; - event: boolean; - eventName?: string; - eventCover?: string; -} +interface RadioInfoKpop extends RadioInfo {} export default class ListenClient extends AkairoClient { public logger = logger; From 49eefc9cf8145ad2e53aa79378b07a2bb3a7b7ff Mon Sep 17 00:00:00 2001 From: Papaia <43409674+ItsPapaia@users.noreply.github.com> Date: Fri, 10 Jul 2020 12:01:49 +0300 Subject: [PATCH 2/4] chore: move interfaces to types file --- src/bot/client/ListenClient.ts | 18 +----------------- src/types/RadioInfo.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 src/types/RadioInfo.ts diff --git a/src/bot/client/ListenClient.ts b/src/bot/client/ListenClient.ts index 409319e..ea2bfcb 100644 --- a/src/bot/client/ListenClient.ts +++ b/src/bot/client/ListenClient.ts @@ -7,6 +7,7 @@ import database from '../structures/Database'; import { Setting } from '../models/Settings'; import TypeORMProvider from '../structures/SettingsProvider'; import WebSocketManager from '../structures/WebSocketManager'; +import { RadioInfo, RadioInfoKpop } from '../../types/RadioInfo'; declare module 'discord-akairo' { interface AkairoClient { @@ -26,23 +27,6 @@ interface ListenOptions { token?: string; } -interface RadioInfo { - songName: string; - artistName?: string; - artistList?: string; - artistCount: number; - sourceName: string; - albumName: string; - albumCover: string; - listeners: number; - requestedBy: string; - event: boolean; - eventName?: string; - eventCover?: string; -} - -interface RadioInfoKpop extends RadioInfo {} - export default class ListenClient extends AkairoClient { public logger = logger; diff --git a/src/types/RadioInfo.ts b/src/types/RadioInfo.ts new file mode 100644 index 0000000..7c3cc8a --- /dev/null +++ b/src/types/RadioInfo.ts @@ -0,0 +1,16 @@ +export interface RadioInfo { + songName: string; + artistName?: string; + artistList?: string; + artistCount: number; + sourceName: string; + albumName: string; + albumCover: string; + listeners: number; + requestedBy: string; + event: boolean; + eventName?: string; + eventCover?: string; +} + +export interface RadioInfoKpop extends RadioInfo {} From b5036afe822b4aa2fdc4d384d79ca839c4e69a24 Mon Sep 17 00:00:00 2001 From: Papaia <43409674+ItsPapaia@users.noreply.github.com> Date: Fri, 10 Jul 2020 12:09:49 +0300 Subject: [PATCH 3/4] chore: move formatting to utilities --- src/bot/commands/listen/np.ts | 26 +++------------------- src/bot/commands/listen/npk.ts | 26 +++------------------- src/util/formatRadioInfo.ts | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 src/util/formatRadioInfo.ts diff --git a/src/bot/commands/listen/np.ts b/src/bot/commands/listen/np.ts index 913e367..977dbb0 100644 --- a/src/bot/commands/listen/np.ts +++ b/src/bot/commands/listen/np.ts @@ -1,5 +1,6 @@ import { Command } from 'discord-akairo'; -import { Message, MessageEmbed, Util } from 'discord.js'; +import { Message } from 'discord.js'; +import { formatRadioInfo } from '../../../util/formatRadioInfo'; export default class NowPlayingCommand extends Command { public constructor() { @@ -15,28 +16,7 @@ export default class NowPlayingCommand extends Command { } public async exec(message: Message) { - const { radioInfo } = this.client; - const name = `**Name**: ${Util.escapeMarkdown(radioInfo.songName)}`; - const artists = `${radioInfo.artistCount > 1 ? '**Artists**' : '**Artist**'}: ${Util.escapeMarkdown( - radioInfo.artistList ?? '', - )}`; - const anime = radioInfo.sourceName ? `**Source**: ${Util.escapeMarkdown(radioInfo.sourceName)}` : ''; - const album = radioInfo.albumName ? `**Album**: ${Util.escapeMarkdown(radioInfo.albumName)}` : ''; - const requestedBy = radioInfo.event - ? `🎉 **${Util.escapeMarkdown(radioInfo.eventName ?? '')}** 🎉` - : radioInfo.requestedBy - ? `Requested by: ${Util.escapeMarkdown(radioInfo.requestedBy)}` - : ''; - const ifAlbum = radioInfo.albumName ? '\n' : ''; - const ifAnime = radioInfo.sourceName ? '\n' : ''; - const ifRequest = requestedBy ? '\n\n' : ''; - const song = `${name}\n${artists}${ifAlbum}${album}${ifAnime}${anime}${ifRequest}${requestedBy}`; - const cover = radioInfo.event ? radioInfo.eventCover : radioInfo.albumCover; - - const embed = new MessageEmbed() - .setColor(15473237) - .addField('❯ Now playing', song) - .setThumbnail(cover ?? ''); + const embed = formatRadioInfo(this.client.radioInfo); return message.util!.send(embed); } diff --git a/src/bot/commands/listen/npk.ts b/src/bot/commands/listen/npk.ts index 74a90fb..60d941c 100644 --- a/src/bot/commands/listen/npk.ts +++ b/src/bot/commands/listen/npk.ts @@ -1,5 +1,6 @@ import { Command } from 'discord-akairo'; -import { Message, MessageEmbed, Util } from 'discord.js'; +import { Message } from 'discord.js'; +import { formatRadioInfoKpop } from '../../../util/formatRadioInfo'; export default class NowPlayingKPOPCommand extends Command { public constructor() { @@ -15,28 +16,7 @@ export default class NowPlayingKPOPCommand extends Command { } public async exec(message: Message) { - const { radioInfoKpop } = this.client; - const name = `**Name**: ${Util.escapeMarkdown(radioInfoKpop.songName)}`; - const artists = `${radioInfoKpop.artistCount > 1 ? '**Artists**' : '**Artist**'}: ${Util.escapeMarkdown( - radioInfoKpop.artistList ?? '', - )}`; - const anime = radioInfoKpop.sourceName ? `**Source**: ${Util.escapeMarkdown(radioInfoKpop.sourceName)}` : ''; - const album = radioInfoKpop.albumName ? `**Album**: ${Util.escapeMarkdown(radioInfoKpop.albumName)}` : ''; - const requestedBy = radioInfoKpop.event - ? `🎉 **${Util.escapeMarkdown(radioInfoKpop.eventName ?? '')}** 🎉` - : radioInfoKpop.requestedBy - ? `Requested by: ${Util.escapeMarkdown(radioInfoKpop.requestedBy)}` - : ''; - const ifAlbum = radioInfoKpop.albumName ? '\n' : ''; - const ifAnime = radioInfoKpop.sourceName ? '\n' : ''; - const ifRequest = requestedBy ? '\n\n' : ''; - const song = `${name}\n${artists}${ifAlbum}${album}${ifAnime}${anime}${ifRequest}${requestedBy}`; - const cover = radioInfoKpop.event ? radioInfoKpop.eventCover : radioInfoKpop.albumCover; - - const embed = new MessageEmbed() - .setColor(3189229) - .addField('❯ Now playing', song) - .setThumbnail(cover ?? ''); + const embed = formatRadioInfoKpop(this.client.radioInfoKpop); return message.util!.send(embed); } diff --git a/src/util/formatRadioInfo.ts b/src/util/formatRadioInfo.ts new file mode 100644 index 0000000..aa4bf0b --- /dev/null +++ b/src/util/formatRadioInfo.ts @@ -0,0 +1,40 @@ +import { RadioInfo, RadioInfoKpop } from '../types/RadioInfo'; +import { MessageEmbed, Util } from 'discord.js'; + +const format = (info: RadioInfo | RadioInfoKpop): { cover?: string; song: string } => { + const name = `**Name**: ${Util.escapeMarkdown(info.songName)}`; + const artists = `${info.artistCount > 1 ? '**Artists**' : '**Artist**'}: ${Util.escapeMarkdown( + info.artistList ?? '', + )}`; + const anime = info.sourceName ? `**Source**: ${Util.escapeMarkdown(info.sourceName)}` : ''; + const album = info.albumName ? `**Album**: ${Util.escapeMarkdown(info.albumName)}` : ''; + const requestedBy = info.event + ? `🎉 **${Util.escapeMarkdown(info.eventName ?? '')}** 🎉` + : info.requestedBy + ? `Requested by: ${Util.escapeMarkdown(info.requestedBy)}` + : ''; + const ifAlbum = info.albumName ? '\n' : ''; + const ifAnime = info.sourceName ? '\n' : ''; + const ifRequest = requestedBy ? '\n\n' : ''; + const song = `${name}\n${artists}${ifAlbum}${album}${ifAnime}${anime}${ifRequest}${requestedBy}`; + const cover = info.event ? info.eventCover : info.albumCover; + return { cover, song }; +}; + +export function formatRadioInfo(info: RadioInfo): MessageEmbed { + const { song, cover } = format(info); + + return new MessageEmbed() + .setColor(15473237) + .addField('❯ Now playing', song) + .setThumbnail(cover ?? ''); +} + +export function formatRadioInfoKpop(info: RadioInfoKpop): MessageEmbed { + const { song, cover } = format(info); + + return new MessageEmbed() + .setColor(3189229) + .addField('❯ Now playing', song) + .setThumbnail(cover ?? ''); +} From 3099c956dd730289dfc061d84b076e2920b8daca Mon Sep 17 00:00:00 2001 From: Papaia <43409674+ItsPapaia@users.noreply.github.com> Date: Fri, 10 Jul 2020 12:20:45 +0300 Subject: [PATCH 4/4] chore: avoid repeating snippet in ws manager --- src/bot/structures/WebSocketManager.ts | 46 ++++++++++---------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/bot/structures/WebSocketManager.ts b/src/bot/structures/WebSocketManager.ts index 8baa017..4226e9a 100644 --- a/src/bot/structures/WebSocketManager.ts +++ b/src/bot/structures/WebSocketManager.ts @@ -1,5 +1,6 @@ import * as WebSocket from 'ws'; import ListenClient from '../client/ListenClient'; +import { RadioInfo, RadioInfoKpop } from '../../types/RadioInfo'; export default class WebSocketManager { private ws: WebSocket | null = null; @@ -101,36 +102,25 @@ export default class WebSocketManager { eventCover = response.d.event.image; } + const info: RadioInfo | RadioInfoKpop = { + songName: response.d.song.title, + artistName: artist, + artistList: artists, + artistCount: response.d.song.artists.length, + sourceName: source, + albumName: album, + albumCover: cover, + listeners: response.d.listeners, + requestedBy: requester, + event, + eventName, + eventCover, + }; + if (this.type === 'kpop') { - this.client.radioInfoKpop = { - songName: response.d.song.title, - artistName: artist, - artistList: artists, - artistCount: response.d.song.artists.length, - sourceName: source, - albumName: album, - albumCover: cover, - listeners: response.d.listeners, - requestedBy: requester, - event, - eventName, - eventCover, - }; + this.client.radioInfoKpop = info; } else { - this.client.radioInfo = { - songName: response.d.song.title, - artistName: artist, - artistList: artists, - artistCount: response.d.song.artists.length, - sourceName: source, - albumName: album, - albumCover: cover, - listeners: response.d.listeners, - requestedBy: requester, - event, - eventName, - eventCover, - }; + this.client.radioInfo = info; } this.currentSongGame();