diff --git a/NadekoBot/Commands/ServerGreetCommand.cs b/NadekoBot/Commands/ServerGreetCommand.cs new file mode 100644 index 00000000..5d8ea590 --- /dev/null +++ b/NadekoBot/Commands/ServerGreetCommand.cs @@ -0,0 +1,231 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using System.Collections.Concurrent; +using NadekoBot.Extensions; +using Discord; +using Parse; +using System.ComponentModel; + +/* Voltana's legacy +public class AsyncLazy : Lazy> +{ + public AsyncLazy(Func valueFactory) : + base(() => Task.Factory.StartNew(valueFactory)) { } + + public AsyncLazy(Func> taskFactory) : + base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap()) { } + + public TaskAwaiter GetAwaiter() { return Value.GetAwaiter(); } +} +*/ + +namespace NadekoBot.Commands { + class ServerGreetCommand : DiscordCommand { + + public static ConcurrentDictionary AnnouncementsDictionary; + + public ServerGreetCommand() : base() { + AnnouncementsDictionary = new ConcurrentDictionary(); + + NadekoBot.client.UserJoined += UserJoined; + NadekoBot.client.UserLeft += UserLeft; + + var data = new ParseQuery("Announcements") + .FindAsync() + .Result; + if(data.Any()) + foreach (var po in data) + AnnouncementsDictionary.TryAdd(po.Get("serverId"), new AnnounceControls(po.Get("serverId")).Initialize(po)); + } + + private async void UserLeft(object sender, UserEventArgs e) { + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id) || + !AnnouncementsDictionary[e.Server.Id].Bye) return; + + var controls = AnnouncementsDictionary[e.Server.Id]; + var channel = NadekoBot.client.GetChannel(controls.ByeChannel); + if (channel == null) return; + + await channel.Send(controls.ByeText.Replace("%user%", e.User.Mention)); + } + + private async void UserJoined(object sender, Discord.UserEventArgs e) { + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id) || + !AnnouncementsDictionary[e.Server.Id].Greet) return; + + var controls = AnnouncementsDictionary[e.Server.Id]; + var channel = NadekoBot.client.GetChannel(controls.GreetChannel); + if (channel == null) return; + + await channel.Send(controls.GreetText.Replace("%user%", e.User.Mention)); + } + + public class AnnounceControls { + private ParseObject ParseObj = null; + + private bool greet; + + public bool Greet { + get { return greet; } + set { greet = value; Save(); } + } + + private ulong greetChannel; + + public ulong GreetChannel { + get { return greetChannel; } + set { greetChannel = value; } + } + + private string greetText = "Welcome to the server %user%"; + public string GreetText { + get { return greetText; } + set { greetText = value; Save(); } + } + + private bool bye; + + public bool Bye { + get { return bye; } + set { bye = value; Save(); } + } + + private ulong byeChannel; + + public ulong ByeChannel { + get { return byeChannel; } + set { byeChannel = value; } + } + + private string byeText = "%user% has left the server"; + public string ByeText { + get { return byeText; } + set { byeText = value; Save(); } + } + + + public ulong ServerId { get; } + + public AnnounceControls(ulong serverId) { + this.ServerId = serverId; + } + + internal bool ToggleBye(ulong id) { + if (Bye) { + return Bye = false; + } else { + ByeChannel = id; + return Bye = true; + } + } + + internal bool ToggleGreet(ulong id) { + if (Greet) { + return Greet = false; + } else { + GreetChannel = id; + return Greet = true; + } + } + + private void Save() { + ParseObject p = null; + if (this.ParseObj != null) + p = ParseObj; + else + p = ParseObj = new ParseObject("Announcements"); + p["greet"] = greet; + p["greetText"] = greetText; + p["greetChannel"] = greetChannel; + + p["bye"] = bye; + p["byeText"] = byeText; + p["byeChannel"] = byeChannel; + + p["serverId"] = ServerId; + + p.SaveAsync(); + } + + internal AnnounceControls Initialize(ParseObject po) { + greet = po.Get("greet"); + greetText = po.Get("greetText"); + greetChannel = po.Get("greetChannel"); + + bye = po.Get("bye"); + byeText = po.Get("byeText"); + byeChannel = po.Get("byeChannel"); + + this.ParseObj = po; + return this; + } + } + + public override Func DoFunc() { + throw new NotImplementedException(); + } + + public override void Init(CommandGroupBuilder cgb) { + + cgb.CreateCommand(".greet") + .Description("Enables or Disables anouncements on the current channel when someone joins the server.") + .Do(async e => { + if (!e.User.ServerPermissions.ManageServer) return; + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id)) + AnnouncementsDictionary.TryAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); + + var controls = AnnouncementsDictionary[e.Server.Id]; + + if (controls.ToggleGreet(e.Channel.Id)) + await e.Send("Greet announcements enabled on this channel."); + else + await e.Send("Greet announcements disabled."); + }); + + cgb.CreateCommand(".greetmsg") + .Description("Sets a new announce message. Type %user% if you want to mention the new member.\n**Usage**: .greetmsg Welcome to the server, %user%.") + .Parameter("msg", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.ManageServer) return; + if (e.GetArg("msg") == null) return; + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id)) + AnnouncementsDictionary.TryAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); + + AnnouncementsDictionary[e.Server.Id].GreetText = e.GetArg("msg"); + await e.Send("New greet message set."); + }); + + cgb.CreateCommand(".bye") + .Description("Enables or Disables anouncements on the current channel when someone leaves the server.") + .Do(async e => { + if (!e.User.ServerPermissions.ManageServer) return; + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id)) + AnnouncementsDictionary.TryAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); + + var controls = AnnouncementsDictionary[e.Server.Id]; + + if (controls.ToggleBye(e.Channel.Id)) + await e.Send("Bye announcements enabled on this channel."); + else + await e.Send("Bye announcements disabled."); + }); + + cgb.CreateCommand(".byemsg") + .Description("Sets a new announce leave message. Type %user% if you want to mention the new member.\n**Usage**: .byemsg %user% has left the server.") + .Parameter("msg", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.ManageServer) return; + if (e.GetArg("msg") == null) return; + if (!AnnouncementsDictionary.ContainsKey(e.Server.Id)) + AnnouncementsDictionary.TryAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); + + AnnouncementsDictionary[e.Server.Id].ByeText = e.GetArg("msg"); + await e.Send("New bye message set."); + }); + } + } +} diff --git a/NadekoBot/Commands/SpeedTyping.cs b/NadekoBot/Commands/SpeedTyping.cs index e8c35539..5cd8d7d7 100644 --- a/NadekoBot/Commands/SpeedTyping.cs +++ b/NadekoBot/Commands/SpeedTyping.cs @@ -8,6 +8,7 @@ using Discord.Commands; using NadekoBot.Extensions; using System.Threading; using System.Diagnostics; +using Parse; namespace NadekoBot { diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 6f2dc62f..0bba2021 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Timers; using NadekoBot.Extensions; using System.Threading.Tasks; +using NadekoBot.Commands; namespace NadekoBot.Modules { @@ -14,6 +15,7 @@ namespace NadekoBot.Modules { public Administration() : base() { commands.Add(new HelpCommand()); + commands.Add(new ServerGreetCommand()); } public override void Install(ModuleManager manager) @@ -62,7 +64,6 @@ namespace NadekoBot.Modules await e.Send("You failed to supply a valid username"); return; } - var role = e.Server.FindRoles(e.GetArg("role_name")).FirstOrDefault(); if (role == null) { await e.Send("You failed to supply a valid role"); @@ -269,14 +270,23 @@ namespace NadekoBot.Modules }); cgb.CreateCommand(".die") - .Description("Works only for the owner. Shuts the bot down.") + .Alias(".graceful") + .Parameter("reason",ParameterType.Unparsed) + .Description("Works only for the owner. Shuts the bot down and notifies users about the restart.") .Do(async e => { if (e.User.Id == NadekoBot.OwnerID) { Timer t = new Timer(); t.Interval = 2000; t.Elapsed += (s, ev) => { Environment.Exit(0); }; t.Start(); - await e.Send("Shutting down."); + string reason = e.GetArg("reason"); + if (reason == null) + reason = "Unspecified."; + foreach (var kvp in Music.musicPlayers) { + if(kvp.Value?.CurrentSong?.Channel!=null) + await kvp.Value.CurrentSong.Channel.Send($"Owner initiated a shutdown, sorry for the interruption.\nReason: `{reason}`"); + } + await e.Send("`Shutting down.`"); } }); @@ -314,66 +324,6 @@ namespace NadekoBot.Modules - cgb.CreateCommand(".greet") - .Description("Enables or Disables anouncements on the current channel when someone joins the server.") - .Do(async e => { - if (e.User.Id != NadekoBot.OwnerID) return; - announcingGreet = !announcingGreet; - - if (announcingGreet) { - announceChannel = e.Channel; - joinServer = e.Server; - NadekoBot.client.UserJoined += Client_UserJoined; - await e.Send("Greet announcements enabled on this channel."); - } else { - announceChannel = null; - joinServer = null; - NadekoBot.client.UserJoined -= Client_UserJoined; - await e.Send("Announcements disabled."); - } - }); - //todo add greet/bye for everyone - cgb.CreateCommand(".greetmsg") - .Description("Sets a new announce message. Type %user% if you want to mention the new member.\n**Usage**: .greetmsg Welcome to the server, %user%.") - .Parameter("msg", ParameterType.Unparsed) - .Do(async e => { - if (e.User.Id != NadekoBot.OwnerID) return; - - if (e.GetArg("msg") == null) return; - announceMsg = e.GetArg("msg"); - await e.Send("New greet message set."); - }); - - cgb.CreateCommand(".bye") - .Description("Enables or Disables anouncements on the current channel when someone leaves the server.") - .Do(async e => { - if (e.User.Id != NadekoBot.OwnerID) return; - announcingLeave = !announcingLeave; - - if (announcingLeave) { - announceLeaveChannel = e.Channel; - leaveServer = e.Server; - NadekoBot.client.UserLeft += Client_UserLeft; - await e.Send("Leave announcements enabled on this channel."); - } else { - announceLeaveChannel = null; - leaveServer = null; - NadekoBot.client.UserLeft -= Client_UserLeft; - await e.Send("Leave announcements disabled."); - } - }); - - cgb.CreateCommand(".byemsg") - .Description("Sets a new announce leave message. Type %user% if you want to mention the new member.\n**Usage**: .byemsg %user% has left the server.") - .Parameter("msg", ParameterType.Unparsed) - .Do(async e => { - if (e.User.Id != NadekoBot.OwnerID) return; - - if (e.GetArg("msg") == null) return; - announceLeaveMsg = e.GetArg("msg"); - await e.Send("New bye message set."); - }); - cgb.CreateCommand(".checkmyperms") .Description("Checks your userspecific permissions on this channel.") .Do(async e => { @@ -424,7 +374,6 @@ namespace NadekoBot.Modules await e.Send("Sending failed."); } }); - }); } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index e8f168b8..b14e96f1 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -122,11 +122,12 @@ namespace NadekoBot.Modules { .Alias("yq") .Description("Queue a song using keywords or link. **You must be in a voice channel**.\n**Usage**: `!m q Dream Of Venice`") .Parameter("Query", ParameterType.Unparsed) - .Do(e => { + .Do(async e => { if (musicPlayers.ContainsKey(e.Server) == false) musicPlayers.TryAdd(e.Server, new MusicControls()); var player = musicPlayers[e.Server]; player.SongQueue.Add(new StreamRequest(NadekoBot.client, e, e.GetArg("Query"))); + await e.Send(":warning: Music is unstable atm, working on a fix. :warning:"); }); cgb.CreateCommand("lq") diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index c5522e4c..c3e6a067 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -24,7 +24,7 @@ namespace NadekoBot public static string password; public static string TrelloAppKey; public static bool ForwardMessages = false; - public static string BotVersion = "0.7-beta2"; + public static string BotVersion = "0.7-beta3"; static void Main() { diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 03f1eab3..88491a59 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -141,6 +141,7 @@ +