Greet commands now function without delay?

This commit is contained in:
Kwoth 2016-08-25 16:41:06 +02:00
parent f552e03e24
commit 612eb22154

View File

@ -26,19 +26,19 @@ namespace NadekoBot.Modules.Administration
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
} }
private async Task UserLeft(IGuildUser user) private Task UserLeft(IGuildUser user)
{
var leftTask = Task.Run(async () =>
{ {
_log.Info("Left: User Left");
GuildConfig conf; GuildConfig conf;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
conf = uow.GuildConfigs.For(user.Guild.Id); conf = uow.GuildConfigs.For(user.Guild.Id);
} }
_log.Info("Left: Got unit of work");
if (!conf.SendChannelByeMessage) return; if (!conf.SendChannelByeMessage) return;
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId); var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
_log.Info("Left: Found channel");
if (channel == null) //maybe warn the server owner that the channel is missing if (channel == null) //maybe warn the server owner that the channel is missing
return; return;
@ -46,45 +46,45 @@ namespace NadekoBot.Modules.Administration
if (string.IsNullOrWhiteSpace(msg)) if (string.IsNullOrWhiteSpace(msg))
return; return;
_log.Info("Left: Sending");
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
if (conf.AutoDeleteByeMessages) if (conf.AutoDeleteByeMessages)
{ {
_log.Info("Left: Sent, waiting for delete"); var t = Task.Run(async () =>
{
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
_log.Info("Left: Deleted");
await toDelete.DeleteAsync().ConfigureAwait(false); await toDelete.DeleteAsync().ConfigureAwait(false);
});
} }
});
return Task.CompletedTask;
} }
private async Task UserJoined(IGuildUser user) private Task UserJoined(IGuildUser user)
{
var joinedTask = Task.Run(async () =>
{ {
_log.Info("Joined: User joined");
GuildConfig conf; GuildConfig conf;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
conf = uow.GuildConfigs.For(user.Guild.Id); conf = uow.GuildConfigs.For(user.Guild.Id);
} }
_log.Info("Joined: Got unit of work");
if (conf.SendChannelGreetMessage) if (conf.SendChannelGreetMessage)
{ {
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId); var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
_log.Info("Joined: Found required channel");
if (channel != null) //maybe warn the server owner that the channel is missing if (channel != null) //maybe warn the server owner that the channel is missing
{ {
var msg = conf.ChannelGreetMessageText.Replace("%user%", "**" + user.Username + "**"); var msg = conf.ChannelGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name);
if (!string.IsNullOrWhiteSpace(msg)) if (!string.IsNullOrWhiteSpace(msg))
{ {
_log.Info("Joined: Sending message");
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
_log.Info("Joined: Message sent");
if (conf.AutoDeleteGreetMessages) if (conf.AutoDeleteGreetMessages)
{ {
_log.Info("Joined: Waiting to delete"); var t = Task.Run(async () =>
{
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
await toDelete.DeleteAsync().ConfigureAwait(false); await toDelete.DeleteAsync().ConfigureAwait(false);
_log.Info("Joined: Deleted"); });
} }
} }
} }
@ -96,13 +96,15 @@ namespace NadekoBot.Modules.Administration
if (channel != null) if (channel != null)
{ {
var msg = conf.DmGreetMessageText.Replace("%user%", "**" + user.Username + "**"); var msg = conf.DmGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name);
if (!string.IsNullOrWhiteSpace(msg)) if (!string.IsNullOrWhiteSpace(msg))
{ {
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); await channel.SendMessageAsync(msg).ConfigureAwait(false);
} }
} }
} }
});
return Task.CompletedTask;
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary] [LocalizedCommand, LocalizedDescription, LocalizedSummary]
@ -227,7 +229,7 @@ namespace NadekoBot.Modules.Administration
} }
await channel.SendMessageAsync("New DM greet message set.").ConfigureAwait(false); await channel.SendMessageAsync("New DM greet message set.").ConfigureAwait(false);
if (!conf.SendDmGreetMessage) if (!conf.SendDmGreetMessage)
await channel.SendMessageAsync("Enable DM greet messsages by typing `.greet`").ConfigureAwait(false); await channel.SendMessageAsync("Enable DM greet messsages by typing `.greetdm`").ConfigureAwait(false);
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary] [LocalizedCommand, LocalizedDescription, LocalizedSummary]