.autodc added (Auto leave voice channel when all songs have been played)

This commit is contained in:
Master Kwoth
2017-10-22 17:16:11 +02:00
parent 65fc585a7b
commit ed32e85bac
9 changed files with 2000 additions and 11 deletions

View File

@ -142,11 +142,8 @@ namespace NadekoBot.Modules.Music.Common
this._musicService = musicService;
this._google = google;
_log.Info("Initialized");
_player = new Thread(new ThreadStart(PlayerLoop));
_player.Start();
_log.Info("Loop started");
}
private async void PlayerLoop()
@ -383,7 +380,6 @@ namespace NadekoBot.Modules.Music.Common
}
newVoiceChannel = false;
_log.Info("Get current user");
var curUser = await VoiceChannel.Guild.GetCurrentUserAsync();
if (curUser.VoiceChannel != null)
{

View File

@ -332,6 +332,19 @@ namespace NadekoBot.Modules.Music
mp.Stop();
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task AutoDisconnect()
{
var newVal = _service.ToggleAutoDc(Context.Guild.Id);
if(newVal)
await ReplyConfirmLocalized("autodc_enable").ConfigureAwait(false);
else
await ReplyConfirmLocalized("autodc_disable").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Destroy()

View File

@ -14,6 +14,7 @@ using NadekoBot.Core.Services;
using NadekoBot.Modules.Music.Common;
using NadekoBot.Modules.Music.Common.Exceptions;
using NadekoBot.Modules.Music.Common.SongResolver;
using NadekoBot.Common.Collections;
namespace NadekoBot.Modules.Music.Services
{
@ -29,6 +30,9 @@ namespace NadekoBot.Modules.Music.Services
private readonly SoundCloudApiService _sc;
private readonly IBotCredentials _creds;
private readonly ConcurrentDictionary<ulong, float> _defaultVolumes;
public ConcurrentHashSet<ulong> AutoDcServers { get; }
private readonly DiscordSocketClient _client;
public ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();
@ -54,6 +58,8 @@ namespace NadekoBot.Modules.Music.Services
bot.AllGuildConfigs
.ToDictionary(x => x.GuildId, x => x.DefaultMusicVolume));
AutoDcServers = new ConcurrentHashSet<ulong>(bot.AllGuildConfigs.Where(x => x.AutoDcFromVc).Select(x => x.GuildId));
Directory.CreateDirectory(MusicDataPath);
}
@ -92,8 +98,7 @@ namespace NadekoBot.Modules.Music.Services
{
string GetText(string text, params object[] replacements) =>
_strings.GetText(text, _localization.GetCultureInfo(textCh.Guild), "Music".ToLowerInvariant(), replacements);
_log.Info("Checks");
if (voiceCh == null || voiceCh.Guild != textCh.Guild)
{
if (textCh != null)
@ -102,18 +107,14 @@ namespace NadekoBot.Modules.Music.Services
}
throw new NotInVoiceChannelException();
}
_log.Info("Get or add");
return MusicPlayers.GetOrAdd(guildId, _ =>
{
_log.Info("Getting default volume");
var vol = GetDefaultVolume(guildId);
_log.Info("Creating musicplayer instance");
var mp = new MusicPlayer(this, _google, voiceCh, textCh, vol);
IUserMessage playingMessage = null;
IUserMessage lastFinishedMessage = null;
_log.Info("Subscribing");
mp.OnCompleted += async (s, song) =>
{
try
@ -132,6 +133,16 @@ namespace NadekoBot.Modules.Music.Services
{
// ignored
}
var cur = mp.Current;
if (cur.Current == null
&& !mp.RepeatCurrentSong
&& !mp.RepeatPlaylist
&& !mp.FairPlay
&& AutoDcServers.Contains(guildId))
{
await DestroyPlayer(guildId).ConfigureAwait(false);
}
}
catch
{
@ -235,5 +246,23 @@ namespace NadekoBot.Modules.Music.Services
if (MusicPlayers.TryRemove(id, out var mp))
await mp.Destroy();
}
public bool ToggleAutoDc(ulong id)
{
bool val;
using (var uow = _db.UnitOfWork)
{
var gc = uow.GuildConfigs.For(id, set => set);
val = gc.AutoDcFromVc = !gc.AutoDcFromVc;
uow.Complete();
}
if (val)
AutoDcServers.Add(id);
else
AutoDcServers.TryRemove(id);
return val;
}
}
}