2015-12-05 10:27:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Security.Cryptography;
|
2015-12-10 01:22:09 +00:00
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord;
|
2015-12-30 04:44:36 +00:00
|
|
|
|
using Discord.Legacy;
|
2016-01-21 02:17:01 +00:00
|
|
|
|
using NadekoBot.Modules;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
2016-01-21 02:17:01 +00:00
|
|
|
|
namespace NadekoBot.Extensions
|
2015-12-05 10:27:00 +00:00
|
|
|
|
{
|
|
|
|
|
public static class Extensions
|
|
|
|
|
{
|
2015-12-09 21:51:20 +00:00
|
|
|
|
public static string Scramble(this string word) {
|
|
|
|
|
|
|
|
|
|
var letters = word.ToArray();
|
|
|
|
|
for (int i = 0; i < letters.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i % 3 == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (letters[i] != ' ')
|
|
|
|
|
letters[i] = '_';
|
|
|
|
|
}
|
|
|
|
|
return "`"+string.Join(" ", letters)+"`";
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-10 01:22:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to the channel from which this command is called.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">EventArg</param>
|
|
|
|
|
/// <param name="message">Message to be sent</param>
|
|
|
|
|
/// <returns></returns>
|
2015-12-12 22:39:58 +00:00
|
|
|
|
public static async Task<Message> Send(this CommandEventArgs e, string message)
|
2015-12-30 04:44:36 +00:00
|
|
|
|
=> await e.Channel.SendMessage(message);
|
2015-12-13 02:54:21 +00:00
|
|
|
|
|
2015-12-10 01:22:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to the channel from which MessageEventArg came.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">EventArg</param>
|
|
|
|
|
/// <param name="message">Message to be sent</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task Send(this MessageEventArgs e, string message)
|
|
|
|
|
{
|
2015-12-30 04:44:36 +00:00
|
|
|
|
await e.Channel.SendMessage(message);
|
2015-12-10 01:22:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 02:54:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to this channel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="c"></param>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task Send(this Channel c, string message)
|
|
|
|
|
{
|
2015-12-30 04:44:36 +00:00
|
|
|
|
await c.SendMessage(message);
|
2015-12-13 02:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a private message to this user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="c"></param>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task Send(this User u, string message)
|
|
|
|
|
{
|
2015-12-30 04:44:36 +00:00
|
|
|
|
await u.SendMessage(message);
|
2015-12-13 02:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replies to a user who invoked this command, message start with that user's mention.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <returns></returns>
|
2015-12-10 01:22:09 +00:00
|
|
|
|
public static async Task Reply(this CommandEventArgs e, string message)
|
|
|
|
|
{
|
2015-12-13 02:54:21 +00:00
|
|
|
|
await e.Send(e.User.Mention + " " + message);
|
2015-12-10 01:22:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 02:54:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replies to a user who invoked this command, message start with that user's mention.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <returns></returns>
|
2015-12-10 01:22:09 +00:00
|
|
|
|
public static async Task Reply(this MessageEventArgs e, string message)
|
|
|
|
|
{
|
2015-12-13 02:54:21 +00:00
|
|
|
|
await e.Send(e.User.Mention + " " + message);
|
2015-12-10 01:22:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 02:54:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Randomizes element order in a list
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="list"></param>
|
2015-12-05 10:27:00 +00:00
|
|
|
|
public static void Shuffle<T>(this IList<T> list)
|
|
|
|
|
{
|
|
|
|
|
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
|
|
|
|
|
int n = list.Count;
|
|
|
|
|
while (n > 1)
|
|
|
|
|
{
|
|
|
|
|
byte[] box = new byte[1];
|
|
|
|
|
do provider.GetBytes(box);
|
|
|
|
|
while (!(box[0] < n * (Byte.MaxValue / n)));
|
|
|
|
|
int k = (box[0] % n);
|
|
|
|
|
n--;
|
|
|
|
|
T value = list[k];
|
|
|
|
|
list[k] = list[n];
|
|
|
|
|
list[n] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 04:44:36 +00:00
|
|
|
|
|
2016-01-21 02:17:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shortens a string URL
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
public static string ShortenUrl(this string str) {
|
|
|
|
|
return Searches.ShortenUrl(str);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 04:44:36 +00:00
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
|
|
|
|
|
foreach (T element in source) {
|
|
|
|
|
action(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-05 10:27:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|