NadekoBot/NadekoBot.Core/Modules/Administration/MuteCommands.cs

172 lines
6.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Core.Services;
using System;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
2017-07-15 16:34:34 +00:00
public class MuteCommands : NadekoSubmodule<MuteService>
{
private readonly DbService _db;
2017-07-15 16:34:34 +00:00
public MuteCommands(DbService db)
2017-03-10 22:06:22 +00:00
{
2017-05-27 08:19:27 +00:00
_db = db;
2017-03-10 22:06:22 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[Priority(0)]
public async Task SetMuteRole([Remainder] string name)
{
name = name.Trim();
if (string.IsNullOrWhiteSpace(name))
return;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
{
2016-12-16 21:44:26 +00:00
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set);
config.MuteRoleName = name;
2017-05-27 08:19:27 +00:00
_service.GuildMuteRoles.AddOrUpdate(Context.Guild.Id, name, (id, old) => name);
await uow.CompleteAsync().ConfigureAwait(false);
}
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("mute_role_set").ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[Priority(1)]
2016-12-17 00:16:14 +00:00
public Task SetMuteRole([Remainder] IRole role)
=> SetMuteRole(role.Name);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.MuteMembers)]
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Mute(IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.MuteUser(user).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_muted", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
2017-03-10 22:06:22 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.MuteMembers)]
[Priority(1)]
2017-03-10 22:06:22 +00:00
public async Task Mute(int minutes, IGuildUser user)
{
2017-03-10 22:10:42 +00:00
if (minutes < 1 || minutes > 1440)
2017-03-10 22:06:22 +00:00
return;
try
{
2017-05-27 08:19:27 +00:00
await _service.TimedMute(user, TimeSpan.FromMinutes(minutes)).ConfigureAwait(false);
2017-03-10 22:06:22 +00:00
await ReplyConfirmLocalized("user_muted_time", Format.Bold(user.ToString()), minutes).ConfigureAwait(false);
}
catch (Exception ex)
{
_log.Warn(ex);
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.MuteMembers)]
2016-12-17 00:16:14 +00:00
public async Task Unmute(IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.UnmuteUser(user).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_unmuted", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2016-12-17 00:16:14 +00:00
public async Task ChatMute(IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.MuteUser(user, MuteType.Chat).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_chat_mute", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
2016-12-17 00:16:14 +00:00
public async Task ChatUnmute(IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.UnmuteUser(user, MuteType.Chat).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_chat_unmute", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.MuteMembers)]
2017-05-27 08:19:27 +00:00
public async Task VoiceMute([Remainder] IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.MuteUser(user, MuteType.Voice).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_voice_mute", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.MuteMembers)]
2017-05-27 08:19:27 +00:00
public async Task VoiceUnmute([Remainder] IGuildUser user)
{
try
{
2017-05-27 08:19:27 +00:00
await _service.UnmuteUser(user, MuteType.Voice).ConfigureAwait(false);
2017-02-14 15:36:02 +00:00
await ReplyConfirmLocalized("user_voice_unmute", Format.Bold(user.ToString())).ConfigureAwait(false);
}
catch
{
2017-02-14 15:36:02 +00:00
await ReplyErrorLocalized("mute_error").ConfigureAwait(false);
}
}
}
}
}