Add Minecraft Commands

This commit is contained in:
fkndean 2016-11-06 16:04:24 -05:00
parent 2248298bc9
commit 3611817e22
3 changed files with 159 additions and 2 deletions

View File

@ -4,6 +4,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text;
using System.Net.Http;
using NadekoBot.Services;
using System.Threading.Tasks;
@ -225,7 +226,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
throw new KeyNotFoundException("Cannot find a card by that name");
foreach (var item in items.Where(item => item.HasValues && item["img"] != null).Take(4))
{
using (var sr =await http.GetStreamAsync(item["img"].ToString()))
using (var sr = await http.GetStreamAsync(item["img"].ToString()))
{
var imgStream = new MemoryStream();
await sr.CopyToAsync(imgStream);
@ -271,7 +272,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
try
{
var items = JObject.Parse(res);
var sb = new System.Text.StringBuilder();
var sb = new StringBuilder();
sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}");
sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}");
sb.Append($"`Link:` <{await _google.ShortenUrl(items["list"][0]["permalink"].ToString()).ConfigureAwait(false)}>");
@ -464,6 +465,90 @@ $@"🌍 **Weather for** 【{obj["target"]}】
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task MCPing(IUserMessage umsg, [Remainder] string query = null)
{
var channel = (ITextChannel)umsg.Channel;
var arg = query;
if (string.IsNullOrWhiteSpace(arg))
{
await channel.SendMessageAsync("💢 Please enter a `ip:port`.").ConfigureAwait(false);
return;
}
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
using (var http = new HttpClient())
{
http.DefaultRequestHeaders.Clear();
string ip = arg.Split(':')[0];
string port = arg.Split(':')[1];
var res = await http.GetStringAsync($"https://api.minetools.eu/ping/{Uri.EscapeUriString(ip)}/{Uri.EscapeUriString(port)}").ConfigureAwait(false);
try
{
var items = JObject.Parse(res);
var sb = new StringBuilder();
int ping = (int)Math.Ceiling(Double.Parse(items["latency"].ToString()));
sb.AppendLine($"`Server:` {arg}");
sb.AppendLine($"`Version:` {items["version"]["name"].ToString()} / Protocol {items["version"]["protocol"].ToString()}");
sb.AppendLine($"`Description:` {items["description"].ToString()}");
sb.AppendLine($"`Online Players:` {items["players"]["online"].ToString()}/{items["players"]["max"].ToString()}");
sb.Append($"`Latency:` {ping}");
await channel.SendMessageAsync(sb.ToString());
}
catch
{
await channel.SendMessageAsync($"💢 Failed finding `{arg}`.").ConfigureAwait(false);
}
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task MCQ(IUserMessage umsg, [Remainder] string query = null)
{
var channel = (ITextChannel)umsg.Channel;
var arg = query;
if (string.IsNullOrWhiteSpace(arg))
{
await channel.SendMessageAsync("💢 Please enter a `ip:port`.").ConfigureAwait(false);
return;
}
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
using (var http = new HttpClient())
{
http.DefaultRequestHeaders.Clear();
try
{
string ip = arg.Split(':')[0];
string port = arg.Split(':')[1];
var res = await http.GetStringAsync($"https://api.minetools.eu/query/{Uri.EscapeUriString(ip)}/{Uri.EscapeUriString(port)}").ConfigureAwait(false);
var items = JObject.Parse(res);
var sb = new StringBuilder();
sb.AppendLine($"`Server:` {arg.ToString()} 〘Status: {items["status"]}〙");
sb.AppendLine($"`Player List:`");
for (int i = 0; i < items["Playerlist"].Count(); i++)
{
if (i == 5)
{
break;
}
else
{
sb.AppendLine($"{i + 1}. {items["Playerlist"][i]}");
}
}
sb.AppendLine($"`Online Players:` {items["Players"]} / {items["MaxPlayers"]}");
sb.AppendLine($"`Plugins:` {items["Plugins"]}");
sb.Append($"`Version:` {items["Version"]}");
await channel.SendMessageAsync(sb.ToString());
}
catch
{
await channel.SendMessageAsync($"💢 Failed finding server `{arg}`.").ConfigureAwait(false);
}
}
}
public static async Task<bool> ValidateQuery(ITextChannel ch, string query)
{
if (!string.IsNullOrEmpty(query.Trim())) return true;

View File

@ -3937,6 +3937,60 @@ namespace NadekoBot.Resources {
return ResourceManager.GetString("memegen_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to minecraftping mcping.
/// </summary>
public static string mcping_cmd {
get {
return ResourceManager.GetString("mcping_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Pings a minecraft server..
/// </summary>
public static string mcping_desc {
get {
return ResourceManager.GetString("mcping_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}mcping 127.0.0.1:1337`.
/// </summary>
public static string mcping_usage {
get {
return ResourceManager.GetString("mcping_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to minecraftquery mcq.
/// </summary>
public static string mcq_cmd {
get {
return ResourceManager.GetString("mcq_cmd", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Finds information about a minecraft server..
/// </summary>
public static string mcq_desc {
get {
return ResourceManager.GetString("mcq_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}mcq server:ip`.
/// </summary>
public static string mcq_usage {
get {
return ResourceManager.GetString("mcq_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}memegen biw &quot;gets iced coffee&quot; &quot;in the winter&quot;`.

View File

@ -2619,4 +2619,22 @@
<data name="hentaibomb_usage" xml:space="preserve">
<value>`{0}hentaibomb yuri`</value>
</data>
<data name="mcping_cmd" xml:space="preserve">
<value>minecraftping mcping</value>
</data>
<data name="mcping_desc" xml:space="preserve">
<value>Pings a minecraft server.</value>
</data>
<data name="mcping_usage" xml:space="preserve">
<value>`{0}mcping 127.0.0.1:25565`</value>
</data>
<data name="mcq_cmd" xml:space="preserve">
<value>minecraftquery mcq</value>
</data>
<data name="mcq_desc" xml:space="preserve">
<value>Finds information about a minecraft server.</value>
</data>
<data name="mcq_usage" xml:space="preserve">
<value>`{0}mcq server:ip`</value>
</data>
</root>