Skip to content

Commit

Permalink
feat: shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
googlefan256 committed Jan 14, 2024
1 parent ae1d2aa commit a4efdce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Build and Publish Docker

on:
workflow_dispatch:
push:
branches:
- main

jobs:
build_and_push:
Expand Down
26 changes: 22 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const commands: Command[] = [
alias: ["s"],
run: skip,
},
{
name: "shuffle",
alias: [],
run: shuffle,
},
{
name: "pause",
alias: [],
Expand Down Expand Up @@ -142,7 +147,7 @@ async function skip(message: Message<true>, args: string[]) {
}
const { player } = get(message.guildId!);
if (!player) return;
player.skip();
await player.skip();
}

async function pause(message: Message<true>, args: string[]) {
Expand All @@ -151,7 +156,7 @@ async function pause(message: Message<true>, args: string[]) {
}
const { player } = get(message.guildId!);
if (!player) return;
player.pause();
await player.pause();
}

async function resume(message: Message<true>, args: string[]) {
Expand All @@ -160,7 +165,7 @@ async function resume(message: Message<true>, args: string[]) {
}
const { player } = get(message.guildId!);
if (!player) return;
player.resume();
await player.resume();
}

async function stop(message: Message<true>, args: string[]) {
Expand All @@ -169,7 +174,7 @@ async function stop(message: Message<true>, args: string[]) {
}
const { player } = get(message.guildId!);
if (!player) return;
player.clear();
await player.clear();
}

async function loop(message: Message<true>, args: string[]) {
Expand Down Expand Up @@ -223,6 +228,10 @@ async function help(message: Message<true>, args: string[]) {
name: "skip",
value: "再生中の音楽をスキップします。",
},
{
name: "shuffle",
value: "キューをシャッフルします。",
},
{
name: "pause",
value: "再生中の音楽を一時停止します。",
Expand Down Expand Up @@ -297,3 +306,12 @@ async function playlist(message: Message<true>, args: string[]) {
}
await message.reply(`プレイリストから${pl.length}曲追加しました。`);
}

async function shuffle(message: Message<true>, args: string[]) {
if (!message.member?.voice.channelId) {
return;
}
const { player } = get(message.guildId!);
if (!player) return;
await player.shuffle();
}
20 changes: 20 additions & 0 deletions src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import {
} from "discord.js";
import { Video, dl } from "./yt";
import { error, info } from "./logger";
function shuffle<T>(arr: T[]) {
arr = arr.slice();
let i = arr.length;
let j = 0;
let temp: T;
if (i === 0) {
return arr;
}
while (--i) {
j = Math.floor(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}

export class Player {
private playing = false;
Expand Down Expand Up @@ -54,6 +70,10 @@ export class Player {
this.player.stop();
await this.channel.send("停止しました。");
}
async shuffle() {
this.queue = shuffle(this.queue);
await this.channel.send("シャッフルしました。");
}
async play() {
if (!this.queue.length) {
this.playing = false;
Expand Down

0 comments on commit a4efdce

Please sign in to comment.