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

370 lines
15 KiB
C#
Raw Normal View History

2016-08-16 16:00:52 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Core.Services;
2017-07-17 19:42:36 +00:00
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Core.Services.Database.Models;
2016-08-16 16:00:52 +00:00
namespace NadekoBot.Modules.Administration
{
2017-07-15 16:34:34 +00:00
public partial class Administration : NadekoTopLevelModule<AdministrationService>
2016-08-16 16:00:52 +00:00
{
2017-05-27 08:19:27 +00:00
private IGuild _nadekoSupportServer;
private readonly DbService _db;
2016-10-05 05:01:19 +00:00
2017-07-15 16:34:34 +00:00
public Administration(DbService db)
2016-10-12 21:41:31 +00:00
{
2017-05-27 08:19:27 +00:00
_db = db;
2016-10-12 21:41:31 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-24 17:04:24 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.Administrator)]
[RequireBotPermission(GuildPermission.ManageMessages)]
2016-12-16 18:43:57 +00:00
public async Task Delmsgoncmd()
2016-08-24 17:04:24 +00:00
{
bool enabled;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-08-24 17:04:24 +00:00
{
2016-12-16 18:43:57 +00:00
var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set);
enabled = conf.DeleteMessageOnCommand = !conf.DeleteMessageOnCommand;
2016-08-24 17:04:24 +00:00
await uow.CompleteAsync();
}
if (enabled)
2016-12-21 11:52:01 +00:00
{
2017-07-15 16:34:34 +00:00
_service.DeleteMessagesOnCommand.Add(Context.Guild.Id);
await ReplyConfirmLocalized("delmsg_on").ConfigureAwait(false);
2016-12-21 11:52:01 +00:00
}
2016-08-24 17:04:24 +00:00
else
2016-12-21 11:52:01 +00:00
{
2017-07-15 16:34:34 +00:00
_service.DeleteMessagesOnCommand.TryRemove(Context.Guild.Id);
await ReplyConfirmLocalized("delmsg_off").ConfigureAwait(false);
2016-12-21 11:52:01 +00:00
}
2016-08-24 17:04:24 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task Setrole(IGuildUser usr, [Remainder] IRole role)
2016-08-16 16:00:52 +00:00
{
var guser = (IGuildUser)Context.User;
var maxRole = guser.GetRoles().Max(x => x.Position);
if ((Context.User.Id != Context.Guild.OwnerId) && (maxRole <= role.Position || maxRole <= usr.GetRoles().Max(x => x.Position)))
return;
2016-08-16 16:00:52 +00:00
try
{
2017-04-15 00:54:19 +00:00
await usr.AddRoleAsync(role).ConfigureAwait(false);
await ReplyConfirmLocalized("setrole", Format.Bold(role.Name), Format.Bold(usr.ToString()))
.ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch (Exception ex)
{
await ReplyErrorLocalized("setrole_err").ConfigureAwait(false);
2017-06-15 13:29:41 +00:00
_log.Info(ex);
2016-08-16 16:00:52 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task Removerole(IGuildUser usr, [Remainder] IRole role)
2016-08-16 16:00:52 +00:00
{
var guser = (IGuildUser)Context.User;
if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= usr.GetRoles().Max(x => x.Position))
return;
2016-08-16 16:00:52 +00:00
try
{
2017-04-15 00:54:19 +00:00
await usr.RemoveRoleAsync(role).ConfigureAwait(false);
await ReplyConfirmLocalized("remrole", Format.Bold(role.Name), Format.Bold(usr.ToString())).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch
{
await ReplyErrorLocalized("remrole_err").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task RenameRole(IRole roleToEdit, string newname)
2016-08-16 16:00:52 +00:00
{
var guser = (IGuildUser)Context.User;
if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= roleToEdit.Position)
return;
2016-08-16 16:00:52 +00:00
try
{
2016-12-17 04:09:04 +00:00
if (roleToEdit.Position > (await Context.Guild.GetCurrentUserAsync().ConfigureAwait(false)).GetRoles().Max(r => r.Position))
2016-08-16 16:00:52 +00:00
{
await ReplyErrorLocalized("renrole_perms").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
return;
}
2016-08-18 15:33:45 +00:00
await roleToEdit.ModifyAsync(g => g.Name = newname).ConfigureAwait(false);
await ReplyConfirmLocalized("renrole").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch (Exception)
{
await ReplyErrorLocalized("renrole_err").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task RemoveAllRoles([Remainder] IGuildUser user)
2016-08-16 16:00:52 +00:00
{
var guser = (IGuildUser)Context.User;
2017-06-25 01:56:23 +00:00
var userRoles = user.GetRoles().Except(new[] { guser.Guild.EveryoneRole });
if (user.Id == Context.Guild.OwnerId || (Context.User.Id != Context.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= userRoles.Max(x => x.Position)))
return;
2016-08-16 16:00:52 +00:00
try
{
await user.RemoveRolesAsync(userRoles).ConfigureAwait(false);
await ReplyConfirmLocalized("rar", Format.Bold(user.ToString())).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch (Exception)
2016-08-16 16:00:52 +00:00
{
await ReplyErrorLocalized("rar_err").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task CreateRole([Remainder] string roleName = null)
2016-08-16 16:00:52 +00:00
{
2016-08-18 15:33:45 +00:00
if (string.IsNullOrWhiteSpace(roleName))
2016-08-16 16:00:52 +00:00
return;
var r = await Context.Guild.CreateRoleAsync(roleName).ConfigureAwait(false);
await ReplyConfirmLocalized("cr", Format.Bold(r.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2017-05-03 07:49:35 +00:00
public async Task RoleHoist(IRole role)
2017-04-05 13:04:37 +00:00
{
2017-05-03 07:49:35 +00:00
await role.ModifyAsync(r => r.Hoist = !role.IsHoisted).ConfigureAwait(false);
await ReplyConfirmLocalized("rh", Format.Bold(role.Name), Format.Bold(role.IsHoisted.ToString())).ConfigureAwait(false);
2017-04-05 13:04:37 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[RequireBotPermission(GuildPermission.ManageRoles)]
2016-12-16 18:43:57 +00:00
public async Task RoleColor(params string[] args)
2016-08-16 16:00:52 +00:00
{
if (args.Length != 2 && args.Length != 4)
2016-08-16 16:00:52 +00:00
{
await ReplyErrorLocalized("rc_params").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
return;
}
2016-08-18 15:33:45 +00:00
var roleName = args[0].ToUpperInvariant();
2017-02-14 14:53:51 +00:00
var role = Context.Guild.Roles.FirstOrDefault(r => r.Name.ToUpperInvariant() == roleName);
2016-08-16 16:00:52 +00:00
if (role == null)
{
await ReplyErrorLocalized("rc_not_exist").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
return;
}
try
{
2017-02-14 23:59:10 +00:00
var rgb = args.Length == 4;
2016-08-18 15:33:45 +00:00
var arg1 = args[1].Replace("#", "");
2016-08-16 16:00:52 +00:00
var red = Convert.ToByte(rgb ? int.Parse(arg1) : Convert.ToInt32(arg1.Substring(0, 2), 16));
2016-08-18 15:33:45 +00:00
var green = Convert.ToByte(rgb ? int.Parse(args[2]) : Convert.ToInt32(arg1.Substring(2, 2), 16));
var blue = Convert.ToByte(rgb ? int.Parse(args[3]) : Convert.ToInt32(arg1.Substring(4, 2), 16));
2016-12-17 00:16:14 +00:00
await role.ModifyAsync(r => r.Color = new Color(red, green, blue)).ConfigureAwait(false);
await ReplyConfirmLocalized("rc", Format.Bold(role.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch (Exception)
{
await ReplyErrorLocalized("rc_perms").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.DeafenMembers)]
[RequireBotPermission(GuildPermission.DeafenMembers)]
2016-12-16 18:43:57 +00:00
public async Task Deafen(params IGuildUser[] users)
2016-08-16 16:00:52 +00:00
{
2016-08-18 15:33:45 +00:00
if (!users.Any())
2016-08-16 16:00:52 +00:00
return;
foreach (var u in users)
2016-08-16 16:00:52 +00:00
{
try
2016-08-16 16:00:52 +00:00
{
2016-12-26 07:28:48 +00:00
await u.ModifyAsync(usr => usr.Deaf = true).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch
{
// ignored
}
2016-08-16 16:00:52 +00:00
}
await ReplyConfirmLocalized("deafen").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.DeafenMembers)]
[RequireBotPermission(GuildPermission.DeafenMembers)]
2016-12-16 18:43:57 +00:00
public async Task UnDeafen(params IGuildUser[] users)
2016-08-16 16:00:52 +00:00
{
2016-08-18 15:33:45 +00:00
if (!users.Any())
2016-08-16 16:00:52 +00:00
return;
foreach (var u in users)
2016-08-16 16:00:52 +00:00
{
try
2016-08-16 16:00:52 +00:00
{
2016-12-26 07:28:48 +00:00
await u.ModifyAsync(usr => usr.Deaf = false).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
catch
{
// ignored
}
2016-08-16 16:00:52 +00:00
}
await ReplyConfirmLocalized("undeafen").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task DelVoiChanl([Remainder] IVoiceChannel voiceChannel)
2016-08-16 16:00:52 +00:00
{
2016-08-20 17:38:55 +00:00
await voiceChannel.DeleteAsync().ConfigureAwait(false);
await ReplyConfirmLocalized("delvoich", Format.Bold(voiceChannel.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task CreatVoiChanl([Remainder] string channelName)
2016-08-16 16:00:52 +00:00
{
2016-12-16 18:43:57 +00:00
var ch = await Context.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false);
await ReplyConfirmLocalized("createvoich", Format.Bold(ch.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task DelTxtChanl([Remainder] ITextChannel toDelete)
2016-08-16 16:00:52 +00:00
{
2016-11-20 11:11:23 +00:00
await toDelete.DeleteAsync().ConfigureAwait(false);
await ReplyConfirmLocalized("deltextchan", Format.Bold(toDelete.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task CreaTxtChanl([Remainder] string channelName)
2016-08-16 16:00:52 +00:00
{
2016-12-16 18:43:57 +00:00
var txtCh = await Context.Guild.CreateTextChannelAsync(channelName).ConfigureAwait(false);
await ReplyConfirmLocalized("createtextchan", Format.Bold(txtCh.Name)).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task SetTopic([Remainder] string topic = null)
2016-08-16 16:00:52 +00:00
{
2016-12-17 00:16:14 +00:00
var channel = (ITextChannel)Context.Channel;
2016-08-18 15:33:45 +00:00
topic = topic ?? "";
2016-12-17 00:16:14 +00:00
await channel.ModifyAsync(c => c.Topic = topic);
await ReplyConfirmLocalized("set_topic").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.ManageChannels)]
2016-12-16 18:43:57 +00:00
public async Task SetChanlName([Remainder] string name)
2016-08-16 16:00:52 +00:00
{
2016-12-17 00:16:14 +00:00
var channel = (ITextChannel)Context.Channel;
await channel.ModifyAsync(c => c.Name = name).ConfigureAwait(false);
await ReplyConfirmLocalized("set_channel_name").ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 16:00:52 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.MentionEveryone)]
public async Task MentionRole(params IRole[] roles)
2016-08-16 16:00:52 +00:00
{
string send = "❕" + GetText("menrole", Context.User.Mention);
2016-08-18 15:33:45 +00:00
foreach (var role in roles)
2016-12-26 07:28:48 +00:00
{
send += $"\n**{role.Name}**\n";
send += string.Join(", ", (await Context.Guild.GetUsersAsync())
.Where(u => u.GetRoles().Contains(role))
.Take(50).Select(u => u.Mention));
2016-08-16 16:00:52 +00:00
}
while (send.Length > 2000)
{
var curstr = send.Substring(0, 2000);
2016-12-16 18:43:57 +00:00
await Context.Channel.SendMessageAsync(curstr.Substring(0,
2016-08-16 16:00:52 +00:00
curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1)).ConfigureAwait(false);
send = curstr.Substring(curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1) +
send.Substring(2000);
}
2016-12-16 18:43:57 +00:00
await Context.Channel.SendMessageAsync(send).ConfigureAwait(false);
2016-08-16 16:00:52 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-12-16 18:43:57 +00:00
public async Task Donators()
2016-08-24 17:04:24 +00:00
{
IEnumerable<Donator> donatorsOrdered;
2016-10-15 06:25:52 +00:00
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-08-24 17:04:24 +00:00
{
donatorsOrdered = uow.Donators.GetDonatorsOrdered();
}
await Context.Channel.SendConfirmAsync(GetText("donators"), string.Join("⭐", donatorsOrdered.Select(d => d.Name))).ConfigureAwait(false);
2017-05-27 08:19:27 +00:00
_nadekoSupportServer = _nadekoSupportServer ?? (await Context.Client.GetGuildAsync(117523346618318850));
2016-10-15 06:25:52 +00:00
2017-02-14 14:53:51 +00:00
var patreonRole = _nadekoSupportServer?.GetRole(236667642088259585);
2016-10-15 06:25:52 +00:00
if (patreonRole == null)
return;
2017-02-14 14:53:51 +00:00
var usrs = (await _nadekoSupportServer.GetUsersAsync()).Where(u => u.RoleIds.Contains(236667642088259585u));
2016-12-16 18:43:57 +00:00
await Context.Channel.SendConfirmAsync("Patreon supporters", string.Join("⭐", usrs.Select(d => d.Username))).ConfigureAwait(false);
2016-08-24 17:04:24 +00:00
}
2016-08-16 16:00:52 +00:00
2016-08-24 17:04:24 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-08 22:21:29 +00:00
[OwnerOnly]
2016-12-16 18:43:57 +00:00
public async Task Donadd(IUser donator, int amount)
2016-08-24 17:04:24 +00:00
{
Donator don;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-08-24 17:04:24 +00:00
{
don = uow.Donators.AddOrUpdateDonator(donator.Id, donator.Username, amount);
await uow.CompleteAsync();
}
await ReplyConfirmLocalized("donadd", don.Amount).ConfigureAwait(false);
2016-08-24 17:04:24 +00:00
}
2016-08-16 16:00:52 +00:00
}
2016-12-26 07:28:48 +00:00
}