.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

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace NadekoBot.Migrations
{
public partial class vcautodc : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "AutoDcFromVc",
table: "GuildConfigs",
type: "INTEGER",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AutoDcFromVc",
table: "GuildConfigs");
}
}
}

View File

@ -592,6 +592,8 @@ namespace NadekoBot.Migrations
b.Property<ulong>("AutoAssignRoleId");
b.Property<bool>("AutoDcFromVc");
b.Property<bool>("AutoDeleteByeMessages");
b.Property<int>("AutoDeleteByeMessagesTimer");

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);
}
@ -93,7 +99,6 @@ 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;
}
}
}

View File

@ -91,6 +91,7 @@ namespace NadekoBot.Core.Services.Database.Models
public XpSettings XpSettings { get; set; }
public List<FeedSub> FeedSubs { get; set; } = new List<FeedSub>();
public bool AutoDcFromVc { get; set; }
//public List<ProtectionIgnoredChannel> ProtectionIgnoredChannels { get; set; } = new List<ProtectionIgnoredChannel>();
}

View File

@ -891,5 +891,7 @@
"gambling_rafflecur": "{0} Currency Raffle",
"gambling_rafflecur_joined": "User {0} joined the raffle",
"gambling_rafflecur_already_joined": "You have already joined this raffle or the value you used is not valid.",
"gambling_rafflecur_ended": "{0} raffle ended. {1} won {2}!"
"gambling_rafflecur_ended": "{0} raffle ended. {1} won {2}!",
"music_autodc_enable": "I will disconnect from the voice channel when there are no more songs to play.",
"music_autodc_disable": "I will no longer disconnect from the voice channel when there are no more songs to play."
}

View File

@ -3047,5 +3047,12 @@
"{0}rafflecur 20",
"{0}rafflecur mixed 15"
]
},
"autodisconnect": {
"cmd": "autodisconnect autodc",
"desc": "Toggles whether the bot should disconnect from the voice channel once it's done playing all of the songs.",
"usage": [
"{0}autodc"
]
}
}