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

chore: avoid repetitive code #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 1 addition & 30 deletions src/bot/client/ListenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,36 +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 {
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 default class ListenClient extends AkairoClient {
public logger = logger;

Expand Down
26 changes: 3 additions & 23 deletions src/bot/commands/listen/np.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
Expand Down
26 changes: 3 additions & 23 deletions src/bot/commands/listen/npk.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
Expand Down
46 changes: 18 additions & 28 deletions src/bot/structures/WebSocketManager.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/types/RadioInfo.ts
Original file line number Diff line number Diff line change
@@ -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 {}
40 changes: 40 additions & 0 deletions src/util/formatRadioInfo.ts
Original file line number Diff line number Diff line change
@@ -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 ?? '');
}