Announcement commands are not visible

This commit is contained in:
Kwoth 2016-08-25 14:38:38 +02:00
parent 7aa3a1a092
commit 840c3d86f4

View File

@ -8,283 +8,284 @@ using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
// todo DB
// todo rewrite
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
{ {
public partial class ServerGreetCommands public partial class Administration
{ {
public static long Greeted = 0; public class ServerGreetCommands
public ServerGreetCommands(DiscordModule module)
{ {
NadekoBot.Client.UserJoined += UserJoined; public static long Greeted = 0;
NadekoBot.Client.UserLeft += UserLeft;
}
private async Task UserLeft(IGuildUser user) public ServerGreetCommands(DiscordModule module)
{
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{ {
conf = uow.GuildConfigs.For(user.Guild.Id); NadekoBot.Client.UserJoined += UserJoined;
NadekoBot.Client.UserLeft += UserLeft;
} }
if (!conf.SendChannelByeMessage) return; private async Task UserLeft(IGuildUser user)
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
if (channel == null) //maybe warn the server owner that the channel is missing
return;
var msg = conf.ChannelByeMessageText.Replace("%user%", "**" + user.Username + "**");
if (string.IsNullOrWhiteSpace(msg))
return;
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
if (conf.AutoDeleteByeMessages)
{ {
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes GuildConfig conf;
await toDelete.DeleteAsync().ConfigureAwait(false); using (var uow = DbHandler.UnitOfWork())
}
}
private async Task UserJoined(IGuildUser user)
{
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(user.Guild.Id);
}
if (conf.SendChannelGreetMessage)
{
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
if (channel != null) //maybe warn the server owner that the channel is missing
{ {
conf = uow.GuildConfigs.For(user.Guild.Id);
}
var msg = conf.ChannelGreetMessageText.Replace("%user%", "**" + user.Username + "**"); if (!conf.SendChannelByeMessage) return;
if (!string.IsNullOrWhiteSpace(msg)) var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
if (channel == null) //maybe warn the server owner that the channel is missing
return;
var msg = conf.ChannelByeMessageText.Replace("%user%", "**" + user.Username + "**");
if (string.IsNullOrWhiteSpace(msg))
return;
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
if (conf.AutoDeleteByeMessages)
{
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
await toDelete.DeleteAsync().ConfigureAwait(false);
}
}
private async Task UserJoined(IGuildUser user)
{
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(user.Guild.Id);
}
if (conf.SendChannelGreetMessage)
{
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
if (channel != null) //maybe warn the server owner that the channel is missing
{ {
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
if (conf.AutoDeleteGreetMessages) var msg = conf.ChannelGreetMessageText.Replace("%user%", "**" + user.Username + "**");
if (!string.IsNullOrWhiteSpace(msg))
{ {
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
await toDelete.DeleteAsync().ConfigureAwait(false); if (conf.AutoDeleteGreetMessages)
{
await Task.Delay(conf.AutoDeleteGreetMessagesTimer * 1000).ConfigureAwait(false); // 5 minutes
await toDelete.DeleteAsync().ConfigureAwait(false);
}
}
}
}
if (conf.SendDmGreetMessage)
{
var channel = await user.CreateDMChannelAsync();
if (channel != null)
{
var msg = conf.DmGreetMessageText.Replace("%user%", "**" + user.Username + "**");
if (!string.IsNullOrWhiteSpace(msg))
{
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false);
} }
} }
} }
} }
if (conf.SendDmGreetMessage) [LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task GreetDel(IMessage imsg)
{ {
var channel = await user.CreateDMChannelAsync(); var channel = (ITextChannel)imsg.Channel;
if (channel != null) GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{ {
var msg = conf.DmGreetMessageText.Replace("%user%", "**" + user.Username + "**"); conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(msg)) conf.AutoDeleteGreetMessages = !conf.AutoDeleteGreetMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.AutoDeleteGreetMessages)
await channel.SendMessageAsync("`Automatic deletion of greet messages has been enabled.`").ConfigureAwait(false);
else
await channel.SendMessageAsync("`Automatic deletion of greet messages has been disabled.`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task ByeDel(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.AutoDeleteByeMessages = !conf.AutoDeleteByeMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.AutoDeleteByeMessages)
await channel.SendMessageAsync("`Automatic deletion of bye messages has been enabled.`").ConfigureAwait(false);
else
await channel.SendMessageAsync("`Automatic deletion of bye messages has been disabled.`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task Greet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendChannelGreetMessage = !conf.SendChannelGreetMessage;
conf.GreetMessageChannelId = channel.Id;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.SendChannelGreetMessage)
await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task DmGreet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendDmGreetMessage = !conf.SendDmGreetMessage;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.SendDmGreetMessage)
await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task GreetMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{ {
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); conf.ChannelGreetMessageText = text;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
} }
} }
}
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary] if (string.IsNullOrWhiteSpace(text))
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task GreetDel(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.AutoDeleteGreetMessages = !conf.AutoDeleteGreetMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.AutoDeleteGreetMessages)
await channel.SendMessageAsync("`Automatic deletion of greet messages has been enabled.`").ConfigureAwait(false);
else
await channel.SendMessageAsync("`Automatic deletion of greet messages has been disabled.`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task ByeDel(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.AutoDeleteByeMessages = !conf.AutoDeleteByeMessages;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.AutoDeleteByeMessages)
await channel.SendMessageAsync("`Automatic deletion of bye messages has been enabled.`").ConfigureAwait(false);
else
await channel.SendMessageAsync("`Automatic deletion of bye messages has been disabled.`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task Greet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendChannelGreetMessage = !conf.SendChannelGreetMessage;
conf.GreetMessageChannelId = channel.Id;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.SendChannelGreetMessage)
await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task DmGreet(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendDmGreetMessage = !conf.SendDmGreetMessage;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.SendDmGreetMessage)
await channel.SendMessageAsync("Greet announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Greet announcements disabled.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task GreetMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{ {
conf.ChannelGreetMessageText = text; await channel.SendMessageAsync("`Current greet message:` " + conf.ChannelGreetMessageText);
return;
}
await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false);
if (!conf.SendChannelGreetMessage)
await channel.SendMessageAsync("Enable greet messsages by typing `.greet`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task DmGreetMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{
conf.DmGreetMessageText = text;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
}
if (string.IsNullOrWhiteSpace(text))
{
await channel.SendMessageAsync("`Current DM greet message:` " + conf.DmGreetMessageText);
return;
}
await channel.SendMessageAsync("New DM greet message set.").ConfigureAwait(false);
if (!conf.SendDmGreetMessage)
await channel.SendMessageAsync("Enable DM greet messsages by typing `.greet`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task Bye(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendChannelByeMessage = !conf.SendChannelByeMessage;
conf.ByeMessageChannelId = channel.Id;
uow.GuildConfigs.Update(conf); uow.GuildConfigs.Update(conf);
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
if (conf.SendChannelByeMessage)
await channel.SendMessageAsync("Bye announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Bye announcements disabled.").ConfigureAwait(false);
} }
if (string.IsNullOrWhiteSpace(text)) [LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task ByeMsg(IMessage imsg, [Remainder] string text)
{ {
await channel.SendMessageAsync("`Current greet message:` " + conf.ChannelGreetMessageText); var channel = (ITextChannel)imsg.Channel;
return;
}
await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false);
if (!conf.SendChannelGreetMessage)
await channel.SendMessageAsync("Enable greet messsages by typing `.greet`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary] GuildConfig conf;
[RequireContext(ContextType.Guild)] using (var uow = DbHandler.UnitOfWork())
public async Task DmGreetMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{ {
conf.DmGreetMessageText = text; conf = uow.GuildConfigs.For(channel.Guild.Id);
uow.GuildConfigs.Update(conf); if (!string.IsNullOrWhiteSpace(text))
await uow.CompleteAsync(); {
conf.ChannelByeMessageText = text;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
} }
}
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
{
await channel.SendMessageAsync("`Current DM greet message:` " + conf.DmGreetMessageText);
return;
}
await channel.SendMessageAsync("New DM greet message set.").ConfigureAwait(false);
if (!conf.SendDmGreetMessage)
await channel.SendMessageAsync("Enable DM greet messsages by typing `.greet`").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageGuild)]
public async Task Bye(IMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.SendChannelByeMessage = !conf.SendChannelByeMessage;
conf.ByeMessageChannelId = channel.Id;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.SendChannelByeMessage)
await channel.SendMessageAsync("Bye announcements enabled on this channel.").ConfigureAwait(false);
else
await channel.SendMessageAsync("Bye announcements disabled.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task ByeMsg(IMessage imsg, [Remainder] string text)
{
var channel = (ITextChannel)imsg.Channel;
GuildConfig conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text))
{ {
conf.ChannelByeMessageText = text; await channel.SendMessageAsync("`Current bye message:` " + conf.ChannelGreetMessageText);
uow.GuildConfigs.Update(conf); return;
await uow.CompleteAsync();
} }
await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false);
if (!conf.SendChannelByeMessage)
await channel.SendMessageAsync("Enable bye messsages by typing `.bye`").ConfigureAwait(false);
} }
if (string.IsNullOrWhiteSpace(text))
{
await channel.SendMessageAsync("`Current bye message:` " + conf.ChannelGreetMessageText);
return;
}
await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false);
if (!conf.SendChannelByeMessage)
await channel.SendMessageAsync("Enable bye messsages by typing `.bye`").ConfigureAwait(false);
} }
} }
} }