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

@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl;
namespace NadekoBot.Migrations namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoSqliteContext))] [DbContext(typeof(NadekoSqliteContext))]
[Migration("20160826172044_first")] [Migration("20160826234229_first")]
partial class first partial class first
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -110,6 +110,8 @@ namespace NadekoBot.Migrations
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");
b.Property<bool>("RotatingStatuses");
b.Property<bool>("SendChannelByeMessage"); b.Property<bool>("SendChannelByeMessage");
b.Property<bool>("SendChannelGreetMessage"); b.Property<bool>("SendChannelGreetMessage");
@ -124,6 +126,22 @@ namespace NadekoBot.Migrations
b.ToTable("GuildConfigs"); b.ToTable("GuildConfigs");
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.PlayingStatus", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int?>("GuildConfigId");
b.Property<string>("Status");
b.HasKey("Id");
b.HasIndex("GuildConfigId");
b.ToTable("PlayingStatus");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b => modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -193,6 +211,13 @@ namespace NadekoBot.Migrations
.HasForeignKey("ClashWarId") .HasForeignKey("ClashWarId")
.OnDelete(DeleteBehavior.Cascade); .OnDelete(DeleteBehavior.Cascade);
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.PlayingStatus", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("RotatingStatusMessages")
.HasForeignKey("GuildConfigId");
});
} }
} }
} }

View File

@ -60,6 +60,7 @@ namespace NadekoBot.Migrations
ExclusiveSelfAssignedRoles = table.Column<bool>(nullable: false), ExclusiveSelfAssignedRoles = table.Column<bool>(nullable: false),
GreetMessageChannelId = table.Column<ulong>(nullable: false), GreetMessageChannelId = table.Column<ulong>(nullable: false),
GuildId = table.Column<ulong>(nullable: false), GuildId = table.Column<ulong>(nullable: false),
RotatingStatuses = table.Column<bool>(nullable: false),
SendChannelByeMessage = table.Column<bool>(nullable: false), SendChannelByeMessage = table.Column<bool>(nullable: false),
SendChannelGreetMessage = table.Column<bool>(nullable: false), SendChannelGreetMessage = table.Column<bool>(nullable: false),
SendDmGreetMessage = table.Column<bool>(nullable: false) SendDmGreetMessage = table.Column<bool>(nullable: false)
@ -141,6 +142,26 @@ namespace NadekoBot.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "PlayingStatus",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
GuildConfigId = table.Column<int>(nullable: true),
Status = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_PlayingStatus", x => x.Id);
table.ForeignKey(
name: "FK_PlayingStatus_GuildConfigs_GuildConfigId",
column: x => x.GuildConfigId,
principalTable: "GuildConfigs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_ClashCallers_ClashWarId", name: "IX_ClashCallers_ClashWarId",
table: "ClashCallers", table: "ClashCallers",
@ -158,6 +179,11 @@ namespace NadekoBot.Migrations
column: "GuildId", column: "GuildId",
unique: true); unique: true);
migrationBuilder.CreateIndex(
name: "IX_PlayingStatus_GuildConfigId",
table: "PlayingStatus",
column: "GuildConfigId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_SelfAssignableRoles_GuildId_RoleId", name: "IX_SelfAssignableRoles_GuildId_RoleId",
table: "SelfAssignableRoles", table: "SelfAssignableRoles",
@ -174,7 +200,7 @@ namespace NadekoBot.Migrations
name: "Donators"); name: "Donators");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "GuildConfigs"); name: "PlayingStatus");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Quotes"); name: "Quotes");
@ -187,6 +213,9 @@ namespace NadekoBot.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ClashOfClans"); name: "ClashOfClans");
migrationBuilder.DropTable(
name: "GuildConfigs");
} }
} }
} }

View File

@ -109,6 +109,8 @@ namespace NadekoBot.Migrations
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");
b.Property<bool>("RotatingStatuses");
b.Property<bool>("SendChannelByeMessage"); b.Property<bool>("SendChannelByeMessage");
b.Property<bool>("SendChannelGreetMessage"); b.Property<bool>("SendChannelGreetMessage");
@ -123,6 +125,22 @@ namespace NadekoBot.Migrations
b.ToTable("GuildConfigs"); b.ToTable("GuildConfigs");
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.PlayingStatus", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int?>("GuildConfigId");
b.Property<string>("Status");
b.HasKey("Id");
b.HasIndex("GuildConfigId");
b.ToTable("PlayingStatus");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b => modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -192,6 +210,13 @@ namespace NadekoBot.Migrations
.HasForeignKey("ClashWarId") .HasForeignKey("ClashWarId")
.OnDelete(DeleteBehavior.Cascade); .OnDelete(DeleteBehavior.Cascade);
}); });
modelBuilder.Entity("NadekoBot.Services.Database.Models.PlayingStatus", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.GuildConfig")
.WithMany("RotatingStatusMessages")
.HasForeignKey("GuildConfigId");
});
} }
} }
} }

View File

@ -1,168 +1,113 @@
//using Discord.Commands; using Discord;
//using NadekoBot.Classes; using Discord.Commands;
//using NadekoBot.Classes.JSONModels; using Discord.WebSocket;
//using NadekoBot.Modules.Music; using NadekoBot.Attributes;
//using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Services;
//using System; using NadekoBot.Services.Database;
//using System.Collections.Generic; using NadekoBot.Services.Database.Models;
//using System.Linq; using System;
//using System.Text; using System.Collections.Generic;
//using System.Threading; using System.Linq;
//using System.Threading.Tasks; using System.Text;
//using System.Timers; using System.Threading;
//using Timer = System.Timers.Timer; using System.Threading.Tasks;
////todo DB //todo owner only
//namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
//{ {
// internal class PlayingRotate : DiscordCommand public partial class Administration
// { {
// private static readonly Timer timer = new Timer(20000); [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; } = [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// new Dictionary<string, Func<string>> { [RequireContext(ContextType.Guild)]
// {"%servers%", () => NadekoBot.Client.Servers.Count().ToString()}, public async Task RotatePlaying(IMessage imsg)
// {"%users%", () => NadekoBot.Client.Servers.SelectMany(s => s.Users).Count().ToString()}, {
// {"%playing%", () => { var channel = (ITextChannel)imsg.Channel;
// 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()}
// };
// 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) config.RotatingStatuses = !config.RotatingStatuses;
// { await uow.CompleteAsync();
// 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 { }
// };
// 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 => using (var uow = DbHandler.UnitOfWork())
// { {
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false); var config = uow.GuildConfigs.For(channel.Guild.Id);
// try config.RotatingStatusMessages.Add(new PlayingStatus { Status = status });
// { await uow.CompleteAsync();
// 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);
// };
// 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") [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// .Alias(Module.Prefix + "adpl") [RequireContext(ContextType.Guild)]
// .Description("Adds a specified string to the list of playing strings to rotate. " + public async Task ListPlaying(IMessage imsg)
// "Supported placeholders: " + string.Join(", ", PlayingPlaceholders.Keys)+ $" **Bot Owner Only!**| `{Prefix}adpl`") {
// .Parameter("text", ParameterType.Unparsed) var channel = (ITextChannel)imsg.Channel;
// .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);
// });
// cgb.CreateCommand(Module.Prefix + "listplaying") List<PlayingStatus> statuses;
// .Alias(Module.Prefix + "lipl") using (var uow = DbHandler.UnitOfWork())
// .Description($"Lists all playing statuses with their corresponding number. **Bot Owner Only!**| `{Prefix}lipl`") {
// .AddCheck(SimpleCheckers.OwnerOnly()) statuses = uow.GuildConfigs.For(channel.Guild.Id).RotatingStatusMessages;
// .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);
// });
// cgb.CreateCommand(Module.Prefix + "removeplaying") if (!statuses.Any())
// .Alias(Module.Prefix + "repl", Module.Prefix + "rmpl") await channel.SendMessageAsync("`No rotating playing statuses set.`");
// .Description($"Removes a playing string on a given number. **Bot Owner Only!**| `{Prefix}rmpl`") else
// .Parameter("number", ParameterType.Required) {
// .AddCheck(SimpleCheckers.OwnerOnly()) var i = 1;
// .Do(async e => await channel.SendMessageAsync($"{imsg.Author.Mention} Here is a list of rotating statuses:\n" + string.Join("\n", statuses.Select(rs => $"`{i++}.` {rs.Status}\n")));
// { }
// var arg = number;
// int num; }
// string str;
// await playingPlaceholderLock.WaitAsync().ConfigureAwait(false); [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// try { [RequireContext(ContextType.Guild)]
// if (!int.TryParse(arg.Trim(), out num) || num <= 0 || num > NadekoBot.Config.RotatingStatuses.Count) public async Task RemovePlaying(IMessage imsg, int index)
// return; {
// str = NadekoBot.Config.RotatingStatuses[num - 1]; var channel = (ITextChannel)imsg.Channel;
// NadekoBot.Config.RotatingStatuses.RemoveAt(num - 1);
// await ConfigHandler.SaveConfig().ConfigureAwait(false); using (var uow = DbHandler.UnitOfWork())
// } {
// finally { playingPlaceholderLock.Release(); } var config = uow.GuildConfigs.For(channel.Guild.Id);
// await channel.SendMessageAsync($"🆗 `Removed playing string #{num}`({str})").ConfigureAwait(false);
// }); if (index >= config.RotatingStatusMessages.Count)
// } return;
// }
//} config.RotatingStatusMessages.RemoveAt(index);
await uow.CompleteAsync();
}
}
}
}
}

View File

@ -31,5 +31,12 @@ namespace NadekoBot.Services.Database.Models
//self assignable roles //self assignable roles
public bool ExclusiveSelfAssignedRoles { get; set; } public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; } public bool AutoDeleteSelfAssignedRoleMessages { get; set; }
public bool RotatingStatuses { get; set; }
public List<PlayingStatus> RotatingStatusMessages { get; set; }
}
public class PlayingStatus : DbEntity
{
public string Status { get; set; }
} }
} }