Skip to content

Commit

Permalink
Merge pull request #56 from RainyXeon/dev
Browse files Browse the repository at this point in the history
5.0.12 update
  • Loading branch information
RainyXeon authored Mar 12, 2024
2 parents 886f9d3 + 1fc019b commit 2a06ceb
Show file tree
Hide file tree
Showing 25 changed files with 300 additions and 80 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.8"

services:
lavalink:
image: ghcr.io/lavalink-devs/lavalink:4-alpine
image: ghcr.io/lavalink-devs/lavalink:latest-alpine
container_name: lavalink
user: root
restart: unless-stopped
Expand Down
8 changes: 0 additions & 8 deletions src/buttons/VolumeDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ export default class implements PlayerButton {

player.setVolume(player.volume * 100 - 10);

this.setVol247(client, player, player.volume * 100 - 10);

await new ReplyInteractionService(client, message, reply_msg);
return;
}

async setVol247(client: Manager, player: KazagumoPlayer, vol: number) {
if (await client.db.autoreconnect.get(player.guildId)) {
await client.db.autoreconnect.set(`${player.guildId}.config.volume`, vol);
}
}
}
8 changes: 0 additions & 8 deletions src/buttons/VolumeUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ export default class implements PlayerButton {
}

player.setVolume(player.volume * 100 + 10);

this.setVol247(client, player, player.volume * 100 + 10);
await new ReplyInteractionService(client, message, reply_msg);
return;
}

async setVol247(client: Manager, player: KazagumoPlayer, vol: number) {
if (await client.db.autoreconnect.get(player.guildId)) {
await client.db.autoreconnect.set(`${player.guildId}.config.volume`, vol);
}
}
}
154 changes: 154 additions & 0 deletions src/commands/Music/Insert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
ApplicationCommandOptionType,
AutocompleteInteraction,
CommandInteraction,
EmbedBuilder,
Message,
} from "discord.js";
import { Manager } from "../../manager.js";
import { Accessableby, Command } from "../../structures/Command.js";
import { CommandHandler } from "../../structures/CommandHandler.js";
import { KazagumoPlayer, KazagumoTrack } from "../../lib/main.js";
import { ConvertTime } from "../../utilities/ConvertTime.js";
import { AutocompleteInteractionChoices, GlobalInteraction } from "../../@types/Interaction.js";

// Main code
export default class implements Command {
public name = ["insert"];
public description = "Insert a song into a specific position in queue.";
public category = "Music";
public accessableby = Accessableby.Member;
public usage = "";
public aliases = [];
public lavalink = true;
public playerCheck = true;
public usingInteraction = true;
public sameVoiceCheck = true;
public permissions = [];
public options = [
{
name: "position",
description: "The position in queue want to remove.",
type: ApplicationCommandOptionType.Integer,
required: true,
},
{
name: "search",
description: "The song link or name",
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
},
];

public async execute(client: Manager, handler: CommandHandler) {
await handler.deferReply();

const player = client.manager.players.get(handler.guild!.id) as KazagumoPlayer;

const position = Number(handler.args[0]);
handler.args.splice(0, 1);
const song = handler.args.join(" ");
if (position && isNaN(+position))
return handler.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`${client.i18n.get(handler.language, "error", "number_invalid")}`)
.setColor(client.color),
],
});
if (Number(position) == 0)
return handler.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`${client.i18n.get(handler.language, "command.music", "insert_already")}`)
.setColor(client.color),
],
});
if (Number(position) > player.queue.length)
return handler.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`${client.i18n.get(handler.language, "command.music", "insert_notfound")}`)
.setColor(client.color),
],
});

const result = await player.search(song, { requester: handler.user });
const track = result.tracks[0];

if (!result.tracks.length)
return handler.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`${client.i18n.get(handler.language, "command.music", "insert_notfound")}`)
.setColor(client.color),
],
});

player.queue.splice(position - 1, 0, track);

const embed = new EmbedBuilder()
.setDescription(
`${client.i18n.get(handler.language, "command.music", "insert_desc", {
name: this.getTitle(client, track),
duration: new ConvertTime().parse(player.shoukaku.position),
request: String(track.requester),
})}`
)
.setColor(client.color);

return handler.editReply({ embeds: [embed] });
}

getTitle(client: Manager, tracks: KazagumoTrack): string {
if (client.config.lavalink.AVOID_SUSPEND) return tracks.title;
else {
return `[${tracks.title}](${tracks.uri})`;
}
}

// Autocomplete function
async autocomplete(client: Manager, interaction: GlobalInteraction, language: string) {
let choice: AutocompleteInteractionChoices[] = [];
const url = String((interaction as CommandInteraction).options.get("search")!.value);

const Random =
client.config.lavalink.AUTOCOMPLETE_SEARCH[
Math.floor(Math.random() * client.config.lavalink.AUTOCOMPLETE_SEARCH.length)
];

const match = client.REGEX.some((match) => {
return match.test(url) == true;
});

if (match == true) {
choice.push({ name: url, value: url });
await (interaction as AutocompleteInteraction).respond(choice).catch(() => {});
return;
}

if (client.lavalinkUsing.length == 0) {
choice.push({
name: `${client.i18n.get(language, "command.music", "no_node")}`,
value: `${client.i18n.get(language, "command.music", "no_node")}`,
});
return;
}
const searchRes = await client.manager.search(url || Random);

if (searchRes.tracks.length == 0 || !searchRes.tracks) {
return choice.push({ name: "Error song not matches", value: url });
}

for (let i = 0; i < 10; i++) {
const x = searchRes.tracks[i];
choice.push({
name: x && x.title ? x.title : "Unknown track name",
value: x && x.uri ? x.uri : url,
});
}

await (interaction as AutocompleteInteraction).respond(choice).catch(() => {});
}
}
1 change: 0 additions & 1 deletion src/commands/Music/Play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class implements Command {
public usingInteraction = true;
public sameVoiceCheck = false;
public permissions = [];

public options = [
{
name: "search",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Music/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class implements Command {

const pages = [];
for (let i = 0; i < pagesNum; i++) {
const str = songStrings.slice(i * 10, i * 10 + 10).join("");
const str = songStrings.slice(i * 10, i * 10 + 10).join("\n");

const embed = new EmbedBuilder()
.setAuthor({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Music/Remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class implements Command {
return handler.editReply({
embeds: [
new EmbedBuilder()
.setDescription(`${client.i18n.get(handler.language, "command.music", "number_invalid")}`)
.setDescription(`${client.i18n.get(handler.language, "error", "number_invalid")}`)
.setColor(client.color),
],
});
Expand Down
9 changes: 0 additions & 9 deletions src/commands/Music/Volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export default class implements Command {

await player.setVolume(Number(value));

this.setVol247(client, player, Number(value));

const changevol = new EmbedBuilder()
.setDescription(
`${client.i18n.get(handler.language, "command.music", "volume_msg", {
Expand All @@ -73,11 +71,4 @@ export default class implements Command {

handler.editReply({ content: " ", embeds: [changevol] });
}

async setVol247(client: Manager, player: KazagumoPlayer, vol: number) {
const data = await new AutoReconnectBuilderService(client, player).execute(player.guildId);
if (data) {
await client.db.autoreconnect.set(`${player.guildId}.config.volume`, vol);
}
}
}
1 change: 0 additions & 1 deletion src/database/setup/lavalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export class AutoReconnectLavalinkService {
if (data.value.previous.length !== 0) await this.previousDataPush(data.value.previous, player);

if (data.value.config.loop !== "none") player.setLoop(data.value.config.loop as KazagumoLoopMode);
if (data.value.config.volume !== 1) player.setVolume(data.value.config.volume);
await player.play(search.tracks[0]);
}
}
Expand Down
42 changes: 28 additions & 14 deletions src/events/guild/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from "discord.js";
import { Manager } from "../../manager.js";
import { GlobalInteraction, NoAutoInteraction } from "../../@types/Interaction.js";
import { CheckPermissionServices } from "../../services/CheckPermissionService.js";
import {
CheckPermissionResultInterface,
CheckPermissionServices,
} from "../../services/CheckPermissionService.js";
import { CommandHandler } from "../../structures/CommandHandler.js";
import { Accessableby } from "../../structures/Command.js";
import { ConvertToMention } from "../../utilities/ConvertToMention.js";
Expand Down Expand Up @@ -84,19 +87,30 @@ export default class {
const permissionChecker = new CheckPermissionServices();

// Default permission
const defaultPermissions = [PermissionFlagsBits.ManageMessages];
const defaultPermissions = [
PermissionFlagsBits.ManageMessages,
PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.SendMessages,
PermissionFlagsBits.EmbedLinks,
];
const musicPermissions = [PermissionFlagsBits.Speak, PermissionFlagsBits.Connect];
const managePermissions = [PermissionFlagsBits.ManageChannels];

async function respondError(
interaction: ChatInputCommandInteraction | CommandInteraction,
permission: string
permissionResult: CheckPermissionResultInterface
) {
const selfErrorString = `${client.i18n.get(language, "error", "no_perms", {
perm: permissionResult.result,
})}`;
const embed = new EmbedBuilder()
.setDescription(
`${client.i18n.get(language, "error", "no_perms", {
perm: permission,
})}`
permissionResult.channel == "Self"
? selfErrorString
: `${client.i18n.get(language, "error", "no_perms_channel", {
perm: permissionResult.result,
channel: permissionResult.channel,
})}`
)
.setColor(client.color);
await interaction.reply({
Expand All @@ -105,19 +119,19 @@ export default class {
}

if (command.name[0] !== "help") {
const returnData = await permissionChecker.interaction(interaction, defaultPermissions);
if (returnData !== "PermissionPass") return respondError(interaction, returnData);
const returnData = permissionChecker.interaction(interaction, defaultPermissions);
if (returnData.result !== "PermissionPass") return respondError(interaction, returnData);
}
if (command.category.toLocaleLowerCase() == "music") {
const returnData = await permissionChecker.interaction(interaction, musicPermissions);
if (returnData !== "PermissionPass") return respondError(interaction, returnData);
const returnData = permissionChecker.interaction(interaction, musicPermissions);
if (returnData.result !== "PermissionPass") return respondError(interaction, returnData);
}
if (command.accessableby == Accessableby.Manager) {
const returnData = await permissionChecker.interaction(interaction, managePermissions);
if (returnData !== "PermissionPass") return respondError(interaction, returnData);
const returnData = permissionChecker.interaction(interaction, managePermissions);
if (returnData.result !== "PermissionPass") return respondError(interaction, returnData);
} else if (command.permissions.length !== 0) {
const returnData = await permissionChecker.interaction(interaction, command.permissions);
if (returnData !== "PermissionPass") return respondError(interaction, returnData);
const returnData = permissionChecker.interaction(interaction, command.permissions);
if (returnData.result !== "PermissionPass") return respondError(interaction, returnData);
}
//////////////////////////////// Permission check end ////////////////////////////////

Expand Down
39 changes: 24 additions & 15 deletions src/events/guild/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Manager } from "../../manager.js";
import { EmbedBuilder } from "discord.js";
import { stripIndents } from "common-tags";
import fs from "fs";
import { CheckPermissionServices } from "../../services/CheckPermissionService.js";
import {
CheckPermissionResultInterface,
CheckPermissionServices,
} from "../../services/CheckPermissionService.js";
import { CommandHandler } from "../../structures/CommandHandler.js";
import { Accessableby } from "../../structures/Command.js";
import { RatelimitReplyService } from "../../services/RatelimitReplyService.js";
Expand Down Expand Up @@ -116,12 +119,18 @@ export default class {
const musicPermissions = [PermissionFlagsBits.Speak, PermissionFlagsBits.Connect];
const managePermissions = [PermissionFlagsBits.ManageChannels];

async function respondError(permission: string) {
async function respondError(permissionResult: CheckPermissionResultInterface) {
const selfErrorString = `${client.i18n.get(language, "error", "no_perms", {
perm: permissionResult.result,
})}`;
const embed = new EmbedBuilder()
.setDescription(
`${client.i18n.get(language, "error", "no_perms", {
perm: permission,
})}`
permissionResult.channel == "Self"
? selfErrorString
: `${client.i18n.get(language, "error", "no_perms_channel", {
perm: permissionResult.result,
channel: permissionResult.channel,
})}`
)
.setColor(client.color);
const dmChannel =
Expand All @@ -131,21 +140,21 @@ export default class {
});
}

const returnData = await permissionChecker.message(message, defaultPermissions);
if (returnData !== "PermissionPass") return respondError(returnData);
const returnData = permissionChecker.message(message, defaultPermissions);
if (returnData.result !== "PermissionPass") return respondError(returnData);

if (command.accessableby == Accessableby.Manager) {
const returnData = await permissionChecker.message(message, managePermissions);
if (returnData !== "PermissionPass") return respondError(returnData);
const returnData = permissionChecker.message(message, managePermissions);
if (returnData.result !== "PermissionPass") return respondError(returnData);
} else if (command.category == "Music") {
const returnData = await permissionChecker.message(message, musicPermissions);
if (returnData !== "PermissionPass") return respondError(returnData);
const returnData = permissionChecker.message(message, musicPermissions);
if (returnData.result !== "PermissionPass") return respondError(returnData);
} else if (command.name.join("-") !== "help") {
const returnData = await permissionChecker.message(message, allCommandPermissions);
if (returnData !== "PermissionPass") return respondError(returnData);
const returnData = permissionChecker.message(message, allCommandPermissions);
if (returnData.result !== "PermissionPass") return respondError(returnData);
} else if (command.permissions.length !== 0) {
const returnData = await permissionChecker.message(message, command.permissions);
if (returnData !== "PermissionPass") return respondError(returnData);
const returnData = permissionChecker.message(message, command.permissions);
if (returnData.result !== "PermissionPass") return respondError(returnData);
}
//////////////////////////////// Permission check end ////////////////////////////////

Expand Down
Loading

0 comments on commit 2a06ceb

Please sign in to comment.