Skip to content

Commit

Permalink
feat: getIt + bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomassasovsky committed Aug 1, 2024
1 parent 312c6a0 commit a235a7d
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 271 deletions.
24 changes: 20 additions & 4 deletions bin/radio_horizon_development.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:get_it/get_it.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:radio_horizon/radio_horizon.dart';
import 'package:shazam_client/shazam_client.dart';

final getIt = GetIt.instance;

Future<void> main() async {
dotEnvFlavour = DotEnvFlavour.development;
Expand Down Expand Up @@ -44,12 +48,24 @@ Future<void> main() async {
..registerPlugin(IgnoreExceptions())
..registerPlugin(commands);

// Initialise our services
MusicService.init(client);
await DatabaseService.init(client);
final databaseService = DatabaseService(client);
await databaseService.initialize();

final musicService = MusicService(client);
final bootupService =
BootUpService(client: client, databaseService: databaseService);
final songRecognitionService =
SongRecognitionService(ShazamClient.dockerized());

getIt
..registerSingleton<MusicService>(musicService)
..registerSingleton<DatabaseService>(databaseService)
..registerSingleton<BootUpService>(bootupService)
..registerSingleton<SongRecognitionService>(songRecognitionService);

client.onReady.listen((_) async {
BootUpService.init(client, DatabaseService.instance);
await musicService.initialize();
await bootupService.initialize(musicService.cluster);
});

// Connect
Expand Down
27 changes: 23 additions & 4 deletions bin/radio_horizon_production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

import 'dart:async';

import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:radio_horizon/radio_horizon.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_logging/sentry_logging.dart';
import 'package:shazam_client/shazam_client.dart';

final getIt = GetIt.instance;

Future<void> main() async {
await runZonedGuarded(() async {
Expand Down Expand Up @@ -59,10 +63,25 @@ Future<void> main() async {
..registerPlugin(IgnoreExceptions())
..registerPlugin(commands);

// Initialise our services
MusicService.init(client);
await DatabaseService.init(client);
BootUpService.init(client, DatabaseService.instance);
final databaseService = DatabaseService(client);
await databaseService.initialize();

final musicService = MusicService(client);
final bootupService =
BootUpService(client: client, databaseService: databaseService);
final songRecognitionService =
SongRecognitionService(ShazamClient.dockerized());

getIt
..registerSingleton<MusicService>(musicService)
..registerSingleton<DatabaseService>(databaseService)
..registerSingleton<BootUpService>(bootupService)
..registerSingleton<SongRecognitionService>(songRecognitionService);

client.onReady.listen((_) async {
await musicService.initialize();
await bootupService.initialize(musicService.cluster);
});

// Connect
await client.connect();
Expand Down
11 changes: 9 additions & 2 deletions lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:get_it/get_it.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
import 'package:nyxx_interactions/nyxx_interactions.dart';
Expand All @@ -14,12 +15,16 @@ export 'music.dart';
export 'radio.dart';
export 'sound.dart';

final _getIt = GetIt.instance;

Future<void> connectIfNeeded(
IChatContext context, {
bool replace = false,
}) async {
if (replace) {
MusicService.instance.cluster
_getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.destroy(context.guild!.id);
context.guild!.shard.changeVoiceState(
Expand Down Expand Up @@ -51,7 +56,9 @@ Future<void> connectToChannel(
bool replace = false,
}) async {
if (replace) {
MusicService.instance.cluster
_getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(guild.id)
.destroy(guild.id);
guild.shard.changeVoiceState(
Expand Down
5 changes: 4 additions & 1 deletion lib/src/commands/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'dart:io';

import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
Expand All @@ -22,13 +23,15 @@ String getCurrentMemoryString() {
final _enInfoCommand = AppLocale.en.translations.commands.info;
final _logger = Logger('command/info');

final _getIt = GetIt.instance;

ChatCommand info = ChatCommand(
_enInfoCommand.command,
_enInfoCommand.description,
id('info', (IChatContext context) async {
context as InteractionChatContext;
final commandTranslations = getCommandTranslations(context).info;
final nodes = MusicService.instance.cluster.connectedNodes;
final nodes = _getIt.get<MusicService>().cluster.connectedNodes;
final players = nodes.values
.map((b) => b.players.length)
.reduce((value, element) => value + element);
Expand Down
16 changes: 12 additions & 4 deletions lib/src/commands/music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'dart:async';
import 'dart:math' as math;

import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';
Expand All @@ -18,6 +19,7 @@ final _enMusicCommand = AppLocale.en.translations.commands.music;
final _enPlayCommand = _enMusicCommand.children.play;

final _logger = Logger('command/music');
final _getIt = GetIt.instance;

ChatGroup music = ChatGroup(
_enMusicCommand.command,
Expand Down Expand Up @@ -53,7 +55,9 @@ ChatCommand:music-play: {
}''',
);

final node = MusicService.instance.cluster
final node = _getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id);
await connectIfNeeded(context);
final result = await node.autoSearch(query);
Expand Down Expand Up @@ -106,7 +110,9 @@ ChatCommand:music-play: {
);
}

await DatabaseService.instance.deleteRadioFromList(context.guild!.id);
await _getIt
.get<DatabaseService>()
.deleteRadioFromList(context.guild!.id);
}),
localizedDescriptions: localizedValues(
(translations) => translations.commands.music.children.play.description,
Expand Down Expand Up @@ -141,8 +147,10 @@ ChatCommand:music-play: autocompletion: {
}''',
);

final node =
MusicService.instance.cluster.getOrCreatePlayerNode(context.guild!.id);
final node = _getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id);
final response =
await node.autoSearch(query).timeout(const Duration(milliseconds: 2500));

Expand Down
44 changes: 26 additions & 18 deletions lib/src/commands/radio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:shazam_client/shazam_client.dart';

final _enRadioCommand = AppLocale.en.translations.commands.radio;
final _enPlayCommand = _enRadioCommand.children.play;
final _enPlayRandomCommand = _enRadioCommand.children.playRandom;
final _enRecognizeCommand = _enRadioCommand.children.recognize;
final _enUpvoteCommand = _enRadioCommand.children.upvote;

Expand All @@ -46,7 +47,7 @@ ChatGroup radio = ChatGroup(
ChatCommand(
_enPlayCommand.command,
_enPlayCommand.description,
id('radio-play', (
id('radioplay', (
IChatContext context,
@Description('The name of the Radio Station to play')
@Autocomplete(autocompleteRadioQuery)
Expand Down Expand Up @@ -76,7 +77,9 @@ ChatCommand:radio-play: {

await connectIfNeeded(context, replace: true);

final node = MusicService.instance.cluster
final node = getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id);

late final RadioBrowserListResponse<Station> stations;
Expand Down Expand Up @@ -125,7 +128,7 @@ ChatCommand:radio-play: {
channelId: context.channel.id,
).startPlaying();

final databaseService = DatabaseService.instance;
final databaseService = getIt.get<DatabaseService>();
await databaseService.setCurrentRadio(
context.guild!.id,
context.member!.voiceState!.channel!.id,
Expand All @@ -151,8 +154,8 @@ ChatCommand:radio-play: {
),
),
ChatCommand(
_enPlayCommand.command,
_enPlayCommand.description,
_enPlayRandomCommand.command,
_enPlayRandomCommand.description,
id('radio-play-random', (
IChatContext context,
) async {
Expand Down Expand Up @@ -187,7 +190,9 @@ ChatCommand:radio-play-random: {

await connectIfNeeded(context, replace: true);

final node = MusicService.instance.cluster
final node = getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id);

await _radioBrowserClient.clickStation(
Expand Down Expand Up @@ -215,12 +220,12 @@ ChatCommand:radio-play-random: {
channelId: context.channel.id,
).startPlaying();

await DatabaseService.instance.setCurrentRadio(
context.guild!.id,
context.member!.voiceState!.channel!.id,
context.channel.id,
radio,
);
await getIt.get<DatabaseService>().setCurrentRadio(
context.guild!.id,
context.member!.voiceState!.channel!.id,
context.channel.id,
radio,
);

final embed = EmbedBuilder()
..color = getRandomColor()
Expand All @@ -233,10 +238,12 @@ ChatCommand:radio-play-random: {
await context.respond(MessageBuilder.embed(embed));
}),
localizedDescriptions: localizedValues(
(translations) => translations.commands.radio.children.play.description,
(translations) =>
translations.commands.radio.children.playRandom.description,
),
localizedNames: localizedValues(
(translations) => translations.commands.radio.children.play.command,
(translations) =>
translations.commands.radio.children.playRandom.command,
),
),
ChatCommand(
Expand All @@ -251,8 +258,8 @@ ChatCommand:radio-play-random: {
CurrentStationInfo? stationInfo;

try {
final recognitionService = SongRecognitionService.instance;
final databaseService = DatabaseService.instance;
final recognitionService = getIt.get<SongRecognitionService>();
final databaseService = getIt.get<DatabaseService>();
final guildId = context.guild!.id;

var recognitionSampleDuration = 10;
Expand Down Expand Up @@ -363,8 +370,9 @@ ChatCommand:radio-play-random: {

late GuildRadio? guildRadio;
try {
guildRadio =
await DatabaseService.instance.currentRadio(context.guild!.id);
guildRadio = await getIt
.get<DatabaseService>()
.currentRadio(context.guild!.id);
} on RadioNotPlayingException {
await context.respond(
MessageBuilder.embed(
Expand Down
28 changes: 20 additions & 8 deletions lib/src/commands/sound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ ChatCommand:skip: {
}''',
);

final node =
MusicService.instance.cluster.getOrCreatePlayerNode(context.guild!.id);
final node = getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id);
final player = node.players[context.guild!.id]!;

if (player.queue.isEmpty) {
Expand Down Expand Up @@ -78,7 +80,9 @@ ChatCommand:leave: {
}''',
);

MusicService.instance.cluster
getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.destroy(context.guild!.id);
context.guild!.shard.changeVoiceState(context.guild!.id, null);
Expand Down Expand Up @@ -113,7 +117,7 @@ ChatCommand:join: {
}''',
);

MusicService.instance.cluster.getOrCreatePlayerNode(context.guild!.id);
getIt.get<MusicService>().cluster.getOrCreatePlayerNode(context.guild!.id);
await connectIfNeeded(context);
await context.respond(MessageBuilder.content(commandTranslations.joined));
}),
Expand Down Expand Up @@ -154,7 +158,9 @@ ChatCommand:volume: {
}''',
);

MusicService.instance.cluster
getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.volume(
context.guild!.id,
Expand Down Expand Up @@ -195,7 +201,9 @@ ChatCommand:pause: {
}''',
);

MusicService.instance.cluster
getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.pause(context.guild!.id);
await context.respond(MessageBuilder.content(commandTranslations.paused));
Expand Down Expand Up @@ -228,7 +236,9 @@ ChatCommand:resume: {
}''',
);

MusicService.instance.cluster
getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.resume(context.guild!.id);
await context.respond(MessageBuilder.content(commandTranslations.resumed));
Expand Down Expand Up @@ -260,7 +270,9 @@ ChatCommand:stop: {
}''',
);

MusicService.instance.cluster
getIt
.get<MusicService>()
.cluster
.getOrCreatePlayerNode(context.guild!.id)
.stop(context.guild!.id);
await context.respond(MessageBuilder.content(commandTranslations.stopped));
Expand Down
Loading

0 comments on commit a235a7d

Please sign in to comment.