Created
August 19, 2016 05:55
-
-
Save mzabsky/d0e388752516c3dd83b00fea5ba6fa68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using Discord; | |
namespace PlaneSculptorsDiscordBot | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var client = new DiscordClient(); | |
Server server = null; | |
client.MessageReceived += async (s, e) => | |
{ | |
/*if (!e.Message.IsAuthor) | |
await e.Channel.SendMessage(e.Message.Text);*/ | |
if (e.Channel.Server == server/* && e.User.Name == "mzabsky"*/) | |
{ | |
try | |
{ | |
// Parse the message | |
var regex = @"\[\[((?<set>[^:]+|[0-9]+|[0-9]+):((?<version>[a-z][a-z0-9-]+|[0-9]+|[0-9]+):)?)?(?<card>[^:]+?|[0-9]+)?\]\]"; | |
var match = Regex.Match(e.Message.Text, regex); | |
if (!match.Success) return; | |
var context = e.Channel.Name; | |
var url = $@"http://www.planesculptors.net/autocard?context={context}&contextVersion=&set={match.Groups["set"]}&setVersion={match.Groups["version"]}&card={match.Groups["card"]}&bot"; | |
Console.WriteLine($"#{e.Channel} {e.User}: {e.Message} -> {url}"); | |
// Download card data from PlaneSculptors.net | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | |
var response = request.GetResponse(); | |
var responseStream = response.GetResponseStream(); | |
var responseContentStream = new StreamReader(responseStream, Encoding.UTF8); | |
var responseContent = await responseContentStream.ReadToEndAsync(); | |
// Respond to the message | |
if (responseContent.Length > 2000) | |
{ | |
await e.Channel.SendMessage(match.Groups["card"].ToString() + " not found"); | |
} | |
else | |
{ | |
await e.Channel.SendMessage(responseContent); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
} | |
}; | |
client.ExecuteAndWait(async () => { | |
await client.Connect("<discord username>", "<password>"); | |
server = client.Servers.Single(p => p.Name == "Custom Magic"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment