More refactoring

This commit is contained in:
Master Kwoth 2017-07-17 04:37:51 +02:00
parent 4e11a6c8bc
commit 618968d2e4
62 changed files with 103 additions and 375 deletions

View File

@ -2,9 +2,9 @@
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Extensions;
using NadekoBot.Modules.Music.Services;
using NadekoBot.Services;
using NadekoBot.Services.Administration;
using NadekoBot.Services.Music;
using System;
using System.Collections.Concurrent;
using System.Linq;

View File

@ -1,6 +1,6 @@
using Discord.Commands;
using NadekoBot.Modules.CustomReactions.Services;
using NadekoBot.Services;
using NadekoBot.Services.CustomReactions;
using System;
using System.Linq;
using System.Threading.Tasks;

View File

@ -14,7 +14,7 @@ using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Administration;
using System.Diagnostics;
using NadekoBot.Services.Music;
using NadekoBot.Modules.Music.Services;
namespace NadekoBot.Modules.Administration
{

View File

@ -8,7 +8,7 @@ using Discord;
using NadekoBot.Extensions;
using Discord.WebSocket;
using System;
using NadekoBot.Services.CustomReactions;
using NadekoBot.Modules.CustomReactions.Services;
namespace NadekoBot.Modules.CustomReactions
{

View File

@ -5,6 +5,8 @@ using Discord.WebSocket;
using NadekoBot.DataStructures;
using NadekoBot.DataStructures.Replacements;
using NadekoBot.Extensions;
using NadekoBot.Modules.CustomReactions.Services;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
@ -12,7 +14,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Services.CustomReactions
namespace NadekoBot.Modules.CustomReactions.Extensions
{
public static class Extensions
{

View File

@ -4,15 +4,16 @@ using NadekoBot.DataStructures.ModuleBehaviors;
using NadekoBot.Services.Database.Models;
using NLog;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System;
using System.Threading.Tasks;
using NadekoBot.Services.Permissions;
using NadekoBot.Extensions;
using NadekoBot.Services.Database;
using NadekoBot.Services;
using NadekoBot.Modules.CustomReactions.Extensions;
namespace NadekoBot.Services.CustomReactions
namespace NadekoBot.Modules.CustomReactions.Services
{
public class CustomReactionsService : IEarlyBlockingExecutor, INService
{

View File

@ -1,185 +0,0 @@
namespace NadekoBot.Modules.Gambling
{
//public partial class Gambling
//{
// [Group]
// public class Lucky7Commands : NadekoSubmodule
// {
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// [OwnerOnly]
// public async Task Lucky7Test(uint tests)
// {
// if (tests <= 0)
// return;
// var dict = new Dictionary<float, int>();
// var totalWon = 0;
// for (var i = 0; i < tests; i++)
// {
// var g = new Lucky7Game(10);
// while (!g.Ended)
// {
// if (g.CurrentPosition == 0)
// g.Stay();
// else
// g.Move();
// }
// totalWon += (int)(g.CurrentMultiplier * g.Bet);
// if (!dict.ContainsKey(g.CurrentMultiplier))
// dict.Add(g.CurrentMultiplier, 0);
// dict[g.CurrentMultiplier] ++;
// }
// await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
// .WithTitle("Move Or Stay test")
// .WithDescription(string.Join("\n",
// dict.Select(x => $"x{x.Key} occured {x.Value} times {x.Value * 1.0f / tests * 100:F2}%")))
// .WithFooter(
// efb => efb.WithText($"Total Bet: {tests * 10} | Payout: {totalWon} | {totalWon *1.0f / tests * 10}%")));
// }
// private static readonly ConcurrentDictionary<ulong, Lucky7Game> _games =
// new ConcurrentDictionary<ulong, Lucky7Game>();
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// public async Task Lucky7(int bet)
// {
// if (bet < 4)
// return;
// var game = new Lucky7Game(bet);
// if (!_games.TryAdd(Context.User.Id, game))
// {
// await ReplyAsync("You're already betting on move or stay.").ConfigureAwait(false);
// return;
// }
// if (!await CurrencyHandler.RemoveCurrencyAsync(Context.User, "MoveOrStay bet", bet, false))
// {
// _games.TryRemove(Context.User.Id, out game);
// await ReplyConfirmLocalized("not_enough", CurrencySign).ConfigureAwait(false);
// return;
// }
// await Context.Channel.EmbedAsync(GetGameState(game),
// string.Format("{0} rolled {1}.", Context.User, game.Rolled)).ConfigureAwait(false);
// }
// public enum MoveOrStay
// {
// Move = 1,
// M = 1,
// Stay = 2,
// S = 2
// }
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// public async Task Lucky7(MoveOrStay action)
// {
// Lucky7Game game;
// if (!_games.TryGetValue(Context.User.Id, out game))
// {
// await ReplyAsync("You're not betting on move or stay.").ConfigureAwait(false);
// return;
// }
// if (action == MoveOrStay.Move)
// {
// game.Move();
// await Context.Channel.EmbedAsync(GetGameState(game),
// string.Format("{0} rolled {1}.", Context.User, game.Rolled)).ConfigureAwait(false);
// if (game.Ended)
// _games.TryRemove(Context.User.Id, out game);
// }
// else if (action == MoveOrStay.Stay)
// {
// var won = game.Stay();
// await CurrencyHandler.AddCurrencyAsync(Context.User, "MoveOrStay stay", won, false)
// .ConfigureAwait(false);
// _games.TryRemove(Context.User.Id, out game);
// await ReplyAsync(string.Format("You've finished with {0}",
// won + CurrencySign))
// .ConfigureAwait(false);
// }
// }
// private EmbedBuilder GetGameState(Lucky7Game game)
// {
// var arr = Lucky7Game.Winnings.ToArray();
// var sb = new StringBuilder();
// for (var i = 0; i < arr.Length; i++)
// {
// if (i == game.CurrentPosition)
// {
// sb.Append("[" + arr[i] + "]");
// }
// else
// {
// sb.Append(arr[i].ToString());
// }
// if (i != arr.Length - 1)
// sb.Append(' ');
// }
// return new EmbedBuilder().WithOkColor()
// .WithTitle("Lucky7")
// .WithDescription(sb.ToString())
// .AddField(efb => efb.WithName("Bet")
// .WithValue(game.Bet.ToString())
// .WithIsInline(true))
// .AddField(efb => efb.WithName("Current Value")
// .WithValue((game.CurrentMultiplier * game.Bet).ToString(_cultureInfo))
// .WithIsInline(true));
// }
// }
// public class Lucky7Game
// {
// public int Bet { get; }
// public bool Ended { get; private set; }
// public int PreviousPosition { get; private set; }
// public int CurrentPosition { get; private set; } = -1;
// public int Rolled { get; private set; }
// public float CurrentMultiplier => Winnings[CurrentPosition];
// private readonly NadekoRandom _rng = new NadekoRandom();
// public static readonly ImmutableArray<float> Winnings = new[]
// {
// 1.2f, 0.8f, 0.75f, 0.90f, 0.7f, 0.5f, 1.8f, 0f, 0f
// }.ToImmutableArray();
// public Lucky7Game(int bet)
// {
// Bet = bet;
// Move();
// }
// public void Move()
// {
// if (Ended)
// return;
// PreviousPosition = CurrentPosition;
// Rolled = _rng.Next(1, 4);
// CurrentPosition += Rolled;
// if (CurrentPosition >= 6)
// Ended = true;
// }
// public int Stay()
// {
// if (Ended)
// return 0;
// Ended = true;
// return (int) (CurrentMultiplier * Bet);
// }
// }
//}
}

View File

@ -11,7 +11,7 @@ using System.Text;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Permissions;
using NadekoBot.Services.Help;
using NadekoBot.Modules.Help.Services;
namespace NadekoBot.Modules.Help
{

View File

@ -8,8 +8,9 @@ using Discord.Commands;
using NadekoBot.Extensions;
using System.Linq;
using NadekoBot.Attributes;
using NadekoBot.Services;
namespace NadekoBot.Services.Help
namespace NadekoBot.Modules.Help.Services
{
public class HelpService : ILateExecutor, INService
{

View File

@ -0,0 +1,14 @@
using System;
namespace NadekoBot.Modules.Music.Common.Exceptions
{
// todo use this
public class NotInVoiceChannelException : Exception
{
public NotInVoiceChannelException(string message) : base(message)
{
}
public NotInVoiceChannelException() : base("You're not in the voice channel on this server.") { }
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace NadekoBot.Modules.Music.Common.Exceptions
{
public class QueueFullException : Exception
{
public QueueFullException(string message) : base(message)
{
}
public QueueFullException() : base("Queue is full.") { }
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace NadekoBot.Modules.Music.Common.Exceptions
{
public class SongNotFoundException : Exception
{
public SongNotFoundException(string message) : base(message)
{
}
public SongNotFoundException() : base("Song is not found.") { }
}
}

View File

@ -8,8 +8,11 @@ using System.Linq;
using System.Collections.Concurrent;
using NadekoBot.Extensions;
using System.Diagnostics;
using NadekoBot.Modules.Music.Services;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Common
{
public enum StreamState
{
@ -167,7 +170,7 @@ namespace NadekoBot.Services.Music
SongBuffer b = null;
try
{
b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == Database.Models.MusicType.Local);
b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == MusicType.Local);
//_log.Info("Created buffer, buffering...");
//var bufferTask = b.StartBuffering(cancelToken);
@ -270,7 +273,7 @@ namespace NadekoBot.Services.Music
{
//if last song, and autoplay is enabled, and if it's a youtube song
// do autplay magix
if (queueCount - 1 == data.Index && Autoplay && data.Song?.ProviderType == Database.Models.MusicType.YouTube)
if (queueCount - 1 == data.Index && Autoplay && data.Song?.ProviderType == MusicType.YouTube)
{
try
{
@ -614,7 +617,7 @@ namespace NadekoBot.Services.Music
var sw = Stopwatch.StartNew();
var (_, songs) = Queue.ToArray();
var toUpdate = songs
.Where(x => x.ProviderType == Database.Models.MusicType.YouTube
.Where(x => x.ProviderType == MusicType.YouTube
&& x.TotalTime == TimeSpan.Zero);
var vIds = toUpdate.Select(x => x.VideoId);

View File

@ -1,11 +1,13 @@
using NadekoBot.Extensions;
using NadekoBot.Modules.Music.Common.Exceptions;
using NadekoBot.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Common
{
public class MusicQueue : IDisposable
{

View File

@ -5,7 +5,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Common
{
public class SongBuffer : IDisposable
{

View File

@ -1,6 +1,6 @@
using NLog;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Common
{
public static class SongHandler
{

View File

@ -6,7 +6,7 @@ using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Common
{
public class SongInfo
{

View File

@ -0,0 +1,11 @@
using NadekoBot.Modules.Music.Common.SongResolver.Strategies;
using NadekoBot.Services.Database.Models;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Music.Common.SongResolver
{
public interface ISongResolverFactory
{
Task<IResolveStrategy> GetResolveStrategy(string query, MusicType? musicType);
}
}

View File

@ -1,9 +1,9 @@
using System.Threading.Tasks;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Music.SongResolver.Strategies;
using NadekoBot.Services.Impl;
using NadekoBot.Modules.Music.Common.SongResolver.Strategies;
namespace NadekoBot.Services.Music.SongResolver
namespace NadekoBot.Modules.Music.Common.SongResolver
{
public class SongResolverFactory : ISongResolverFactory
{

View File

@ -1,6 +1,6 @@
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver.Strategies
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
{
public interface IResolveStrategy
{

View File

@ -2,7 +2,7 @@
using System.IO;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver.Strategies
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
{
public class LocalSongResolveStrategy : IResolveStrategy
{

View File

@ -5,7 +5,7 @@ using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver.Strategies
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
{
public class RadioResolveStrategy : IResolveStrategy
{

View File

@ -1,8 +1,8 @@
using NadekoBot.Services.Impl;
using NadekoBot.Services.Music.Extensions;
using NadekoBot.Modules.Music.Extensions;
using NadekoBot.Services.Impl;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver.Strategies
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
{
public class SoundcloudResolveStrategy : IResolveStrategy
{

View File

@ -5,7 +5,7 @@ using System;
using System.Globalization;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver.Strategies
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
{
public class YoutubeResolveStrategy : IResolveStrategy
{

View File

@ -1,9 +1,10 @@
using NadekoBot.Services.Database.Models;
using NadekoBot.Modules.Music.Common;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Impl;
using System;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.Extensions
namespace NadekoBot.Modules.Music.Extensions
{
public static class Extensions
{

View File

@ -9,14 +9,16 @@ using System.Linq;
using NadekoBot.Extensions;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Music;
using NadekoBot.DataStructures;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using NadekoBot.Services.Music.Extensions;
using NadekoBot.Services.Impl;
using NadekoBot.Modules.Music.Services;
using NadekoBot.Modules.Music.Common.Exceptions;
using NadekoBot.Modules.Music.Common;
using NadekoBot.Modules.Music.Extensions;
namespace NadekoBot.Modules.Music
{

View File

@ -10,10 +10,13 @@ using System.IO;
using System.Collections.Generic;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Services.Music.SongResolver;
using NadekoBot.Services.Impl;
using NadekoBot.Services;
using NadekoBot.Modules.Music.Common;
using NadekoBot.Modules.Music.Common.Exceptions;
using NadekoBot.Modules.Music.Common.SongResolver;
namespace NadekoBot.Services.Music
namespace NadekoBot.Modules.Music.Services
{
public class MusicService : INService
{

View File

@ -29,7 +29,6 @@ namespace NadekoBot.Modules.Searches
{
private readonly IBotCredentials _creds;
private readonly IGoogleApiService _google;
private readonly SearchesService _searches;
public Searches(IBotCredentials creds, IGoogleApiService google)
{
@ -791,7 +790,7 @@ namespace NadekoBot.Modules.Searches
tag = tag?.Trim() ?? "";
var imgObj = await _searches.DapiSearch(tag, type, Context.Guild?.Id).ConfigureAwait(false);
var imgObj = await _service.DapiSearch(tag, type, Context.Guild?.Id).ConfigureAwait(false);
if (imgObj == null)
await channel.SendErrorAsync(umsg.Author.Mention + " " + GetText("no_results"));

View File

@ -91,4 +91,8 @@
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\CustomReactions\" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@
using Discord.WebSocket;
using NadekoBot.DataStructures.Replacements;
using NadekoBot.Modules.Music.Services;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Music;
using NLog;
using System;
using System.Linq;

View File

@ -1,28 +0,0 @@
using System;
namespace NadekoBot.Services.Music
{
public class QueueFullException : Exception
{
public QueueFullException(string message) : base(message)
{
}
public QueueFullException() : base("Queue is full.") { }
}
public class SongNotFoundException : Exception
{
public SongNotFoundException(string message) : base(message)
{
}
public SongNotFoundException() : base("Song is not found.") { }
}
public class NotInVoiceChannelException : Exception
{
public NotInVoiceChannelException(string message) : base(message)
{
}
public NotInVoiceChannelException() : base("You're not in the voice channel on this server.") { }
}
}

View File

@ -1,111 +0,0 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//namespace NadekoBot.Services.Music
//{
// public class OldSongResolver
// {
// // public async Task<Song> ResolveSong(string query, MusicType musicType = MusicType.Normal)
// // {
// // if (string.IsNullOrWhiteSpace(query))
// // throw new ArgumentNullException(nameof(query));
// // if (musicType != MusicType.Local && IsRadioLink(query))
// // {
// // musicType = MusicType.Radio;
// // query = await HandleStreamContainers(query).ConfigureAwait(false) ?? query;
// // }
// // try
// // {
// // switch (musicType)
// // {
// // case MusicType.Local:
// // return new Song(new SongInfo
// // {
// // Uri = "\"" + Path.GetFullPath(query) + "\"",
// // Title = Path.GetFileNameWithoutExtension(query),
// // Provider = "Local File",
// // ProviderType = musicType,
// // Query = query,
// // });
// // case MusicType.Radio:
// // return new Song(new SongInfo
// // {
// // Uri = query,
// // Title = $"{query}",
// // Provider = "Radio Stream",
// // ProviderType = musicType,
// // Query = query
// // })
// // { TotalTime = TimeSpan.MaxValue };
// // }
// // if (_sc.IsSoundCloudLink(query))
// // {
// // var svideo = await _sc.ResolveVideoAsync(query).ConfigureAwait(false);
// // return new Song(new SongInfo
// // {
// // Title = svideo.FullName,
// // Provider = "SoundCloud",
// // Uri = await svideo.StreamLink(),
// // ProviderType = musicType,
// // Query = svideo.TrackLink,
// // AlbumArt = svideo.artwork_url,
// // })
// // { TotalTime = TimeSpan.FromMilliseconds(svideo.Duration) };
// // }
// // if (musicType == MusicType.Soundcloud)
// // {
// // var svideo = await _sc.GetVideoByQueryAsync(query).ConfigureAwait(false);
// // return new Song(new SongInfo
// // {
// // Title = svideo.FullName,
// // Provider = "SoundCloud",
// // Uri = await svideo.StreamLink(),
// // ProviderType = MusicType.Soundcloud,
// // Query = svideo.TrackLink,
// // AlbumArt = svideo.artwork_url,
// // })
// // { TotalTime = TimeSpan.FromMilliseconds(svideo.Duration) };
// // }
// // var link = (await _google.GetVideoLinksByKeywordAsync(query).ConfigureAwait(false)).FirstOrDefault();
// // if (string.IsNullOrWhiteSpace(link))
// // throw new OperationCanceledException("Not a valid youtube query.");
// // var allVideos = await Task.Run(async () => { try { return await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false); } catch { return Enumerable.Empty<YouTubeVideo>(); } }).ConfigureAwait(false);
// // var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);
// // var video = videos
// // .Where(v => v.AudioBitrate < 256)
// // .OrderByDescending(v => v.AudioBitrate)
// // .FirstOrDefault();
// // if (video == null) // do something with this error
// // throw new Exception("Could not load any video elements based on the query.");
// // var m = Regex.Match(query, @"\?t=(?<t>\d*)");
// // int gotoTime = 0;
// // if (m.Captures.Count > 0)
// // int.TryParse(m.Groups["t"].ToString(), out gotoTime);
// // var song = new Song(new SongInfo
// // {
// // Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
// // Provider = "YouTube",
// // Uri = await video.GetUriAsync().ConfigureAwait(false),
// // Query = link,
// // ProviderType = musicType,
// // });
// // song.SkipTo = gotoTime;
// // return song;
// // }
// // catch (Exception ex)
// // {
// // _log.Warn($"Failed resolving the link.{ex.Message}");
// // _log.Warn(ex);
// // return null;
// // }
// // }
// }
//}

View File

@ -1,15 +0,0 @@
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Music.SongResolver.Strategies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Music.SongResolver
{
public interface ISongResolverFactory
{
Task<IResolveStrategy> GetResolveStrategy(string query, MusicType? musicType);
}
}

View File

@ -4,9 +4,9 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Services.Help;
using Discord.Commands;
using System.Linq;
using NadekoBot.Modules.Help.Services;
namespace NadekoBot.Services.Utility
{