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

[5.5.0] History command #199

Merged
merged 9 commits into from
Jan 12, 2024
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
64 changes: 64 additions & 0 deletions djs-bot/commands/music/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const colors = require("colors");
const { EmbedBuilder } = require("discord.js");
const SlashCommand = require("../../lib/SlashCommand");
const { historyEmbed } = require("../../util/embeds");
const { deleteMessageDelay } = require("../../util/message");

const command = new SlashCommand()
.setName("history")
.setDescription("Keep song history in chat (toggle)")
.setRun(async (client, interaction) => {
let channel = await client.getChannel(client, interaction);
if (!channel) {
return;
}

let player;
if (client.manager.Engine) {
player = client.manager.Engine.players.get(interaction.guild.id);
} else {
return interaction.reply({
embeds: [
new EmbedBuilder()
.setColor("Red")
.setDescription("Lavalink node is not connected"),
],
});
}

if (!player) {
return interaction.reply({
embeds: [
new EmbedBuilder()
.setColor("Red")
.setDescription("There's nothing playing in the queue"),
],
ephemeral: true,
});
}

const history = player.get("history");
player.set("requester", interaction.guild.members.me);

if (!history) {
player.set("history", true);
} else {
player.set("history", false);
}

client.warn(
`Player: ${ player.options.guild } | [${ colors.blue(
"history",
) }] has been [${ colors.blue(!history? "ENABLED" : "DISABLED") }] in ${
client.guilds.cache.get(player.options.guild)
? client.guilds.cache.get(player.options.guild).name
: "a guild"
}`,
);

const ret = await interaction.reply({ embeds: [historyEmbed({history})], fetchReply: true });
deleteMessageDelay(ret);
return ret;
});

module.exports = command;
2 changes: 2 additions & 0 deletions djs-bot/commands/music/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const command = new SlashCommand()
}

if (res.loadType === "TRACK_LOADED" || res.loadType === "SEARCH_RESULT") {
player.set("requester", interaction.guild.members.me);
addTrack(player, res.tracks[0]);

if (!player.playing && !player.paused && !player.queue.size) {
Expand All @@ -144,6 +145,7 @@ const command = new SlashCommand()
}

if (res.loadType === "PLAYLIST_LOADED") {
player.set("requester", interaction.guild.members.me);
addTrack(player, res.tracks);

if (
Expand Down
11 changes: 11 additions & 0 deletions djs-bot/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = {
* Music engine to use
* @type {keyof typeof import("./lib/clients/MusicClient").Engine} */
musicEngine: "Erela",

/**
* Nodes to connect to
* @type {import("erela.js").Node[]} */
Expand Down Expand Up @@ -121,6 +122,7 @@ module.exports = {
* 0 = No debug logging (production), 1 = Standard Logging (debug info), 2 = Development (everything)
* @type {number} */
OPLevel: 1,

/**
* Color of the embeds (can also be hex)
* @type {import('discord.js').ColorResolvable} */
Expand Down Expand Up @@ -158,8 +160,17 @@ module.exports = {
}
],
},

/**
* This icon will be in every embed's author field, if you don't want it, just leave it blank or "undefined"
* @type {string} */
iconURL: undefined,

defaultPlayerValues: {
twentyFourSeven: false,
autoLeave: false,
autoPause: true,
autoQueue: false,
history: false,
}
};
26 changes: 25 additions & 1 deletion djs-bot/lib/MusicEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
const colors = require("colors");
const { getClient } = require("../bot");
const socket = require("../api/v1/dist/ws/eventsHandler");
const { updateControlMessage, updateNowPlaying } = require("../util/controlChannel");
const {
updateControlMessage,
updateNowPlaying,
runIfNotControlChannel,
} = require("../util/controlChannel");
const { trackStartedEmbed } = require("../util/embeds");

// entries in this map should be removed when bot disconnected from vc
const progressUpdater = new Map();
Expand Down Expand Up @@ -59,6 +64,24 @@ function handleQueueUpdate({ guildId, player }) {
socket.handleQueueUpdate({ guildId, player });
}

function sendTrackHistory({ player, track }) {
const history = player.get("history");
if (!history) return;

runIfNotControlChannel(player, () => {
const client = getClient();

client.channels.cache
.get(player.textChannel)
?.send({
embeds: [
trackStartedEmbed({ track, player, title: "Played track" }),
],
})
.catch(client.warn);
});
}

/**
* @param {import("./MusicEvents").IHandleTrackStartParams}
*/
Expand All @@ -73,6 +96,7 @@ function handleTrackStart({ player, track }) {

updateNowPlaying(player, track);
updateControlMessage(player.guild, track);
sendTrackHistory({ player, track });

socket.handleTrackStart({ player, track });
socket.handlePause({ guildId: player.guild, state: player.paused });
Expand Down
5 changes: 3 additions & 2 deletions djs-bot/lib/clients/Cosmicord.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { Cosmicord, CosmiPlayer, CosmiNode } = require("cosmicord.js");
const { updateControlMessage } = require("../../util/controlChannel");
const { handleTrackStart } = require("../MusicEvents");
const { pause } = require("../../util/player");
const { setDefaultPlayerConfig } = require("../../util/musicManager");

class CosmicordPlayerExtended extends CosmiPlayer {
/**
Expand All @@ -20,8 +21,8 @@ class CosmicordPlayerExtended extends CosmiPlayer {

/** @type {CosmicordExtended} */
this.cosmicord = cosmicordExtended;
/** @type {boolean} */
this.twentyFourSeven = false;

setDefaultPlayerConfig(this);
}

/** The guild id of the player */
Expand Down
4 changes: 3 additions & 1 deletion djs-bot/lib/clients/Erela.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { default: AppleMusic } = require("better-erela.js-apple"); // <---
const { updateControlMessage } = require("../../util/controlChannel");
const { handleTrackStart } = require("../MusicEvents");
const { pause } = require("../../util/player");
const { setDefaultPlayerConfig } = require("../../util/musicManager");

Structure.extend(
"Player",
Expand All @@ -21,7 +22,8 @@ Structure.extend(
/** @returns {import("erela.js").Player} */
constructor(...props) {
super(...props);
this.twentyFourSeven = false;

setDefaultPlayerConfig(this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions djs-bot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion djs-bot/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"description": "",
"main": "index.js",
"version": "5.4.3",
"version": "5.5.0",
"name": "discord-musicbot",
"scripts": {
"guild": "npm run api-build && node scripts/guild",
Expand Down
21 changes: 18 additions & 3 deletions djs-bot/util/embeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,23 @@ const embedClearedQueue = () =>
/**
* @typedef {object} TrackStartedEmbedParams
* @property {import("cosmicord.js").CosmiTrack=} track
* @property {import("../lib/clients/MusicClient").CosmicordPlayerExtended} player
* @property {string=} title
*
* @param {TrackStartedEmbedParams}
*/
const trackStartedEmbed = ({ track, player } = {}) => {
const trackStartedEmbed = ({ track, player, title = 'Now playing' } = {}) => {
const client = getClient();

const embed = new EmbedBuilder().setColor(client.config.embedColor);

if (track) {
embed.setAuthor({ name: "Now playing", iconURL: client.config.iconURL })
embed.setAuthor({ name: title, iconURL: client.config.iconURL })
.setDescription(`[${track.title}](${track.uri})`)
.addFields([
{
name: "Requested by",
value: `${track.requester}`,
value: `${track.requester ?? 'ʕ•ᴥ•ʔ'}`,
inline: true,
},
{
Expand Down Expand Up @@ -281,6 +283,18 @@ const autoQueueEmbed = ({ autoQueue }) => {
});
};

const historyEmbed = ({ history }) => {
const client = getClient();
return new EmbedBuilder()
.setColor(client.config.embedColor)
.setDescription(`**History is** \`${!history ? "ON" : "OFF"}\``)
.setFooter({
text: `Music history will ${
!history ? "no longer be" : "now be automatically"
} removed.`,
});
};

/**
* @param {import("../lib/clients/MusicClient").CosmicordPlayerExtended} player
* @param {EmbedBuilder} embed
Expand Down Expand Up @@ -328,6 +342,7 @@ module.exports = {
addQueueEmbed,
loadedPlaylistEmbed,
autoQueueEmbed,
historyEmbed,
addPlayerStateFooter,
getButtons,
embedNotEnoughSong,
Expand Down
3 changes: 3 additions & 0 deletions djs-bot/util/message.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Message } from "discord.js";

export function deleteMessageDelay(message: Message, delay?: number): void;
13 changes: 13 additions & 0 deletions djs-bot/util/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

const { getClient } = require("../bot");

const deleteMessageDelay = (message, delay = 20000) => {
if (!message) return;

setTimeout(() => message.delete().catch(getClient().warn), delay);
};

module.exports = {
deleteMessageDelay,
};
20 changes: 20 additions & 0 deletions djs-bot/util/musicManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const { getClient } = require("../bot");

const setDefaultPlayerConfig = (instance) => {
const config = getClient().config;
const defaultValues = config.defaultPlayerValues;

if (typeof defaultValues !== "object") return;

const defaultKeys = Object.keys(config.defaultPlayerValues);

defaultKeys.forEach((key) => {
instance.set(key, defaultValues[key]);
});
};

module.exports = {
setDefaultPlayerConfig,
};