NadekoBot/src/NadekoBot/Modules/Utility/Utility.cs

142 lines
5.8 KiB
C#
Raw Normal View History

2016-08-13 18:45:08 +00:00
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using System;
using System.Linq;
using System.Threading.Tasks;
2016-08-15 14:57:40 +00:00
using NadekoBot.Services;
using System.Text;
using NadekoBot.Extensions;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Reflection;
2016-08-13 18:45:08 +00:00
namespace NadekoBot.Modules.Utility
{
[Module(".", AppendSpace = false)]
2016-08-18 21:00:54 +00:00
public partial class Utility : DiscordModule
2016-08-13 18:45:08 +00:00
{
2016-08-18 21:00:54 +00:00
public Utility(ILocalization loc, CommandService cmds, IBotConfiguration config, IDiscordClient client) : base(loc, cmds, config, client)
2016-08-15 14:57:40 +00:00
{
2016-08-15 23:38:28 +00:00
2016-08-15 14:57:40 +00:00
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
2016-08-18 15:33:45 +00:00
public async Task WhoPlays(IMessage imsg, [Remainder] string game = null)
2016-08-13 18:45:08 +00:00
{
var chnl = (IGuildChannel)imsg.Channel;
game = game.Trim().ToUpperInvariant();
if (string.IsNullOrWhiteSpace(game))
return;
var arr = (await chnl.Guild.GetUsersAsync())
2016-08-15 14:57:40 +00:00
.Where(u => u.Game?.Name?.ToUpperInvariant() == game)
2016-08-13 18:45:08 +00:00
.Select(u => u.Username)
.ToList();
int i = 0;
if (!arr.Any())
2016-08-15 14:57:40 +00:00
await imsg.Channel.SendMessageAsync(_l["`Nobody is playing that game.`"]).ConfigureAwait(false);
2016-08-13 18:45:08 +00:00
else
await imsg.Channel.SendMessageAsync("```xl\n" + string.Join("\n", arr.GroupBy(item => (i++) / 3).Select(ig => string.Concat(ig.Select(el => $"• {el,-35}")))) + "\n```").ConfigureAwait(false);
}
2016-08-15 14:57:40 +00:00
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
2016-08-18 15:33:45 +00:00
public async Task InRole(IMessage imsg, [Remainder] string roles = null)
2016-08-15 14:57:40 +00:00
{
2016-08-13 18:45:08 +00:00
if (string.IsNullOrWhiteSpace(roles))
return;
2016-08-18 15:33:45 +00:00
var channel = imsg.Channel as ITextChannel;
2016-08-13 18:45:08 +00:00
var arg = roles.Split(',').Select(r => r.Trim().ToUpperInvariant());
2016-08-15 14:57:40 +00:00
string send = _l["`Here is a list of users in a specfic role:`"];
2016-08-13 18:45:08 +00:00
foreach (var roleStr in arg.Where(str => !string.IsNullOrWhiteSpace(str) && str != "@EVERYONE" && str != "EVERYONE"))
{
var role = channel.Guild.Roles.Where(r => r.Name.ToUpperInvariant() == roleStr).FirstOrDefault();
if (role == null) continue;
send += $"\n`{role.Name}`\n";
2016-08-15 14:57:40 +00:00
send += string.Join(", ", (await channel.Guild.GetUsersAsync()).Where(u => u.Roles.Contains(role)).Select(u => u.ToString()));
}
var usr = imsg.Author as IGuildUser;
while (send.Length > 2000)
{
if (!usr.GetPermissions(channel).ManageMessages)
{
await imsg.Channel.SendMessageAsync($"{usr.Mention} you are not allowed to use this command on roles with a lot of users in them to prevent abuse.");
return;
}
var curstr = send.Substring(0, 2000);
await imsg.Channel.SendMessageAsync(curstr.Substring(0,
curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1)).ConfigureAwait(false);
send = curstr.Substring(curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1) +
send.Substring(2000);
2016-08-13 18:45:08 +00:00
}
2016-08-15 14:57:40 +00:00
await imsg.Channel.SendMessageAsync(send).ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task CheckMyPerms(IMessage msg)
{
StringBuilder builder = new StringBuilder("```\n");
var user = msg.Author as IGuildUser;
var perms = user.GetPermissions(msg.Channel as ITextChannel);
foreach (var p in perms.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any()))
{
builder.AppendLine($"{p.Name} : {p.GetValue(perms, null).ToString()}");
}
builder.Append("```");
await msg.Reply(builder.ToString());
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task UserId(IMessage msg, IGuildUser target = null)
{
var usr = target ?? msg.Author;
await msg.Reply($"Id of the user { usr.Username } is { usr.Id })");
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
public async Task ChannelId(IMessage msg)
{
await msg.Reply($"This Channel's ID is {msg.Channel.Id}");
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task ServerId(IMessage msg)
{
2016-08-18 15:33:45 +00:00
await msg.Reply($"This server's ID is {(msg.Channel as ITextChannel).Guild.Id}");
2016-08-15 14:57:40 +00:00
}
2016-08-13 18:45:08 +00:00
2016-08-15 14:57:40 +00:00
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Roles(IMessage msg, IGuildUser target = null)
{
2016-08-18 15:33:45 +00:00
var guild = (msg.Channel as ITextChannel).Guild;
2016-08-15 14:57:40 +00:00
if (target != null)
{
await msg.Reply($"`List of roles for **{target.Username}**:` \n• " + string.Join("\n• ", target.Roles.Except(new[] { guild.EveryoneRole })));
}
else
{
2016-08-18 15:33:45 +00:00
await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", (msg.Channel as ITextChannel).Guild.Roles.Except(new[] { guild.EveryoneRole })));
2016-08-15 14:57:40 +00:00
}
2016-08-13 18:45:08 +00:00
}
2016-08-18 21:00:54 +00:00
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Stats(IMessage imsg)
{
var channel = imsg.Channel as ITextChannel;
await channel.SendMessageAsync(await NadekoBot.Stats.Print());
}
2016-08-13 18:45:08 +00:00
}
}