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

157 lines
6.6 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;
using Discord.WebSocket;
2016-08-13 18:45:08 +00:00
namespace NadekoBot.Modules.Utility
{
[NadekoModule("Utility", ".")]
2016-08-18 21:00:54 +00:00
public partial class Utility : DiscordModule
2016-08-13 18:45:08 +00:00
{
public Utility(ILocalization loc, CommandService cmds, DiscordSocketClient client) : base(loc, cmds, 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, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
public async Task WhosPlaying(IUserMessage umsg, [Remainder] string game = null)
2016-08-13 18:45:08 +00:00
{
var channel = (ITextChannel)umsg.Channel;
2016-08-13 18:45:08 +00:00
game = game.Trim().ToUpperInvariant();
if (string.IsNullOrWhiteSpace(game))
return;
var arr = (await (umsg.Channel as IGuildChannel).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())
await channel.SendMessageAsync(_l["`Nobody is playing that game.`"]).ConfigureAwait(false);
2016-08-13 18:45:08 +00:00
else
await 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-13 18:45:08 +00:00
}
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
public async Task InRole(IUserMessage umsg, [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;
var channel = (ITextChannel)umsg.Channel;
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";
send += string.Join(", ", channel.Guild.GetUsers().Where(u => u.Roles.Contains(role)).Select(u => u.ToString()));
2016-08-15 14:57:40 +00:00
}
var usr = umsg.Author as IGuildUser;
2016-08-15 14:57:40 +00:00
while (send.Length > 2000)
{
if (!usr.GetPermissions(channel).ManageMessages)
{
await channel.SendMessageAsync($"{usr.Mention} you are not allowed to use this command on roles with a lot of users in them to prevent abuse.").ConfigureAwait(false);
2016-08-15 14:57:40 +00:00
return;
}
var curstr = send.Substring(0, 2000);
await channel.SendMessageAsync(curstr.Substring(0,
2016-08-15 14:57:40 +00:00
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
}
await channel.SendMessageAsync(send).ConfigureAwait(false);
2016-08-15 14:57:40 +00:00
}
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
public async Task CheckMyPerms(IUserMessage msg)
2016-08-15 14:57:40 +00:00
{
StringBuilder builder = new StringBuilder("```\n");
var user = msg.Author as IGuildUser;
var perms = user.GetPermissions((ITextChannel)msg.Channel);
2016-08-15 14:57:40 +00:00
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, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
public async Task UserId(IUserMessage msg, IGuildUser target = null)
2016-08-15 14:57:40 +00:00
{
var usr = target ?? msg.Author;
await msg.Reply($"Id of the user { usr.Username } is { usr.Id })").ConfigureAwait(false);
2016-08-15 14:57:40 +00:00
}
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
public async Task ChannelId(IUserMessage msg)
2016-08-15 14:57:40 +00:00
{
await msg.Reply($"This Channel's ID is {msg.Channel.Id}").ConfigureAwait(false);
2016-08-15 14:57:40 +00:00
}
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
public async Task ServerId(IUserMessage msg)
2016-08-15 14:57:40 +00:00
{
await msg.Reply($"This server's ID is {((ITextChannel)msg.Channel).Guild.Id}").ConfigureAwait(false);
2016-08-15 14:57:40 +00:00
}
2016-08-13 18:45:08 +00:00
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
public async Task Roles(IUserMessage msg, IGuildUser target = null)
2016-08-15 14:57:40 +00:00
{
var channel = (ITextChannel)msg.Channel;
var guild = channel.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 }).OrderBy(r => r.Position)));
2016-08-15 14:57:40 +00:00
}
else
{
await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", guild.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r=>r.Position)));
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, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task ChannelTopic(IUserMessage umsg)
{
var channel = (ITextChannel)umsg.Channel;
var topic = channel.Topic;
if (string.IsNullOrWhiteSpace(topic))
await channel.SendMessageAsync("`No topic set.`");
else
await channel.SendMessageAsync("`Topic:` " + topic);
}
[LocalizedCommand, LocalizedRemarks, LocalizedSummary, LocalizedAlias]
2016-08-18 21:00:54 +00:00
[RequireContext(ContextType.Guild)]
public async Task Stats(IUserMessage umsg)
2016-08-18 21:00:54 +00:00
{
var channel = (ITextChannel)umsg.Channel;
2016-08-18 21:00:54 +00:00
await channel.SendMessageAsync(await NadekoBot.Stats.Print());
}
2016-08-13 18:45:08 +00:00
}
}