Announcements are now on parse and persist restarts

Also added reason for .die
This commit is contained in:
Master Kwoth 2016-01-24 13:22:44 +01:00
parent a60f46578d
commit 8194e9aed6
6 changed files with 249 additions and 66 deletions

View File

@ -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<T> : Lazy<Task<T>>
{
public AsyncLazy(Func<T> valueFactory) :
base(() => Task.Factory.StartNew(valueFactory)) { }
public AsyncLazy(Func<Task<T>> taskFactory) :
base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap()) { }
public TaskAwaiter<T> GetAwaiter() { return Value.GetAwaiter(); }
}
*/
namespace NadekoBot.Commands {
class ServerGreetCommand : DiscordCommand {
public static ConcurrentDictionary<ulong, AnnounceControls> AnnouncementsDictionary;
public ServerGreetCommand() : base() {
AnnouncementsDictionary = new ConcurrentDictionary<ulong, AnnounceControls>();
NadekoBot.client.UserJoined += UserJoined;
NadekoBot.client.UserLeft += UserLeft;
var data = new ParseQuery<ParseObject>("Announcements")
.FindAsync()
.Result;
if(data.Any())
foreach (var po in data)
AnnouncementsDictionary.TryAdd(po.Get<ulong>("serverId"), new AnnounceControls(po.Get<ulong>("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<bool>("greet");
greetText = po.Get<string>("greetText");
greetChannel = po.Get<ulong>("greetChannel");
bye = po.Get<bool>("bye");
byeText = po.Get<string>("byeText");
byeChannel = po.Get<ulong>("byeChannel");
this.ParseObj = po;
return this;
}
}
public override Func<CommandEventArgs, Task> 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.");
});
}
}
}

View File

@ -8,6 +8,7 @@ using Discord.Commands;
using NadekoBot.Extensions;
using System.Threading;
using System.Diagnostics;
using Parse;
namespace NadekoBot {

View File

@ -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.");
}
});
});
}

View File

@ -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")

View File

@ -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()
{

View File

@ -141,6 +141,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\SParser.cs" />
<Compile Include="Commands\ServerGreetCommand.cs" />
<Compile Include="Commands\SpeedTyping.cs" />
<Compile Include="Classes\_JSONModels.cs" />
<Compile Include="Classes\Cards.cs" />