Announcements converted?!?!?

This commit is contained in:
Kwoth 2016-08-25 03:19:06 +02:00
parent c09a851f74
commit 7aa3a1a092
3 changed files with 272 additions and 286 deletions

View File

@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Administration
if (conf.AutoAssignRoleId == 0) if (conf.AutoAssignRoleId == 0)
return; return;
var role = user.Guild.Roles.Where(r => r.Id == conf.AutoAssignRoleId).FirstOrDefault(); var role = user.Guild.Roles.FirstOrDefault(r => r.Id == conf.AutoAssignRoleId);
if (role != null) if (role != null)
await user.AddRolesAsync(role); await user.AddRolesAsync(role);
@ -55,7 +55,7 @@ namespace NadekoBot.Modules.Administration
conf.AutoAssignRoleId = role.Id; conf.AutoAssignRoleId = role.Id;
uow.GuildConfigs.Update(conf); uow.GuildConfigs.Update(conf);
await uow.CompleteAsync(); await uow.CompleteAsync().ConfigureAwait(false);
} }
if (role == null) if (role == null)

View File

@ -1,321 +1,290 @@
//using Discord; using Discord;
//using Discord.Commands; using Discord.Commands;
//using NadekoBot.Classes; using NadekoBot.Attributes;
//using System.Collections.Concurrent; using NadekoBot.Classes;
//using System.Linq; using NadekoBot.Services;
//using System.Threading.Tasks; using NadekoBot.Services.Database.Models;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
//// todo DB // todo DB
//// todo rewrite // todo rewrite
//namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
//{ {
// public partial class ServerGreetCommands public partial class ServerGreetCommands
// { {
public static long Greeted = 0;
// public static ConcurrentDictionary<ulong, AnnounceControls> AnnouncementsDictionary; public ServerGreetCommands(DiscordModule module)
{
NadekoBot.Client.UserJoined += UserJoined;
NadekoBot.Client.UserLeft += UserLeft;
}
// public static long Greeted = 0; private async Task UserLeft(IGuildUser user)
{
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(user.Guild.Id);
}
// public ServerGreetCommands(DiscordModule module) if (!conf.SendChannelByeMessage) return;
// { var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
// AnnouncementsDictionary = new ConcurrentDictionary<ulong, AnnounceControls>();
// NadekoBot.Client.UserJoined += UserJoined; if (channel == null) //maybe warn the server owner that the channel is missing
// NadekoBot.Client.UserLeft += UserLeft; return;
// var data = Classes.DbHandler.Instance.GetAllRows<DataModels.Announcement>(); var msg = conf.ChannelByeMessageText.Replace("%user%", "**" + user.Username + "**");
if (string.IsNullOrWhiteSpace(msg))
return;
// if (!data.Any()) return; var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
// foreach (var obj in data) if (conf.AutoDeleteByeMessages)
// AnnouncementsDictionary.TryAdd((ulong)obj.ServerId, new AnnounceControls(obj)); {
// } await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
await toDelete.DeleteAsync().ConfigureAwait(false);
}
}
// private async void UserLeft(object sender, UserEventArgs e) private async Task UserJoined(IGuildUser user)
// { {
// try GuildConfig conf;
// { using (var uow = DbHandler.UnitOfWork())
// if (!AnnouncementsDictionary.ContainsKey(e.Server.Id) || {
// !AnnouncementsDictionary[e.Server.Id].Bye) return; conf = uow.GuildConfigs.For(user.Guild.Id);
}
// var controls = AnnouncementsDictionary[e.Server.Id]; if (conf.SendChannelGreetMessage)
// var channel = NadekoBot.Client.GetChannel(controls.ByeChannel); {
// var msg = controls.ByeText.Replace("%user%", "**" + imsg.Author.Username + "**").Trim(); var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
// if (string.IsNullOrEmpty(msg))
// return;
// if (controls.ByePM) if (channel != null) //maybe warn the server owner that the channel is missing
// { {
// Greeted++;
// try
// {
// await imsg.Author.SendMessageAsync($"`Farewell Message From {e.Server?.Name}`\n" + msg).ConfigureAwait(false);
// } var msg = conf.ChannelGreetMessageText.Replace("%user%", "**" + user.Username + "**");
// catch { } if (!string.IsNullOrWhiteSpace(msg))
// } {
// else var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
// { if (conf.AutoDeleteGreetMessages)
// if (channel == null) return; {
// Greeted++; await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
// var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); await toDelete.DeleteAsync().ConfigureAwait(false);
// if (e.Server.CurrentUser.GetPermissions(channel).ManageMessages && controls.DeleteGreetMessages) }
// { }
// await Task.Delay(30000).ConfigureAwait(false); // 5 minutes }
// await toDelete.Delete().ConfigureAwait(false); }
// }
// }
// }
// catch { }
// }
// private async Task UserJoined(object sender, Discord.UserEventArgs e) if (conf.SendDmGreetMessage)
// { {
// try var channel = await user.CreateDMChannelAsync();
// {
// if (!AnnouncementsDictionary.ContainsKey(e.Server.Id) ||
// !AnnouncementsDictionary[e.Server.Id].Greet) return;
// var controls = AnnouncementsDictionary[e.Server.Id]; if (channel != null)
// var channel = NadekoBot.Client.GetChannel(controls.GreetChannel); {
var msg = conf.DmGreetMessageText.Replace("%user%", "**" + user.Username + "**");
if (!string.IsNullOrWhiteSpace(msg))
{
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
}
}
}
}
// var msg = controls.GreetText.Replace("%user%", imsg.Author.Mention).Trim(); [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// if (string.IsNullOrEmpty(msg)) [RequireContext(ContextType.Guild)]
// return; [RequirePermission(GuildPermission.ManageGuild)]
// if (controls.GreetPM) public async Task GreetDel(IMessage imsg)
// { {
// Greeted++; var channel = (ITextChannel)imsg.Channel;
// await imsg.Author.SendMessageAsync($"`Welcome Message From {e.Server.Name}`\n" + msg).ConfigureAwait(false);
// }
// else
// {
// if (channel == null) return;
// Greeted++;
// var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
// if (e.Server.CurrentUser.GetPermissions(channel).ManageMessages && controls.DeleteGreetMessages)
// {
// await Task.Delay(30000).ConfigureAwait(false); // 5 minutes
// await toDelete.Delete().ConfigureAwait(false);
// }
// }
// }
// catch { }
// }
// public class AnnounceControls GuildConfig conf;
// { using (var uow = DbHandler.UnitOfWork())
// private DataModels.Announcement _model { get; } {
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.AutoDeleteGreetMessages = !conf.AutoDeleteGreetMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
// public bool Greet { if (conf.AutoDeleteGreetMessages)
// get { return _model.Greet; } await channel.SendMessageAsync("`Automatic deletion of greet messages has been enabled.`").ConfigureAwait(false);
// set { _model.Greet = value; Save(); } else
// } await channel.SendMessageAsync("`Automatic deletion of greet messages has been disabled.`").ConfigureAwait(false);
}
// public ulong GreetChannel { [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// get { return (ulong)_model.GreetChannelId; } [RequireContext(ContextType.Guild)]
// set { _model.GreetChannelId = (long)value; Save(); } [RequirePermission(GuildPermission.ManageGuild)]
// } public async Task ByeDel(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
// public bool GreetPM { GuildConfig conf;
// get { return _model.GreetPM; } using (var uow = DbHandler.UnitOfWork())
// set { _model.GreetPM = value; Save(); } {
// } conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.AutoDeleteByeMessages = !conf.AutoDeleteByeMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
// public bool ByePM { if (conf.AutoDeleteByeMessages)
// get { return _model.ByePM; } await channel.SendMessageAsync("`Automatic deletion of bye messages has been enabled.`").ConfigureAwait(false);
// set { _model.ByePM = value; Save(); } else
// } await channel.SendMessageAsync("`Automatic deletion of bye messages has been disabled.`").ConfigureAwait(false);
}
// public string GreetText { [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// get { return _model.GreetText; } [RequireContext(ContextType.Guild)]
// set { _model.GreetText = value; Save(); } [RequirePermission(GuildPermission.ManageGuild)]
// } public async Task Greet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
// public bool Bye { GuildConfig conf;
// get { return _model.Bye; } using (var uow = DbHandler.UnitOfWork())
// set { _model.Bye = value; Save(); } {
// } conf = uow.GuildConfigs.For(channel.Guild.Id);
// public ulong ByeChannel { conf.SendChannelGreetMessage = !conf.SendChannelGreetMessage;
// get { return (ulong)_model.ByeChannelId; } conf.GreetMessageChannelId = channel.Id;
// set { _model.ByeChannelId = (long)value; Save(); } uow.GuildConfigs.Update(conf);
// } await uow.CompleteAsync();
}
// public string ByeText { if (conf.SendChannelGreetMessage)
// get { return _model.ByeText; } await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
// set { _model.ByeText = value; Save(); } else
// } await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
// public ulong ServerId { [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// get { return (ulong)_model.ServerId; } [RequireContext(ContextType.Guild)]
// set { _model.ServerId = (long)value; } [RequirePermission(GuildPermission.ManageGuild)]
// } public async Task DmGreet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
// public bool DeleteGreetMessages { GuildConfig conf;
// get { using (var uow = DbHandler.UnitOfWork())
// return _model.DeleteGreetMessages; {
// } conf = uow.GuildConfigs.For(channel.Guild.Id);
// set { conf.SendDmGreetMessage = !conf.SendDmGreetMessage;
// _model.DeleteGreetMessages = value; Save(); uow.GuildConfigs.Update(conf);
// } await uow.CompleteAsync();
// } }
// public AnnounceControls(DataModels.Announcement model) if (conf.SendDmGreetMessage)
// { await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
// this._model = model; else
// } await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
// public AnnounceControls(ulong serverId) [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// { [RequireContext(ContextType.Guild)]
// this._model = new DataModels.Announcement(); public async Task GreetMsg(IMessage imsg, [Remainder] string text)
// ServerId = serverId; {
// } var channel = (ITextChannel)imsg.Channel;
// internal bool ToggleBye(ulong id) GuildConfig conf;
// { using (var uow = DbHandler.UnitOfWork())
// if (Bye) {
// { conf = uow.GuildConfigs.For(channel.Guild.Id);
// return Bye = false; if (!string.IsNullOrWhiteSpace(text))
// } {
// else conf.ChannelGreetMessageText = text;
// { uow.GuildConfigs.Update(conf);
// ByeChannel = id; await uow.CompleteAsync();
// return Bye = true; }
// } }
// }
// internal bool ToggleGreet(ulong id) if (string.IsNullOrWhiteSpace(text))
// { {
// if (Greet) await channel.SendMessageAsync("`Current greet message:` " + conf.ChannelGreetMessageText);
// { return;
// return Greet = false; }
// } await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false);
// else if (!conf.SendChannelGreetMessage)
// { await channel.SendMessageAsync("Enable greet messsages by typing `.greet`").ConfigureAwait(false);
// GreetChannel = id; }
// return Greet = true;
// }
// }
// internal bool ToggleDelete() => DeleteGreetMessages = !DeleteGreetMessages; [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// internal bool ToggleGreetPM() => GreetPM = !GreetPM; [RequireContext(ContextType.Guild)]
// internal bool ToggleByePM() => ByePM = !ByePM; public async Task DmGreetMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
// private void Save() GuildConfig conf;
// { using (var uow = DbHandler.UnitOfWork())
// Classes.DbHandler.Instance.Save(_model); {
// } conf = uow.GuildConfigs.For(channel.Guild.Id);
// } if (!string.IsNullOrWhiteSpace(text))
{
conf.DmGreetMessageText = text;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
}
// internal override void Init(CommandGroupBuilder cgb) if (string.IsNullOrWhiteSpace(text))
// { {
// cgb.CreateCommand(Module.Prefix + "grdel") await channel.SendMessageAsync("`Current DM greet message:` " + conf.DmGreetMessageText);
// .Description($"Toggles automatic deletion of greet and bye messages. **Needs Manage Server Permissions.**| `{Prefix}grdel`") return;
// .Do(async e => }
// { await channel.SendMessageAsync("New DM greet message set.").ConfigureAwait(false);
// if (!imsg.Author.ServerPermissions.ManageServer) return; if (!conf.SendDmGreetMessage)
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); await channel.SendMessageAsync("Enable DM greet messsages by typing `.greet`").ConfigureAwait(false);
}
// if (ann.ToggleDelete()) [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// await channel.SendMessageAsync("`Automatic deletion of greet and bye messages has been enabled.`").ConfigureAwait(false); [RequireContext(ContextType.Guild)]
// else [RequirePermission(GuildPermission.ManageGuild)]
// await channel.SendMessageAsync("`Automatic deletion of greet and bye messages has been disabled.`").ConfigureAwait(false); public async Task Bye(IMessage imsg)
// }); {
var channel = (ITextChannel)imsg.Channel;
// cgb.CreateCommand(Module.Prefix + "greet") GuildConfig conf;
// .Description($"Toggles anouncements on the current channel when someone joins the server. **Needs Manage Server Permissions.**| `{Prefix}greet`") using (var uow = DbHandler.UnitOfWork())
// .Do(async e => {
// { conf = uow.GuildConfigs.For(channel.Guild.Id);
// if (!imsg.Author.ServerPermissions.ManageServer) return; conf.SendChannelByeMessage = !conf.SendChannelByeMessage;
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id)); conf.ByeMessageChannelId = channel.Id;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
// if (ann.ToggleGreet(e.Channel.Id)) if (conf.SendChannelByeMessage)
// await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false); await channel.SendMessageAsync("Bye announcements enabled on this channel.").ConfigureAwait(false);
// else else
// await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false); await channel.SendMessageAsync("Bye announcements disabled.").ConfigureAwait(false);
// }); }
// cgb.CreateCommand(Module.Prefix + "greetmsg") [LocalizedCommand, LocalizedDescription, LocalizedSummary]
// .Description($"Sets a new join announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current greet message. **Needs Manage Server Permissions.**| `{Prefix}greetmsg Welcome to the server, %user%.`") [RequireContext(ContextType.Guild)]
// .Parameter("msg", ParameterType.Unparsed) public async Task ByeMsg(IMessage imsg, [Remainder] string text)
// .Do(async e => {
// { var channel = (ITextChannel)imsg.Channel;
// if (!imsg.Author.ServerPermissions.ManageServer) return;
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id));
// if (string.IsNullOrWhiteSpace(msg))
// {
// await channel.SendMessageAsync("`Current greet message:` " + ann.GreetText);
// return;
// }
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{
conf.ChannelByeMessageText = text;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
}
// ann.GreetText = msg; if (string.IsNullOrWhiteSpace(text))
// await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false); {
// if (!ann.Greet) await channel.SendMessageAsync("`Current bye message:` " + conf.ChannelGreetMessageText);
// await channel.SendMessageAsync("Enable greet messsages by typing `.greet`").ConfigureAwait(false); return;
// }); }
await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false);
// cgb.CreateCommand(Module.Prefix + "bye") if (!conf.SendChannelByeMessage)
// .Description($"Toggles anouncements on the current channel when someone leaves the server. | `{Prefix}bye`") await channel.SendMessageAsync("Enable bye messsages by typing `.bye`").ConfigureAwait(false);
// .Do(async e => }
// { }
// if (!imsg.Author.ServerPermissions.ManageServer) return; }
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id));
// if (ann.ToggleBye(e.Channel.Id))
// await channel.SendMessageAsync("Bye announcements enabled on this channel.").ConfigureAwait(false);
// else
// await channel.SendMessageAsync("Bye announcements disabled.").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "byemsg")
// .Description($"Sets a new leave announcement message. Type %user% if you want to mention the new member. Using it with no message will show the current bye message. **Needs Manage Server Permissions.**| `{Prefix}byemsg %user% has left the server.`")
// .Parameter("msg", ParameterType.Unparsed)
// .Do(async e =>
// {
// if (!imsg.Author.ServerPermissions.ManageServer) return;
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id));
// if (string.IsNullOrWhiteSpace(msg))
// {
// await channel.SendMessageAsync("`Current bye message:` " + ann.ByeText);
// return;
// }
// ann.ByeText = msg;
// await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false);
// if (!ann.Bye)
// await channel.SendMessageAsync("Enable bye messsages by typing `.bye`.").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "byepm")
// .Description($"Toggles whether the good bye messages will be sent in a PM or in the text channel. **Needs Manage Server Permissions.**| `{Prefix}byepm`")
// .Do(async e =>
// {
// if (!imsg.Author.ServerPermissions.ManageServer) return;
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id));
// if (ann.ToggleByePM())
// await channel.SendMessageAsync("Bye messages will be sent in a PM from now on.\n ⚠ Keep in mind this might fail if the user and the bot have no common servers after the user leaves.").ConfigureAwait(false);
// else
// await channel.SendMessageAsync("Bye messages will be sent in a bound channel from now on.").ConfigureAwait(false);
// if (!ann.Bye)
// await channel.SendMessageAsync("Enable bye messsages by typing `.bye`, and set the bye message using `.byemsg`").ConfigureAwait(false);
// });
// cgb.CreateCommand(Module.Prefix + "greetpm")
// .Description($"Toggles whether the greet messages will be sent in a PM or in the text channel. **Needs Manage Server Permissions.**| `{Prefix}greetpm`")
// .Do(async e =>
// {
// if (!imsg.Author.ServerPermissions.ManageServer) return;
// var ann = AnnouncementsDictionary.GetOrAdd(e.Server.Id, new AnnounceControls(e.Server.Id));
// if (ann.ToggleGreetPM())
// await channel.SendMessageAsync("Greet messages will be sent in a PM from now on.").ConfigureAwait(false);
// else
// await channel.SendMessageAsync("Greet messages will be sent in a bound channel from now on.").ConfigureAwait(false);
// if (!ann.Greet)
// await channel.SendMessageAsync("Enable greet messsages by typing `.greet`, and set the greet message using `.greetmsg`").ConfigureAwait(false);
// });
// }
// }
//}

View File

@ -11,5 +11,22 @@ namespace NadekoBot.Services.Database.Models
public ulong GuildId { get; set; } public ulong GuildId { get; set; }
public bool DeleteMessageOnCommand { get; set; } public bool DeleteMessageOnCommand { get; set; }
public ulong AutoAssignRoleId { get; set; } public ulong AutoAssignRoleId { get; set; }
//greet stuff
public bool AutoDeleteGreetMessages { get; set; }
public bool AutoDeleteByeMessages { get; set; }
public int AutoDeleteGreetMessagesTimer { get; set; } = 30;
public ulong GreetMessageChannelId { get; set; }
public ulong ByeMessageChannelId { get; set; }
public bool SendDmGreetMessage { get; set; }
public string DmGreetMessageText { get; set; } = "Welcome to the %server% server, %user%!";
public bool SendChannelGreetMessage { get; set; }
public string ChannelGreetMessageText { get; set; } = "Welcome to the %server% server, %user%!";
public bool SendChannelByeMessage { get; set; }
public string ChannelByeMessageText { get; set; } = "Welcome to the %server% server, %user%!";
} }
} }