Skip to content

Commit

Permalink
implement demo sound playback using lavalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelcmtd committed Dec 26, 2024
1 parent 0637679 commit ef468b2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 2.0.0

- Add `!join` command
- Add `!play` command (optional, needs Lavalink)
- Use `youtube_poll`
- Remove `!vids` (it requires custom youtube logic and wasn't used)
- Poll for videos on the side channels
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ COPY --from=build /runtime/ /
COPY --from=build /app/bin/app /app/bin/

ENV JANA_DISCORD_TOKEN=
ENV JANA_LAVALINK_BASE=http://localhost:2333
ENV JANA_LAVALINK_PASSWORD=

CMD ["/app/bin/app"]
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# jana

A new Discord bot for [the official chrissx Media Server](https://chrissx.de/discord)
Discord bot for [the official chrissx Media Server](https://chrissx.de/discord).

```sh
# Without Lavalink
docker run -d --restart=unless-stopped --pull=always -e JANA_DISCORD_TOKEN=XXX chrissx/jana:latest

# With Lavalink running on Network lavalink on localhost:2333
docker run -d --restart=unless-stopped --pull=always --network lavalink -e JANA_DISCORD_TOKEN=XXX -e JANA_LAVALINK_BASE=http://localhost:2333 -e JANA_LAVALINK_PASSWORD=XXX chrissx/jana:latest

# Example Lavalink setup
docker run -d --restart=unless-stopped --pull=always --network lavalink -e SERVER_PORT=2333 -e LAVALINK_SERVER_PASSWORD=XXX -v$PWD/application.yml:/opt/Lavalink/application.yml ghcr.io/lavalink-devs/lavalink:4
```
43 changes: 31 additions & 12 deletions bin/jana.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';
import 'package:mutex/mutex.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/nyxx_extensions.dart';
import 'package:nyxx_lavalink/nyxx_lavalink.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
import 'package:youtube_poll/youtube_poll.dart';

Expand Down Expand Up @@ -57,9 +58,18 @@ void main(List<String> argv) async {
exit(1);
}

final lavalink = Platform.environment.containsKey('JANA_LAVALINK_BASE') &&
Platform.environment.containsKey('JANA_LAVALINK_PASSWORD')
? LavalinkPlugin(
base: Uri.parse(Platform.environment['JANA_LAVALINK_BASE']!),
password: Platform.environment['JANA_LAVALINK_PASSWORD']!,
)
: null;

final bot = await Nyxx.connectGateway(
token, GatewayIntents.allUnprivileged | GatewayIntents.messageContent,
options: GatewayClientOptions(plugins: [Logging(), CliIntegration()]));
options: GatewayClientOptions(
plugins: [logging, cliIntegration, if (lavalink != null) lavalink]));

final logMutex = Mutex();
Message? lastLog;
Expand Down Expand Up @@ -107,17 +117,22 @@ void main(List<String> argv) async {
(v) => channel.sendJson(json.encode(videoToJson(v)), '$id.json'));
}
},
'!join': () async {
if (!member.roleIds.any(priv.contains)) throw 'Not authorized';
//final file = await msg.attachments
// .firstWhere((a) => a.url.path.endsWith('.mp3'))
// .fetch();
final voice =
await bot.voice.fetchVoiceState(event.guildId!, member.id);
final myState = GatewayVoiceStateBuilder(
channelId: voice.channelId, isMuted: false, isDeafened: false);
bot.updateVoiceState(event.guildId!, myState);
},
if (lavalink != null)
'!play': () async {
if (!member.roleIds.any(priv.contains)) throw 'Not authorized';
//final file = await msg.attachments
// .firstWhere((a) => a.url.path.endsWith('.mp3'))
// .fetch();
//final voice =
// await bot.voice.fetchVoiceState(event.guildId!, member.id);
final voice = event.guild!.voiceStates[member.id]!;
final vc = await voice.channel!.fetch() as VoiceChannel;
final player = await vc.connectLavalink();
await player.play(await lavalink
.loadTrack('https://gock.dev/email_empfangen.flac')
.then((v) => v.data));
player.onTrackEnd.listen((e) => player.disconnect());
},
};

final handler = commands[cmd];
Expand All @@ -131,6 +146,10 @@ void main(List<String> argv) async {
}
});

if (lavalink == null) {
log.warning('No Lavalink configured');
}

Duration interval() {
final target = DateTime(2000, 1, 1, 1, 0, 45);
final now = DateTime.now().copyWith(year: 2000, month: 1, day: 1, hour: 0);
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: jana
description: A Discord bot
version: 1.6.0-alpha.4
version: 2.0.0-beta.1
homepage: https://github.com/chrissxMedia/jana

environment:
Expand All @@ -14,6 +14,7 @@ dependencies:
mutex: ^3.0.0
nyxx: ^6.0.0
nyxx_extensions: ^4.0.0
nyxx_lavalink: ^4.0.0-dev.1
youtube_explode_dart: ^2.0.0
youtube_poll: ^0.2.0

Expand Down

0 comments on commit ef468b2

Please sign in to comment.