Work on rotating playing statuses

This commit is contained in:
Kwoth
2016-08-27 02:41:41 +02:00
parent ae5634c9b7
commit 9b292f3bb8
5 changed files with 191 additions and 160 deletions

View File

@@ -1,168 +1,113 @@
//using Discord.Commands;
//using NadekoBot.Classes;
//using NadekoBot.Classes.JSONModels;
//using NadekoBot.Modules.Music;
//using NadekoBot.Modules.Permissions.Classes;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;
//using System.Timers;
//using Timer = System.Timers.Timer;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
////todo DB
//namespace NadekoBot.Modules.Administration
//{
// internal class PlayingRotate : DiscordCommand
// {
// private static readonly Timer timer = new Timer(20000);
//todo owner only
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class PlayingRotateCommands
{
public static Dictionary<string, Func<string>> PlayingPlaceholders { get; } =
new Dictionary<string, Func<string>> {
{"%servers%", () => NadekoBot.Client.GetGuilds().Count().ToString()},
{"%users%", () => NadekoBot.Client.GetGuilds().Select(s => s.GetUsers().Count).Sum().ToString()},
{"%playing%", () => {
var cnt = Music.Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
if (cnt != 1) return cnt.ToString();
try {
var mp = Music.Music.MusicPlayers.FirstOrDefault();
return mp.Value.CurrentSong.SongInfo.Title;
}
catch {
return "No songs";
}
}
},
{"%queued%", () => Music.Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}
};
// public static Dictionary<string, Func<string>> PlayingPlaceholders { get; } =
// new Dictionary<string, Func<string>> {
// {"%servers%", () => NadekoBot.Client.Servers.Count().ToString()},
// {"%users%", () => NadekoBot.Client.Servers.SelectMany(s => s.Users).Count().ToString()},
// {"%playing%", () => {
// var cnt = MusicModule.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
// if (cnt != 1) return cnt.ToString();
// try {
// var mp = MusicModule.MusicPlayers.FirstOrDefault();
// return mp.Value.CurrentSong.SongInfo.Title;
// }
// catch {
// return "No songs";
// }
// }
// },
// {"%queued%", () => MusicModule.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()},
// {"%trivia%", () => Games.Commands.TriviaCommands.RunningTrivias.Count.ToString()}
// };
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task RotatePlaying(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
// private readonly SemaphoreSlim playingPlaceholderLock = new SemaphoreSlim(1,1);
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(channel.Guild.Id);
// public PlayingRotate(DiscordModule module) : base(module)
// {
// var i = -1;
// timer.Elapsed += async (s, e) =>
// {
// try
// {
// i++;
// var status = "";
// //wtf am i doing, just use a queue ffs
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false);
// try
// {
// if (PlayingPlaceholders.Count == 0
// || NadekoBot.Config.RotatingStatuses.Count == 0
// || i >= NadekoBot.Config.RotatingStatuses.Count)
// {
// i = 0;
// }
// status = NadekoBot.Config.RotatingStatuses[i];
// status = PlayingPlaceholders.Aggregate(status,
// (current, kvp) => current.Replace(kvp.Key, kvp.Value()));
// }
// finally { playingPlaceholderLock.Release(); }
// if (string.IsNullOrWhiteSpace(status))
// return;
// await Task.Run(() => { NadekoBot.Client.SetGame(status); });
// }
// catch { }
// };
config.RotatingStatuses = !config.RotatingStatuses;
await uow.CompleteAsync();
}
}
// timer.Enabled = NadekoBot.Config.IsRotatingStatus;
// }
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task AddPlaying(IMessage imsg, string status)
{
var channel = (ITextChannel)imsg.Channel;
// public Func<CommandEventArgs, Task> DoFunc() => async e =>
// {
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false);
// try
// {
// if (timer.Enabled)
// timer.Stop();
// else
// timer.Start();
// NadekoBot.Config.IsRotatingStatus = timer.Enabled;
// await ConfigHandler.SaveConfig().ConfigureAwait(false);
// }
// finally {
// playingPlaceholderLock.Release();
// }
// await channel.SendMessageAsync($"❗`Rotating playing status has been {(timer.Enabled ? "enabled" : "disabled")}.`").ConfigureAwait(false);
// };
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(channel.Guild.Id);
config.RotatingStatusMessages.Add(new PlayingStatus { Status = status });
await uow.CompleteAsync();
}
// internal override void Init(CommandGroupBuilder cgb)
// {
// cgb.CreateCommand(Module.Prefix + "rotateplaying")
// .Alias(Module.Prefix + "ropl")
// .Description($"Toggles rotation of playing status of the dynamic strings you specified earlier. **Bot Owner Only!** | `{Prefix}ropl`")
// .AddCheck(SimpleCheckers.OwnerOnly())
// .Do(DoFunc());
}
// cgb.CreateCommand(Module.Prefix + "addplaying")
// .Alias(Module.Prefix + "adpl")
// .Description("Adds a specified string to the list of playing strings to rotate. " +
// "Supported placeholders: " + string.Join(", ", PlayingPlaceholders.Keys)+ $" **Bot Owner Only!**| `{Prefix}adpl`")
// .Parameter("text", ParameterType.Unparsed)
// .AddCheck(SimpleCheckers.OwnerOnly())
// .Do(async e =>
// {
// var arg = text;
// if (string.IsNullOrWhiteSpace(arg))
// return;
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false);
// try
// {
// NadekoBot.Config.RotatingStatuses.Add(arg);
// await ConfigHandler.SaveConfig();
// }
// finally
// {
// playingPlaceholderLock.Release();
// }
// await channel.SendMessageAsync("🆗 `Added a new playing string.`").ConfigureAwait(false);
// });
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task ListPlaying(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
// cgb.CreateCommand(Module.Prefix + "listplaying")
// .Alias(Module.Prefix + "lipl")
// .Description($"Lists all playing statuses with their corresponding number. **Bot Owner Only!**| `{Prefix}lipl`")
// .AddCheck(SimpleCheckers.OwnerOnly())
// .Do(async e =>
// {
// if (NadekoBot.Config.RotatingStatuses.Count == 0)
// await channel.SendMessageAsync("`There are no playing strings. " +
// "Add some with .addplaying [text] command.`").ConfigureAwait(false);
// var sb = new StringBuilder();
// for (var i = 0; i < NadekoBot.Config.RotatingStatuses.Count; i++)
// {
// sb.AppendLine($"`{i + 1}.` {NadekoBot.Config.RotatingStatuses[i]}");
// }
// await channel.SendMessageAsync(sb.ToString()).ConfigureAwait(false);
// });
List<PlayingStatus> statuses;
using (var uow = DbHandler.UnitOfWork())
{
statuses = uow.GuildConfigs.For(channel.Guild.Id).RotatingStatusMessages;
}
// cgb.CreateCommand(Module.Prefix + "removeplaying")
// .Alias(Module.Prefix + "repl", Module.Prefix + "rmpl")
// .Description($"Removes a playing string on a given number. **Bot Owner Only!**| `{Prefix}rmpl`")
// .Parameter("number", ParameterType.Required)
// .AddCheck(SimpleCheckers.OwnerOnly())
// .Do(async e =>
// {
// var arg = number;
// int num;
// string str;
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false);
// try {
// if (!int.TryParse(arg.Trim(), out num) || num <= 0 || num > NadekoBot.Config.RotatingStatuses.Count)
// return;
// str = NadekoBot.Config.RotatingStatuses[num - 1];
// NadekoBot.Config.RotatingStatuses.RemoveAt(num - 1);
// await ConfigHandler.SaveConfig().ConfigureAwait(false);
// }
// finally { playingPlaceholderLock.Release(); }
// await channel.SendMessageAsync($"🆗 `Removed playing string #{num}`({str})").ConfigureAwait(false);
// });
// }
// }
//}
if (!statuses.Any())
await channel.SendMessageAsync("`No rotating playing statuses set.`");
else
{
var i = 1;
await channel.SendMessageAsync($"{imsg.Author.Mention} Here is a list of rotating statuses:\n" + string.Join("\n", statuses.Select(rs => $"`{i++}.` {rs.Status}\n")));
}
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task RemovePlaying(IMessage imsg, int index)
{
var channel = (ITextChannel)imsg.Channel;
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(channel.Guild.Id);
if (index >= config.RotatingStatusMessages.Count)
return;
config.RotatingStatusMessages.RemoveAt(index);
await uow.CompleteAsync();
}
}
}
}
}