NadekoBot/NadekoBot.Core/Modules/Administration/RatelimitCommands.cs

131 lines
5.2 KiB
C#
Raw Normal View History

2016-08-20 14:02:06 +00:00
using Discord;
using Discord.Commands;
using Microsoft.EntityFrameworkCore;
2016-08-20 14:02:06 +00:00
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
2017-04-01 19:40:13 +00:00
using System.Collections.Generic;
using System.Linq;
2016-08-20 14:02:06 +00:00
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Common;
using NadekoBot.Modules.Administration.Services;
2016-08-20 14:02:06 +00:00
namespace NadekoBot.Modules.Administration
2016-08-20 14:02:06 +00:00
{
public partial class Administration
{
[Group]
2017-07-15 16:34:34 +00:00
public class SlowModeCommands : NadekoSubmodule<SlowmodeService>
2016-08-20 14:02:06 +00:00
{
private readonly DbService _db;
2017-07-15 16:34:34 +00:00
public SlowModeCommands(DbService db)
2016-09-03 13:04:07 +00:00
{
_db = db;
2016-08-20 14:02:06 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-20 14:02:06 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Slowmode()
2016-08-20 14:02:06 +00:00
{
2017-07-25 16:31:30 +00:00
if (_service.RatelimitingChannels.TryRemove(Context.Channel.Id, out Ratelimiter removed))
2016-08-20 14:02:06 +00:00
{
2017-07-25 16:31:30 +00:00
removed.CancelSource.Cancel();
2017-02-14 19:35:10 +00:00
await ReplyConfirmLocalized("slowmode_disabled").ConfigureAwait(false);
2016-08-20 14:02:06 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
2016-12-17 00:16:14 +00:00
public async Task Slowmode(int msg, int perSec)
{
2016-12-17 00:16:14 +00:00
await Slowmode().ConfigureAwait(false); // disable if exists
if (msg < 1 || perSec < 1 || msg > 100 || perSec > 3600)
2016-09-03 13:04:07 +00:00
{
2017-02-14 19:35:10 +00:00
await ReplyErrorLocalized("invalid_params").ConfigureAwait(false);
2016-09-03 13:04:07 +00:00
return;
}
var toAdd = new Ratelimiter(_service)
{
2016-12-17 00:16:14 +00:00
ChannelId = Context.Channel.Id,
2016-09-03 15:50:10 +00:00
MaxMessages = msg,
PerSeconds = perSec,
};
if(_service.RatelimitingChannels.TryAdd(Context.Channel.Id, toAdd))
2016-08-20 14:02:06 +00:00
{
2017-02-14 19:35:10 +00:00
await Context.Channel.SendConfirmAsync(GetText("slowmode_init"),
GetText("slowmode_desc", Format.Bold(toAdd.MaxMessages.ToString()), Format.Bold(toAdd.PerSeconds.ToString())))
2016-08-20 14:02:06 +00:00
.ConfigureAwait(false);
}
}
2017-04-01 19:40:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(0)]
2017-05-01 08:58:58 +00:00
public async Task SlowmodeWhitelist(IGuildUser user)
2017-04-01 19:40:13 +00:00
{
var siu = new SlowmodeIgnoredUser
{
UserId = user.Id
};
HashSet<SlowmodeIgnoredUser> usrs;
bool removed;
using (var uow = _db.UnitOfWork)
2017-04-01 19:40:13 +00:00
{
usrs = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.SlowmodeIgnoredUsers))
.SlowmodeIgnoredUsers;
if (!(removed = usrs.Remove(siu)))
usrs.Add(siu);
await uow.CompleteAsync().ConfigureAwait(false);
}
_service.IgnoredUsers.AddOrUpdate(Context.Guild.Id, new HashSet<ulong>(usrs.Select(x => x.UserId)), (key, old) => new HashSet<ulong>(usrs.Select(x => x.UserId)));
2017-04-01 19:40:13 +00:00
if(removed)
await ReplyConfirmLocalized("slowmodewl_user_stop", Format.Bold(user.ToString())).ConfigureAwait(false);
else
await ReplyConfirmLocalized("slowmodewl_user_start", Format.Bold(user.ToString())).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Priority(1)]
2017-04-01 19:40:13 +00:00
public async Task SlowmodeWhitelist(IRole role)
{
var sir = new SlowmodeIgnoredRole
{
RoleId = role.Id
};
HashSet<SlowmodeIgnoredRole> roles;
bool removed;
using (var uow = _db.UnitOfWork)
2017-04-01 19:40:13 +00:00
{
roles = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.SlowmodeIgnoredRoles))
.SlowmodeIgnoredRoles;
if (!(removed = roles.Remove(sir)))
roles.Add(sir);
await uow.CompleteAsync().ConfigureAwait(false);
}
_service.IgnoredRoles.AddOrUpdate(Context.Guild.Id, new HashSet<ulong>(roles.Select(x => x.RoleId)), (key, old) => new HashSet<ulong>(roles.Select(x => x.RoleId)));
2017-04-01 19:40:13 +00:00
if (removed)
await ReplyConfirmLocalized("slowmodewl_role_stop", Format.Bold(role.ToString())).ConfigureAwait(false);
else
await ReplyConfirmLocalized("slowmodewl_role_start", Format.Bold(role.ToString())).ConfigureAwait(false);
}
2016-08-20 14:02:06 +00:00
}
}
2016-12-28 06:56:44 +00:00
}