Merged with dev

This commit is contained in:
Master Kwoth
2017-05-17 14:28:50 +02:00
16 changed files with 153 additions and 106 deletions

View File

@@ -41,7 +41,7 @@ namespace NadekoBot.Modules.Administration
var channel = msg.Channel as SocketTextChannel;
if (channel == null)
return;
if (deleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune")
if (deleteMessagesOnCommand.Contains(channel.Guild.Id) && cmd.Name != "prune" && cmd.Name != "pick")
await msg.DeleteAsync().ConfigureAwait(false);
}
catch (Exception ex)
@@ -118,7 +118,7 @@ namespace NadekoBot.Modules.Administration
{
var guser = (IGuildUser)Context.User;
var maxRole = guser.GetRoles().Max(x => x.Position);
if (maxRole < role.Position || maxRole <= usr.GetRoles().Max(x => x.Position))
if ((Context.User.Id != Context.Guild.OwnerId) && (maxRole < role.Position || maxRole <= usr.GetRoles().Max(x => x.Position)))
return;
try
{
@@ -378,12 +378,14 @@ namespace NadekoBot.Modules.Administration
{
var user = await Context.Guild.GetCurrentUserAsync().ConfigureAwait(false);
var enumerable = (await Context.Channel.GetMessagesAsync().Flatten()).AsEnumerable();
enumerable = enumerable.Where(x => x.Author.Id == user.Id);
var enumerable = (await Context.Channel.GetMessagesAsync().Flatten())
.Where(x => x.Author.Id == user.Id && DateTime.Now - x.CreatedAt < twoWeeks);
await Context.Channel.DeleteMessagesAsync(enumerable).ConfigureAwait(false);
Context.Message.DeleteAfter(3);
}
private TimeSpan twoWeeks => TimeSpan.FromDays(14);
// prune x
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
@@ -396,7 +398,8 @@ namespace NadekoBot.Modules.Administration
return;
await Context.Message.DeleteAsync().ConfigureAwait(false);
int limit = (count < 100) ? count + 1 : 100;
var enumerable = (await Context.Channel.GetMessagesAsync(limit: limit).Flatten().ConfigureAwait(false));
var enumerable = (await Context.Channel.GetMessagesAsync(limit: limit).Flatten().ConfigureAwait(false))
.Where(x => DateTime.Now - x.CreatedAt < twoWeeks);
if (enumerable.FirstOrDefault()?.Id == Context.Message.Id)
enumerable = enumerable.Skip(1).ToArray();
else
@@ -419,7 +422,8 @@ namespace NadekoBot.Modules.Administration
count += 1;
int limit = (count < 100) ? count : 100;
var enumerable = (await Context.Channel.GetMessagesAsync(limit: limit).Flatten()).Where(m => m.Author == user);
var enumerable = (await Context.Channel.GetMessagesAsync(limit: limit).Flatten())
.Where(m => m.Author == user && DateTime.Now - m.CreatedAt < twoWeeks);
await Context.Channel.DeleteMessagesAsync(enumerable).ConfigureAwait(false);
Context.Message.DeleteAfter(3);
@@ -434,7 +438,9 @@ namespace NadekoBot.Modules.Administration
foreach (var role in roles)
{
send += $"\n**{role.Name}**\n";
send += string.Join(", ", (await Context.Guild.GetUsersAsync()).Where(u => u.GetRoles().Contains(role)).Take(50).Select(u => u.Mention));
send += string.Join(", ", (await Context.Guild.GetUsersAsync())
.Where(u => u.GetRoles().Contains(role))
.Take(50).Select(u => u.Mention));
}
while (send.Length > 2000)

View File

@@ -10,6 +10,8 @@ using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
namespace NadekoBot.Modules.Administration
{
@@ -35,30 +37,51 @@ namespace NadekoBot.Modules.Administration
= new ConcurrentDictionary<ulong, UserSpamStats>();
}
public class UserSpamStats
public class UserSpamStats : IDisposable
{
public int Count { get; set; }
public int Count => timers.Count;
public string LastMessage { get; set; }
public UserSpamStats(string msg)
{
Count = 1;
LastMessage = msg.ToUpperInvariant();
}
private ConcurrentQueue<Timer> timers { get; }
public UserSpamStats(IUserMessage msg)
{
LastMessage = msg.Content.ToUpperInvariant();
timers = new ConcurrentQueue<Timer>();
ApplyNextMessage(msg);
}
private readonly object applyLock = new object();
public void ApplyNextMessage(IUserMessage message)
{
var upperMsg = message.Content.ToUpperInvariant();
if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any()))
{
LastMessage = upperMsg;
Count = 0;
}
else
{
Count++;
lock(applyLock){
var upperMsg = message.Content.ToUpperInvariant();
if (upperMsg != LastMessage || (string.IsNullOrWhiteSpace(upperMsg) && message.Attachments.Any()))
{
LastMessage = upperMsg;
//todo c#7
Timer old;
while(timers.TryDequeue(out old))
old.Change(Timeout.Infinite, Timeout.Infinite);
}
var t = new Timer((_) => {
//todo c#7
Timer __;
if(timers.TryDequeue(out __))
__.Change(Timeout.Infinite, Timeout.Infinite);
}, null, TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30));
timers.Enqueue(t);
}
}
public void Dispose()
{
//todo c#7
Timer old;
while(timers.TryDequeue(out old))
old.Change(Timeout.Infinite, Timeout.Infinite);
}
}
[Group]
@@ -112,7 +135,7 @@ namespace NadekoBot.Modules.Administration
}))
return;
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, new UserSpamStats(msg.Content),
var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, (id) => new UserSpamStats(msg),
(id, old) =>
{
old.ApplyNextMessage(msg); return old;
@@ -122,6 +145,7 @@ namespace NadekoBot.Modules.Administration
{
if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats))
{
stats.Dispose();
await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, (IGuildUser)msg.Author)
.ConfigureAwait(false);
}
@@ -317,6 +341,7 @@ namespace NadekoBot.Modules.Administration
AntiSpamStats throwaway;
if (_antiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway))
{
throwaway.UserStats.ForEach(x => x.Value.Dispose());
using (var uow = DbHandler.UnitOfWork())
{
var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting)

View File

@@ -108,7 +108,6 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork())
{
var roleModels = uow.SelfAssignedRoles.GetFromGuild(Context.Guild.Id).ToList();
roleCnt = roleModels.Count;
msg.AppendLine();
foreach (var roleModel in roleModels)
@@ -116,11 +115,13 @@ namespace NadekoBot.Modules.Administration
var role = Context.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
if (role == null)
{
toRemove.Add(roleModel);
uow.SelfAssignedRoles.Remove(roleModel);
}
else
{
msg.Append($"**{role.Name}**, ");
roleCnt++;
}
}
foreach (var role in toRemove)

View File

@@ -73,7 +73,7 @@ namespace NadekoBot.Modules.Administration
await guild.AddBanAsync(user).ConfigureAwait(false);
break;
case PunishmentAction.Softban:
await guild.AddBanAsync(user).ConfigureAwait(false);
await guild.AddBanAsync(user, 7).ConfigureAwait(false);
try
{
await guild.RemoveBanAsync(user).ConfigureAwait(false);
@@ -214,25 +214,15 @@ namespace NadekoBot.Modules.Administration
using (var uow = DbHandler.UnitOfWork())
{
var ps = uow.GuildConfigs.For(Context.Guild.Id).WarnPunishments;
var p = ps.FirstOrDefault(x => x.Count == number);
var ps = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.WarnPunishments)).WarnPunishments;
ps.RemoveAll(x => x.Count == number);
if (p == null)
ps.Add(new WarningPunishment()
{
ps.Add(new WarningPunishment()
{
Count = number,
Punishment = punish,
Time = time,
});
}
else
{
p.Count = number;
p.Punishment = punish;
p.Time = time;
uow._context.Update(p);
}
Count = number,
Punishment = punish,
Time = time,
});
uow.Complete();
}

View File

@@ -173,6 +173,8 @@ namespace NadekoBot.Modules.Gambling
amount + CurrencySign);
if (w.Affinity?.UserId == Context.User.Id)
msg += "\n" + GetText("waifu_fulfilled", target, w.Price + CurrencySign);
else
msg = " " + msg;
await Context.Channel.SendConfirmAsync(Context.User.Mention + msg).ConfigureAwait(false);
}
@@ -188,9 +190,15 @@ namespace NadekoBot.Modules.Gambling
private static readonly TimeSpan _divorceLimit = TimeSpan.FromHours(6);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Divorce([Remainder]IGuildUser target)
[Priority(1)]
public Task Divorce([Remainder]IGuildUser target) => Divorce(target.Id);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[Priority(0)]
public async Task Divorce([Remainder]ulong targetId)
{
if (target.Id == Context.User.Id)
if (targetId == Context.User.Id)
return;
DivorceResult result;
@@ -199,7 +207,7 @@ namespace NadekoBot.Modules.Gambling
WaifuInfo w = null;
using (var uow = DbHandler.UnitOfWork())
{
w = uow.Waifus.ByWaifuUserId(target.Id);
w = uow.Waifus.ByWaifuUserId(targetId);
var now = DateTime.UtcNow;
if (w?.Claimer == null || w.Claimer.UserId != Context.User.Id)
result = DivorceResult.NotYourWife;

View File

@@ -15,7 +15,7 @@ namespace NadekoBot.Modules.Games.Hangman
{
public class HangmanTermPool
{
const string termsPath = "data/hangman2.json";
const string termsPath = "data/hangman3.json";
public static IReadOnlyDictionary<string, HangmanObject[]> data { get; }
static HangmanTermPool()
{

View File

@@ -82,9 +82,9 @@ namespace NadekoBot.Modules.Games
var prefix = NadekoBot.ModulePrefixes[typeof(Games).Name];
var toSend = dropAmount == 1
? GetLocalText(channel, "curgen_sn", NadekoBot.BotConfig.CurrencySign)
+ GetLocalText(channel, "pick_sn", prefix)
+ " " + GetLocalText(channel, "pick_sn", prefix)
: GetLocalText(channel, "curgen_pl", dropAmount, NadekoBot.BotConfig.CurrencySign)
+ GetLocalText(channel, "pick_pl", prefix);
+ " " + GetLocalText(channel, "pick_pl", prefix);
var file = GetRandomCurrencyImage();
using (var fileStream = file.Value.ToStream())
{

View File

@@ -26,7 +26,11 @@ namespace NadekoBot.Modules.Music.Classes
{
public SongInfo SongInfo { get; }
public MusicPlayer MusicPlayer { get; set; }
public string QueuerName { get; set; }
private string _queuerName;
public string QueuerName { get{
return Discord.Format.Sanitize(_queuerName);
} set { _queuerName = value; } }
public TimeSpan TotalTime { get; set; } = TimeSpan.Zero;
public TimeSpan CurrentTime => TimeSpan.FromSeconds(BytesSent / (float)_frameBytes / (1000 / (float)_milliseconds));

View File

@@ -65,6 +65,9 @@ namespace NadekoBot.Modules.Permissions
private async Task Blacklist(AddRemove action, ulong id, BlacklistType type)
{
if(action == AddRemove.Add && NadekoBot.Credentials.OwnerIds.Contains(id))
return;
using (var uow = DbHandler.UnitOfWork())
{
if (action == AddRemove.Add)

View File

@@ -190,7 +190,7 @@ namespace NadekoBot.Modules.Permissions
{
var config = uow.GuildConfigs.For(channel.Guild.Id, set => set.Include(gc => gc.FilteredWords));
removed = config.FilteredWords.RemoveWhere(fw => fw.Word == word);
removed = config.FilteredWords.RemoveWhere(fw => fw.Word.Trim().ToLowerInvariant() == word);
if (removed == 0)
config.FilteredWords.Add(new Services.Database.Models.FilteredWord() { Word = word });

View File

@@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Utility
}
await Context.Channel.EmbedAsync(new EmbedBuilder()
.WithTitle(GetText("activity_page", page))
.WithTitle(GetText("activity_page", page + 1))
.WithOkColor()
.WithFooter(efb => efb.WithText(GetText("activity_users_total",
NadekoBot.CommandHandler.UserMessagesSent.Count)))

View File

@@ -123,8 +123,19 @@ namespace NadekoBot.Modules.Utility
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(0)]
public Task Remind(ITextChannel channel, string timeStr, [Remainder] string message) =>
RemindInternal(channel.Id, false, timeStr, message);
public async Task Remind(ITextChannel channel, string timeStr, [Remainder] string message)
{
var perms = ((IGuildUser)Context.User).GetPermissions((ITextChannel)channel);
if (!perms.SendMessages || !perms.ReadMessages)
{
await ReplyErrorLocalized("cant_read_or_send").ConfigureAwait(false);
return;
}
else
{
var _ = RemindInternal(channel.Id, false, timeStr, message).ConfigureAwait(false);
}
}
public async Task RemindInternal(ulong targetId, bool isPrivate, string timeStr, [Remainder] string message)
{

View File

@@ -503,7 +503,6 @@ namespace NadekoBot.Modules.Utility
await JsonConvert.SerializeObject(grouping, Formatting.Indented).ToStream().ConfigureAwait(false), title, title).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Ping()
{
var sw = Stopwatch.StartNew();