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.Reflection ;
2016-08-20 20:35:27 +00:00
using Discord.WebSocket ;
2016-11-30 19:15:22 +00:00
using NadekoBot.Services.Impl ;
using Discord.API ;
using Embed = Discord . API . Embed ;
using EmbedAuthor = Discord . API . EmbedAuthor ;
using EmbedField = Discord . API . EmbedField ;
2016-08-13 18:45:08 +00:00
namespace NadekoBot.Modules.Utility
{
2016-09-10 19:45:12 +00:00
[NadekoModule("Utility", ".")]
2016-08-18 21:00:54 +00:00
public partial class Utility : DiscordModule
2016-08-13 18:45:08 +00:00
{
2016-09-30 03:58:44 +00:00
public Utility ( ILocalization loc , CommandService cmds , ShardedDiscordClient 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
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
2016-08-28 02:33:09 +00:00
public async Task WhosPlaying ( IUserMessage umsg , [ Remainder ] string game = null )
2016-08-13 18:45:08 +00:00
{
2016-08-28 02:33:09 +00:00
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-13 18:45:08 +00:00
game = game . Trim ( ) . ToUpperInvariant ( ) ;
if ( string . IsNullOrWhiteSpace ( game ) )
return ;
2016-08-28 02:33:09 +00:00
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 ( ) )
2016-11-30 19:15:22 +00:00
await channel . SendErrorAsync ( "Nobody is playing that game." ) . ConfigureAwait ( false ) ;
2016-08-13 18:45:08 +00:00
else
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( "```css\n" + string . Join ( "\n" , arr . GroupBy ( item = > ( i + + ) / 2 )
. Select ( ig = > string . Concat ( ig . Select ( el = > $"• {el,-27}" ) ) ) ) + "\n```" )
. ConfigureAwait ( false ) ;
2016-08-13 18:45:08 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-13 18:45:08 +00:00
[RequireContext(ContextType.Guild)]
2016-10-09 20:40:14 +00:00
public async Task InRole ( IUserMessage umsg , [ Remainder ] string roles )
2016-08-15 14:57:40 +00:00
{
2016-08-13 18:45:08 +00:00
if ( string . IsNullOrWhiteSpace ( roles ) )
return ;
2016-08-28 02:33:09 +00:00
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-13 18:45:08 +00:00
var arg = roles . Split ( ',' ) . Select ( r = > r . Trim ( ) . ToUpperInvariant ( ) ) ;
2016-12-08 15:40:59 +00:00
string send = "ℹ ️ **Here is a list of users in those roles:**" ;
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 ;
2016-11-21 00:56:12 +00:00
send + = $"```css\n[{role.Name}]\n" ;
send + = string . Join ( ", " , channel . Guild . GetUsers ( ) . Where ( u = > u . Roles . Contains ( role ) ) . Select ( u = > u . ToString ( ) ) ) ;
send + = $"\n```" ;
2016-08-15 14:57:40 +00:00
}
2016-08-28 02:33:09 +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 )
{
2016-12-08 15:40:59 +00:00
await channel . SendErrorAsync ( $"⚠️ {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 ) ;
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( 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
}
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( send ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
2016-08-28 02:33:09 +00:00
public async Task CheckMyPerms ( IUserMessage msg )
2016-08-15 14:57:40 +00:00
{
2016-11-21 00:24:15 +00:00
StringBuilder builder = new StringBuilder ( "```http\n" ) ;
2016-08-15 14:57:40 +00:00
var user = msg . Author as IGuildUser ;
2016-09-13 16:12:27 +00:00
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 ( "```" ) ;
2016-12-08 15:40:59 +00:00
await msg . Channel . SendConfirmAsync ( builder . ToString ( ) ) ;
2016-08-15 14:57:40 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
2016-08-28 02:33:09 +00:00
public async Task UserId ( IUserMessage msg , IGuildUser target = null )
2016-08-15 14:57:40 +00:00
{
var usr = target ? ? msg . Author ;
2016-12-08 15:40:59 +00:00
await msg . Channel . SendConfirmAsync ( $"🆔 of the user **{ usr.Username }** is `{ usr.Id }`" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-28 02:33:09 +00:00
public async Task ChannelId ( IUserMessage msg )
2016-08-15 14:57:40 +00:00
{
2016-12-08 15:40:59 +00:00
await msg . Channel . SendConfirmAsync ( $"🆔 of this channel is `{msg.Channel.Id}`" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
2016-08-28 02:33:09 +00:00
public async Task ServerId ( IUserMessage msg )
2016-08-15 14:57:40 +00:00
{
2016-12-08 15:40:59 +00:00
await msg . Channel . SendConfirmAsync ( $"🆔 of this server is `{((ITextChannel)msg.Channel).Guild.Id}`" ) . ConfigureAwait ( false ) ;
2016-08-15 14:57:40 +00:00
}
2016-08-13 18:45:08 +00:00
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-15 14:57:40 +00:00
[RequireContext(ContextType.Guild)]
2016-10-10 00:27:33 +00:00
public async Task Roles ( IUserMessage msg , IGuildUser target , int page = 1 )
2016-08-15 14:57:40 +00:00
{
2016-09-13 16:12:27 +00:00
var channel = ( ITextChannel ) msg . Channel ;
var guild = channel . Guild ;
2016-10-10 00:27:33 +00:00
const int RolesPerPage = 20 ;
if ( page < 1 | | page > 100 )
return ;
2016-08-15 14:57:40 +00:00
if ( target ! = null )
{
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( $"⚔ **Page #{page} of roles for {target.Username}**" , $"```css\n• " + string . Join ( "\n• " , target . Roles . Except ( new [ ] { guild . EveryoneRole } ) . OrderBy ( r = > - r . Position ) . Skip ( ( page - 1 ) * RolesPerPage ) . Take ( RolesPerPage ) ) . SanitizeMentions ( ) + "\n```" ) ;
2016-08-15 14:57:40 +00:00
}
else
{
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( $"⚔ **Page #{page} of all roles on this server:**" , $"```css\n• " + string . Join ( "\n• " , guild . Roles . Except ( new [ ] { guild . EveryoneRole } ) . OrderBy ( r = > - r . Position ) . Skip ( ( page - 1 ) * RolesPerPage ) . Take ( RolesPerPage ) ) . SanitizeMentions ( ) + "\n```" ) ;
2016-08-15 14:57:40 +00:00
}
2016-08-13 18:45:08 +00:00
}
2016-08-18 21:00:54 +00:00
2016-10-10 00:27:33 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public Task Roles ( IUserMessage msg , int page = 1 ) = >
Roles ( msg , null , page ) ;
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-28 00:46:36 +00:00
[RequireContext(ContextType.Guild)]
2016-08-28 02:33:09 +00:00
public async Task ChannelTopic ( IUserMessage umsg )
2016-08-28 00:46:36 +00:00
{
2016-08-28 02:33:09 +00:00
var channel = ( ITextChannel ) umsg . Channel ;
2016-08-28 00:46:36 +00:00
var topic = channel . Topic ;
if ( string . IsNullOrWhiteSpace ( topic ) )
2016-12-08 15:40:59 +00:00
await channel . SendErrorAsync ( "No topic set." ) ;
2016-08-28 00:46:36 +00:00
else
2016-12-08 15:40:59 +00:00
await channel . SendConfirmAsync ( "Channel topic" , topic ) ;
2016-08-28 00:46:36 +00:00
}
2016-10-05 03:09:44 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-08-28 02:33:09 +00:00
public async Task Stats ( IUserMessage umsg )
2016-08-18 21:00:54 +00:00
{
2016-10-06 17:19:42 +00:00
var channel = umsg . Channel ;
2016-08-18 21:00:54 +00:00
2016-11-30 19:15:22 +00:00
var stats = NadekoBot . Stats ;
await channel . EmbedAsync (
new Embed ( )
{
Author = new EmbedAuthor ( )
{
Name = $"NadekoBot v{StatsService.BotVersion}" ,
Url = "http://nadekobot.readthedocs.io/en/latest/" ,
IconUrl = "https://cdn.discordapp.com/avatars/116275390695079945/b21045e778ef21c96d175400e779f0fb.jpg"
} ,
Fields = new [ ] {
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Author" ) ,
2016-11-30 19:15:22 +00:00
Value = stats . Author ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Library" ) ,
2016-11-30 19:15:22 +00:00
Value = stats . Library ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Bot ID" ) ,
2016-11-30 19:15:22 +00:00
Value = NadekoBot . Client . GetCurrentUser ( ) . Id . ToString ( ) ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Commands Ran" ) ,
2016-11-30 19:15:22 +00:00
Value = stats . CommandsRan . ToString ( ) ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Messages" ) ,
Value = $"{stats.MessageCounter} ({stats.MessagesPerSecond:F2}/sec)" ,
2016-11-30 19:15:22 +00:00
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Memory" ) ,
2016-11-30 19:15:22 +00:00
Value = $"{stats.Heap} MB" ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Owner ID(s)" ) ,
2016-11-30 19:15:22 +00:00
Value = stats . OwnerIds ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Uptime" ) ,
2016-11-30 19:15:22 +00:00
Value = stats . GetUptimeString ( "\n" ) ,
Inline = true
} ,
new EmbedField ( ) {
2016-12-04 18:51:11 +00:00
Name = Format . Bold ( "Presence" ) ,
2016-12-04 18:52:16 +00:00
Value = $"{NadekoBot.Client.GetGuilds().Count} Servers\n{stats.TextChannels} Text Channels\n{stats.VoiceChannels} Voice Channels" ,
2016-11-30 19:15:22 +00:00
Inline = true
} ,
} ,
2016-12-03 17:52:26 +00:00
Color = 0x00bbd6
2016-11-30 19:15:22 +00:00
} ) ;
2016-08-18 21:00:54 +00:00
}
2016-10-06 17:19:42 +00:00
private Regex emojiFinder { get ; } = new Regex ( @"<:(?<name>.+?):(?<id>\d*)>" , RegexOptions . Compiled ) ;
[NadekoCommand, Usage, Description, Aliases]
public async Task Showemojis ( IUserMessage msg , [ Remainder ] string emojis )
{
var matches = emojiFinder . Matches ( emojis ) ;
var result = string . Join ( "\n" , matches . Cast < Match > ( )
2016-11-21 00:24:15 +00:00
. Select ( m = > $"**Name:** {m.Groups[" name "]} **Link:** http://discordapp.com/api/emojis/{m.Groups[" id "]}.png" ) ) ;
2016-10-06 17:19:42 +00:00
await msg . Channel . SendMessageAsync ( result ) . ConfigureAwait ( false ) ;
}
2016-10-26 20:26:25 +00:00
2016-10-28 11:31:21 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task ListServers ( IUserMessage imsg , int page = 1 )
{
var channel = ( ITextChannel ) imsg . Channel ;
page - = 1 ;
if ( page < 0 )
return ;
var guilds = NadekoBot . Client . GetGuilds ( ) . OrderBy ( g = > g . Name ) . Skip ( ( page - 1 ) * 15 ) . Take ( 15 ) ;
if ( ! guilds . Any ( ) )
{
2016-12-08 15:40:59 +00:00
await channel . SendErrorAsync ( "No servers found on that page." ) . ConfigureAwait ( false ) ;
2016-10-28 11:31:21 +00:00
return ;
}
2016-12-08 15:40:59 +00:00
await channel . EmbedAsync ( guilds . Aggregate ( new EmbedBuilder ( ) . WithColor ( NadekoBot . OkColor ) ,
( embed , g ) = > embed . AddField ( efb = > efb . WithName ( g . Name )
. WithValue ( $"```css\nID: {g.Id}\nMembers: {g.GetUsers().Count}\nOwnerID: {g.OwnerId} ```" )
. WithIsInline ( false ) ) )
. Build ( ) )
. ConfigureAwait ( false ) ;
2016-10-28 11:31:21 +00:00
}
2016-08-13 18:45:08 +00:00
}
}