2016-10-12 21:58:16 -07:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2016 Project Golan
|
|
|
|
|
//
|
|
|
|
|
// See "LICENSE" for more information.
|
|
|
|
|
//
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Doominati Quote DB interface command.
|
|
|
|
|
// .quote
|
|
|
|
|
//
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace ProjectGolan.Vrobot3.Modules
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// Mod_Quote
|
|
|
|
|
//
|
|
|
|
|
public class Mod_Quote : IBotModule
|
|
|
|
|
{
|
|
|
|
|
private struct QDBInterface
|
|
|
|
|
{
|
|
|
|
|
public int numQuotes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 17:57:00 -07:00
|
|
|
|
const String APIURI = "http://www.greyserv.net/qdb/q/";
|
|
|
|
|
const String InterfaceURI = "http://www.greyserv.net/qdb/interface.cgi";
|
2016-10-12 21:58:16 -07:00
|
|
|
|
private Random rnd = Utils.GetRND();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Mod_Quote constructor
|
|
|
|
|
//
|
|
|
|
|
public Mod_Quote(Bot bot_) :
|
|
|
|
|
base(bot_)
|
|
|
|
|
{
|
|
|
|
|
commands["quote"] = new BotCommandStructure{
|
2017-02-12 21:13:17 -08:00
|
|
|
|
cmd = cmdQuote,
|
2016-10-12 21:58:16 -07:00
|
|
|
|
help = "Get a quote from the Doominati Quote DB.\n" +
|
|
|
|
|
"Syntax: .quote [id]\n" +
|
|
|
|
|
"Example: .quote 536"
|
|
|
|
|
};
|
2017-02-12 21:13:17 -08:00
|
|
|
|
|
|
|
|
|
postSetup();
|
2016-10-12 21:58:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// cmdQuote
|
|
|
|
|
//
|
|
|
|
|
public void cmdQuote(User usr, Channel channel, String msg)
|
|
|
|
|
{
|
|
|
|
|
var inter = JsonConvert.DeserializeObject<QDBInterface>(
|
|
|
|
|
Utils.GetResponseString(InterfaceURI, 64));
|
|
|
|
|
|
|
|
|
|
int id;
|
|
|
|
|
|
|
|
|
|
if(String.IsNullOrEmpty(msg?.Trim()) || !int.TryParse(msg, out id))
|
|
|
|
|
id = rnd.Next(inter.numQuotes);
|
|
|
|
|
else if(id < 0 || id > inter.numQuotes)
|
|
|
|
|
throw new CommandArgumentException("invalid quote ID");
|
|
|
|
|
|
|
|
|
|
var quote = Utils.GetResponseString(APIURI + id.ToString(),
|
2016-10-27 23:48:58 -07:00
|
|
|
|
bot.clientInfo.messageSafeMaxLen);
|
2016-10-12 21:58:16 -07:00
|
|
|
|
|
|
|
|
|
if(String.IsNullOrEmpty(quote))
|
|
|
|
|
throw new CommandArgumentException("QDB exploded try again later");
|
|
|
|
|
|
2016-10-27 23:48:58 -07:00
|
|
|
|
if(bot.clientInfo.shortMessages)
|
2016-10-12 21:58:16 -07:00
|
|
|
|
quote = Regex.Replace(quote, "\n+", "\n").Trim();
|
|
|
|
|
|
|
|
|
|
var lines = quote.Split('\n');
|
|
|
|
|
|
2016-10-27 23:48:58 -07:00
|
|
|
|
if(bot.clientInfo.shortMessages &&
|
2016-10-12 21:58:16 -07:00
|
|
|
|
(lines.Length > 5 || quote.Length > 600))
|
|
|
|
|
{
|
|
|
|
|
bot.reply(usr, channel, "Quote is too long.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 23:48:58 -07:00
|
|
|
|
if(bot.clientInfo.hasNewlines)
|
2016-10-12 21:58:16 -07:00
|
|
|
|
bot.message(channel, quote);
|
|
|
|
|
else
|
|
|
|
|
foreach(var ln_ in lines)
|
|
|
|
|
{
|
2016-10-27 17:57:00 -07:00
|
|
|
|
var ln = ln_.Trim();
|
2016-10-12 21:58:16 -07:00
|
|
|
|
if(ln.Length > 0)
|
|
|
|
|
bot.message(channel, ln);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|