added: Vote-to-skip

This commit is contained in:
NoLogicAlan committed 2026-09-13 01:21:36 +03:00
1 parent e315cfbc1d
commit cc0b438ef6
13 files changed
+206 -40

No files matched your search

+13
View File
@@ -11,6 +11,7 @@ import runnables from "../../settings/runnables.mjs";
import {
parseBool, displayValue, getGuildName, embed, tWrap, prettifySettingLabel,
BOOL_SETTINGS, VALID_LOCALES, VOLUME_MIN, VOLUME_MAX, PREFIX_MAX, SHORTCUTS,
VOTE_SKIP_THRESHOLD_MIN, VOTE_SKIP_THRESHOLD_MAX,
} from "./utils.mjs";
import {
get247Channels, save247Channels, build247Panel, disable247, handle247Toggle,
@@ -44,6 +45,18 @@ export async function applySet(ctx, message, set, key, rawValue) {
return null;
}
if (key === "voteSkipThreshold") {
const num = parseInt(rawValue, 10);
if (isNaN(num) || num < VOTE_SKIP_THRESHOLD_MIN || num > VOTE_SKIP_THRESHOLD_MAX) {
return ctx.t(message, "responses.settings.voteSkipThresholdRange", {
min: VOTE_SKIP_THRESHOLD_MIN,
max: VOTE_SKIP_THRESHOLD_MAX,
});
}
set.set(key, num);
return null;
}
if (key === "locale") {
if (!VALID_LOCALES.has(rawValue)) {
return ctx.t(message, "responses.settings.invalidLocale", {
+11 -1
View File
@@ -18,6 +18,8 @@ export const SHORTCUTS = {
prefix: "prefix",
pfx: "prefix",
"247": "stay_247",
voteskip: "voteSkip",
voteskiplimit: "voteSkipThreshold",
};
/** @private @type {Set<string>} Strings that evaluate to boolean true. */
@@ -25,7 +27,7 @@ const BOOL_TRUE = new Set(["true", "1", "yes", "on", "enable", "enabled"]);
/** @private @type {Set<string>} Strings that evaluate to boolean false. */
const BOOL_FALSE = new Set(["false", "0", "no", "off", "disable", "disabled"]);
/** @private @type {Set<string>} Setting keys that use boolean display formatting. */
export const BOOL_SETTINGS = new Set(["songAnnouncements"]);
export const BOOL_SETTINGS = new Set(["songAnnouncements", "voteSkip"]);
/** @private @type {Set<string>} Available locale codes, populated at load time. */
export let VALID_LOCALES = new Set(["en"]);
@@ -38,6 +40,10 @@ export const VOLUME_MAX = 200;
export const PREFIX_MAX = 5;
/** @private @type {number} Maximum number of 24/7 channels per guild. */
export const MAX_247_CHANNELS = 1;
/** @private @type {number} Minimum allowed vote-skip threshold (people in channel). */
export const VOTE_SKIP_THRESHOLD_MIN = 2;
/** @private @type {number} Maximum allowed vote-skip threshold (people in channel). */
export const VOTE_SKIP_THRESHOLD_MAX = 50;
/**
* Replace the set of known locale codes (called by the settings command
@@ -132,6 +138,8 @@ export function prettifySettingLabel(key, t, guildId) {
locale: "responses.settings.labelLocale",
stay_247: "responses.settings.label247",
volume: "responses.settings.labelVolume",
voteSkip: "responses.settings.labelVoteSkip",
voteSkipThreshold: "responses.settings.labelVoteSkipThreshold",
};
if (localeMap[key] && t && guildId) return t(guildId, localeMap[key]);
const fallback = {
@@ -141,6 +149,8 @@ export function prettifySettingLabel(key, t, guildId) {
locale: "Locale",
stay_247: "24/7 mode",
volume: "Default volume",
voteSkip: "Vote to skip",
voteSkipThreshold: "Vote to skip threshold",
};
return fallback[key] ?? key.replace(/_/g, " ");
}
+70 -19
View File
@@ -1,11 +1,14 @@
/**
* @module commands/skip
* @description Skip the currently playing track.
* @description Skip the currently playing track. Solo or duo listeners skip
* instantly; once 3+ (configurable) people are in the voice channel, a
* majority vote is required — unless the server has disabled vote-skip.
*/
import { CommandBuilder } from "../src/commands/index.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../src/ui/index.mjs";
import { getGlobalColor, cleanId } from "../src/ui/index.mjs";
import { countHumansInChannel } from "../src/voice/VoiceStateResolver.mjs";
/** @type {CommandBuilder} @description Command definition for the skip command. */
export const command = new CommandBuilder()
@@ -14,17 +17,8 @@ export const command = new CommandBuilder()
.addAliases("s")
.setCategory("music");
/**
* @async
* Run handler for the skip command.
* Skips the current track and shows which track was skipped.
* @param {object} message - The command message wrapper.
* @returns {Promise<void>}
*/
export async function run(message) {
const p = await this.getPlayer(message);
if (!p) return;
/** @private Perform the actual skip and reply with what was skipped. @param {object} ctx @param {object} message @param {Player} p @returns {Promise<void>} */
async function doSkip(ctx, message, p) {
const current = p.queue.getCurrent();
const skippedTitle = current?.title ?? null;
const skippedLink = current ? (current.spotifyUrl || current.url || "") : "";
@@ -36,12 +30,69 @@ export async function run(message) {
}
const desc = skippedTitle
? this.t(message, "responses.skip.skippedTrack", { title: skippedTitle, url: skippedLink })
: this.t(message, "responses.skip.skipped");
? ctx.t(message, "responses.skip.skippedTrack", { title: skippedTitle, url: skippedLink })
: ctx.t(message, "responses.skip.skipped");
const embed = new EmbedBuilder()
.setColor(getGlobalColor())
.setDescription(desc)
;
message.reply({ embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc)] }).catch(() => {});
}
/**
* @async
* Run handler for the skip command.
* Skips instantly for 1-2 listeners; requires a majority vote for 3+
* (threshold configurable per server via `voteSkipThreshold`, and the whole
* system can be turned off via `voteSkip`).
* @param {object} message - The command message wrapper.
* @returns {Promise<void>}
*/
export async function run(message) {
const p = await this.getPlayer(message);
if (!p) return;
const current = p.queue.getCurrent();
if (!p.connection || !current) {
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses._common.nothingPlaying"));
return message.reply({ embeds: [embed] });
}
const guildId = cleanId(p._guildId) || cleanId(message?.channel?.guildId ?? message?.message?.guildId);
const channelId = cleanId(p._channelId ?? p._home247Channel);
const set = this.getSettings(message);
const voteSkipEnabled = set?.get("voteSkip") ?? true;
const threshold = Number(set?.get("voteSkipThreshold")) || 3;
const humanCount = countHumansInChannel({
guildId,
channelId,
client: this.client,
voiceCache: this.voiceCache,
observedVoiceUsers: this.observedVoiceUsers,
});
if (!voteSkipEnabled || humanCount < threshold) {
return doSkip(this, message, p);
}
const userId = message?.author?.id ?? message?.member?.user?.id;
if (!userId) return doSkip(this, message, p);
const needed = Math.floor(humanCount / 2) + 1;
if (p._skipVotes.has(userId)) {
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.skip.alreadyVoted", { votes: p._skipVotes.size, needed }));
return message.reply({ embeds: [embed] }).catch(() => {});
}
p._skipVotes.add(userId);
if (p._skipVotes.size >= needed) {
p._skipVotes.clear();
return doSkip(this, message, p);
}
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.skip.voteAdded", { votes: p._skipVotes.size, needed }));
message.reply({ embeds: [embed] }).catch(() => {});
}