1
0
Fork 0

Various cleanup.

master
Marrub 2016-10-27 20:57:00 -04:00
parent 00d1b8cb81
commit 8464c6ee6c
12 changed files with 71 additions and 73 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ data
packages packages
*.swp *.swp
Makefile Makefile
.idea

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("vrobot3")] [assembly: AssemblyTitle("vrobot3")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]

View File

@ -70,7 +70,7 @@ namespace ProjectGolan.Vrobot3
var matchbox = Finder.Matches(str); var matchbox = Finder.Matches(str);
if(matchbox.Count == 0) return null; if(matchbox.Count == 0) return null;
var matches = new URI[matchbox.Count]; var matches = new URI[matchbox.Count];
for(int i = 0; i < matchbox.Count; i++) for(var i = 0; i < matchbox.Count; i++)
matches[i] = FromMatch(matchbox[i]); matches[i] = FromMatch(matchbox[i]);
return matches; return matches;
} }

View File

@ -24,8 +24,8 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Mod_Admin constructor // Mod_Admin constructor
// //
public Mod_Admin(Bot bot_) : public Mod_Admin(Bot bot) :
base(bot_) base(bot)
{ {
commands["kill"] = new BotCommandStructure{ commands["kill"] = new BotCommandStructure{
cmd = cmdKill, cmd = cmdKill,
@ -65,7 +65,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void cmdMsg(User usr, Channel channel, String msg) public void cmdMsg(User usr, Channel channel, String msg)
{ {
String[] args = Utils.GetArguments(msg, commands["msg"].help, 2, 2); var args = Utils.GetArguments(msg, commands["msg"].help, 2, 2);
bot.message(ulong.Parse(args[0]), args[1].Trim()); bot.message(ulong.Parse(args[0]), args[1].Trim());
} }
@ -74,7 +74,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void cmdAction(User usr, Channel channel, String msg) public void cmdAction(User usr, Channel channel, String msg)
{ {
String[] args = Utils.GetArguments(msg, commands["action"].help, 2, 2); var args = Utils.GetArguments(msg, commands["action"].help, 2, 2);
bot.action(ulong.Parse(args[0]), args[1].Trim()); bot.action(ulong.Parse(args[0]), args[1].Trim());
} }
} }

View File

@ -48,10 +48,10 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void run(User usr, Channel channel, String msg) public void run(User usr, Channel channel, String msg)
{ {
int n = rnd.Next(min, max); var n = rnd.Next(min, max);
String outp = String.Empty; var outp = String.Empty;
if(bot.serverInfo.hasColors && rnd.Next(0, 8) == 1) if(bot.clientInfo.hasColors && rnd.Next(0, 8) == 1)
for(int i = 0; i < 6; i++) for(int i = 0; i < 6; i++)
{ {
String[] colors = { "04", "07", "08", "09", "12", "06" }; String[] colors = { "04", "07", "08", "09", "12", "06" };
@ -61,7 +61,7 @@ namespace ProjectGolan.Vrobot3.Modules
outp += word; outp += word;
} }
else else
for(int i = 0; i < n; i++) for(var i = 0; i < n; i++)
outp += word; outp += word;
bot.reply(usr, channel, outp + final); bot.reply(usr, channel, outp + final);
@ -71,8 +71,8 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Mod_Fun constructor // Mod_Fun constructor
// //
public Mod_Fun(Bot bot_) : public Mod_Fun(Bot bot) :
base(bot_) base(bot)
{ {
commands["carmack"] = new BotCommandStructure{ commands["carmack"] = new BotCommandStructure{
cmd = new ShitpostingDevice("MM", "", 3, 20, bot).run, cmd = new ShitpostingDevice("MM", "", 3, 20, bot).run,

View File

@ -5,7 +5,7 @@
// See "LICENSE" for more information. // See "LICENSE" for more information.
// //
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// //
// Idgames search module. // Idgames search module.
// .idgames // .idgames
// //
@ -26,7 +26,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public class Mod_Idgames : IBotModule public class Mod_Idgames : IBotModule
{ {
static readonly String APIURI = private const String APIURI =
"http://doomworld.com/idgames/api/api.php"; "http://doomworld.com/idgames/api/api.php";
private Random rnd = Utils.GetRND(); private Random rnd = Utils.GetRND();
@ -34,8 +34,8 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Mod_Idgames constructor // Mod_Idgames constructor
// //
public Mod_Idgames(Bot bot_) : public Mod_Idgames(Bot bot) :
base(bot_) base(bot)
{ {
commands["idgames"] = new BotCommandStructure{ commands["idgames"] = new BotCommandStructure{
cmd = cmdIdgames, cmd = cmdIdgames,
@ -51,11 +51,11 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void cmdIdgames(User usr, Channel channel, String msg) public void cmdIdgames(User usr, Channel channel, String msg)
{ {
String[] args = var args = Utils.GetArguments(msg, commands["idgames"].help, 0, 3);
Utils.GetArguments(msg, commands["idgames"].help, 0, 3);
switch(args.Length) switch(args.Length)
{ {
default:
case 1: case 1:
int id; int id;
if(args[0].Trim().Length == 0) if(args[0].Trim().Length == 0)
@ -67,10 +67,7 @@ namespace ProjectGolan.Vrobot3.Modules
break; break;
case 2: idgames(usr, channel, args[0], args[1]); break; case 2: idgames(usr, channel, args[0], args[1]); break;
case 3: case 3:
if(args[2].Trim().ToLower() == "random") idgames(usr, channel, args[0], args[1], args[2].Trim());
idgames(usr, channel, args[0], args[1], "random");
else
idgames(usr, channel, args[0], args[1], args[2].Trim());
break; break;
} }
} }
@ -82,6 +79,8 @@ namespace ProjectGolan.Vrobot3.Modules
{ {
var req = WebRequest.Create("http://doomworld.com/idgames/?random") var req = WebRequest.Create("http://doomworld.com/idgames/?random")
as HttpWebRequest; as HttpWebRequest;
if(req == null) throw new CommandArgumentException("fug it borked");
req.Referer = "http://doomworld.com/idgames/"; req.Referer = "http://doomworld.com/idgames/";
bot.message(channel, bot.message(channel,
Discord.Format.Escape(req.GetResponse().ResponseUri.ToString())); Discord.Format.Escape(req.GetResponse().ResponseUri.ToString()));
@ -94,6 +93,7 @@ namespace ProjectGolan.Vrobot3.Modules
{ {
var req = WebRequest.Create(APIURI + "?action=get&id=" + id) var req = WebRequest.Create(APIURI + "?action=get&id=" + id)
as HttpWebRequest; as HttpWebRequest;
if(req == null) throw new CommandArgumentException("fug it borked");
using(var response = req.GetResponse()) using(var response = req.GetResponse())
{ {
@ -120,9 +120,9 @@ namespace ProjectGolan.Vrobot3.Modules
private void idgames(User usr, Channel channel, String inquiry, private void idgames(User usr, Channel channel, String inquiry,
String type = "title", String pos = "1") String type = "title", String pos = "1")
{ {
int ipos = 0; var ipos = 0;
if(pos != "random") if(pos.ToLower() != "random")
{ {
Utils.TryParse(pos, "Invalid position.", out ipos); Utils.TryParse(pos, "Invalid position.", out ipos);
@ -145,8 +145,8 @@ namespace ProjectGolan.Vrobot3.Modules
if(!validtypes.Contains(type)) if(!validtypes.Contains(type))
throw new CommandArgumentException("Invalid inquiry type."); throw new CommandArgumentException("Invalid inquiry type.");
String uri = APIURI + "?action=search&sort=rating&query=" + var uri = APIURI + "?action=search&sort=rating&query=" + inquiry +
inquiry + "&type=" + type; "&type=" + type;
var req = WebRequest.Create(uri); var req = WebRequest.Create(uri);
Console.WriteLine("idgames query: {0}", uri); Console.WriteLine("idgames query: {0}", uri);
@ -167,7 +167,7 @@ namespace ProjectGolan.Vrobot3.Modules
if(pos == "random") ipos = rnd.Next(0, x_titles.Count()); if(pos == "random") ipos = rnd.Next(0, x_titles.Count());
if(ipos >= x_titles.Count()) ipos = x_titles.Count() - 1; if(ipos >= x_titles.Count()) ipos = x_titles.Count() - 1;
String title = x_titles.ElementAtOrDefault(ipos); var title = x_titles.ElementAtOrDefault(ipos) ?? "invalid title";
if(title.Trim().Length > 0) title = "[ " + title + " ] "; if(title.Trim().Length > 0) title = "[ " + title + " ] ";
bot.message(channel, bot.message(channel,

View File

@ -29,9 +29,8 @@ namespace ProjectGolan.Vrobot3.Modules
public int numQuotes; public int numQuotes;
} }
static readonly String APIURI = "http://www.greyserv.net/qdb/q/"; const String APIURI = "http://www.greyserv.net/qdb/q/";
static readonly String InterfaceURI = const String InterfaceURI = "http://www.greyserv.net/qdb/interface.cgi";
"http://www.greyserv.net/qdb/interface.cgi";
private Random rnd = Utils.GetRND(); private Random rnd = Utils.GetRND();
// //
@ -86,7 +85,7 @@ namespace ProjectGolan.Vrobot3.Modules
else else
foreach(var ln_ in lines) foreach(var ln_ in lines)
{ {
String ln = ln_.Trim(); var ln = ln_.Trim();
if(ln.Length > 0) if(ln.Length > 0)
bot.message(channel, ln); bot.message(channel, ln);
} }

View File

@ -15,7 +15,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public class Mod_Shittalk : IBotModule public class Mod_Shittalk : IBotModule
{ {
private Random rnd = Utils.GetRND(); private readonly Random rnd = Utils.GetRND();
// //
// Mod_Shittalk constructor // Mod_Shittalk constructor
@ -49,7 +49,8 @@ namespace ProjectGolan.Vrobot3.Modules
"%s DESERVES AN AWARD. AN AWARD FOR BEING UGLY.", "%s DESERVES AN AWARD. AN AWARD FOR BEING UGLY.",
"MAN SOMETIMES I REALLY WANT TO PUNCH %s IN THE GOD DAMN FACE", "MAN SOMETIMES I REALLY WANT TO PUNCH %s IN THE GOD DAMN FACE",
"THERE IS SOMETHING WRONG IN THIS CHANNEL. THAT SOMETHING IS %s.", "THERE IS SOMETHING WRONG IN THIS CHANNEL. THAT SOMETHING IS %s.",
"%s IS A TOTAL SCRUB", "%s IS THE CONDUCTOR OF THE JELLY TRAIN", "%s IS A TOTAL SCRUB",
"%s IS THE CONDUCTOR OF THE JELLY TRAIN",
"%s IS A THING THAT SMELLS BAD MAYBE", "%s IS A THING THAT SMELLS BAD MAYBE",
"%s IS A PILE OF FAIL", "%s IS A PILE OF FAIL",
"%s IS NOT AS COOL AS VROBOT", "%s IS NOT AS COOL AS VROBOT",
@ -103,10 +104,11 @@ namespace ProjectGolan.Vrobot3.Modules
"I AM HERE TO FIGHT THE CANCER THAT AFFLICTS US ALL. NAMELY, %s.", "I AM HERE TO FIGHT THE CANCER THAT AFFLICTS US ALL. NAMELY, %s.",
"WELP, %s IS HERE", "WELP, %s IS HERE",
"OH HAI %s", "OH HAI %s",
"marrub pls upgrade my processor i can't even count to eleventy" "marrub pls upgrade my processor i can't even count to eleventy",
"THIS WAS ALL %s'S FAULT"
}; };
String choice = var choice =
shittalk[rnd.Next(shittalk.Length)].Replace("%s", usr.name); shittalk[rnd.Next(shittalk.Length)].Replace("%s", usr.name);
if(choice.StartsWith("%m ")) if(choice.StartsWith("%m "))

View File

@ -21,13 +21,13 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public class Mod_Utils : IBotModule public class Mod_Utils : IBotModule
{ {
private Random rnd = Utils.GetRND(); private readonly Random rnd = Utils.GetRND();
// //
// Mod_Utils constructor // Mod_Utils constructor
// //
public Mod_Utils(Bot bot_) : public Mod_Utils(Bot bot) :
base(bot_) base(bot)
{ {
commands["rand"] = new BotCommandStructure{ commands["rand"] = new BotCommandStructure{
cmd = cmdRand, cmd = cmdRand,
@ -43,7 +43,7 @@ namespace ProjectGolan.Vrobot3.Modules
"Example: .help\n" + "Example: .help\n" +
"Example: .help eightball" "Example: .help eightball"
}; };
commands["decide"] = new BotCommandStructure{ commands["decide"] = new BotCommandStructure{
cmd = cmdDecide, cmd = cmdDecide,
help = "Decides between 2 or more choices.\n" + help = "Decides between 2 or more choices.\n" +
@ -73,9 +73,8 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void cmdRand(User usr, Channel channel, String msg) public void cmdRand(User usr, Channel channel, String msg)
{ {
String[] args = var args = Utils.GetArguments(msg, commands["rand"].help, 1, 2, ' ');
Utils.GetArguments(msg, commands["rand"].help, 1, 2, ' '); double max = 0.0, min = 0.0;
Double max = 0.0, min = 0.0;
Utils.TryParse(args[0].Trim(), "Invalid maximum.", out max); Utils.TryParse(args[0].Trim(), "Invalid maximum.", out max);
@ -103,7 +102,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
public void cmdDecide(User usr, Channel channel, String msg) public void cmdDecide(User usr, Channel channel, String msg)
{ {
String[] args = Utils.GetArguments(msg, commands["decide"].help, 2); var args = Utils.GetArguments(msg, commands["decide"].help, 2);
bot.reply(usr, channel, args[rnd.Next(args.Length)].Trim()); bot.reply(usr, channel, args[rnd.Next(args.Length)].Trim());
} }
@ -112,7 +111,7 @@ namespace ProjectGolan.Vrobot3.Modules
// //
private void helpList(Channel channel, bool admin) private void helpList(Channel channel, bool admin)
{ {
String outp = String.Empty; var outp = String.Empty;
var en = var en =
from kvp in bot.cmdfuncs from kvp in bot.cmdfuncs
let f = kvp.Value.Item2.flags let f = kvp.Value.Item2.flags
@ -144,7 +143,7 @@ namespace ProjectGolan.Vrobot3.Modules
if(bot.cmdfuncs.ContainsKey(cmdname)) if(bot.cmdfuncs.ContainsKey(cmdname))
{ {
var str = bot.cmdfuncs[cmdname].Item2.help; var str = bot.cmdfuncs[cmdname].Item2.help;
if(!bot.serverInfo.hasNewlines) str.Replace("\n", " || "); if(!bot.clientInfo.hasNewlines) str = str.Replace("\n", " || ");
bot.message(channel, str ?? "No help available for this command."); bot.message(channel, str ?? "No help available for this command.");
} }
else else

View File

@ -40,10 +40,10 @@ namespace ProjectGolan.Vrobot3
public BotInfo[] servers; public BotInfo[] servers;
} }
private List<Bot> bots = new List<Bot>(); private readonly List<Bot> bots = new List<Bot>();
private List<Thread> threads = new List<Thread>(); private readonly List<Thread> threads = new List<Thread>();
public String dataDir = "../data"; public String dataDir = "../data";
public ProgramInfo info; public ProgramInfo info;
public static Program Instance; public static Program Instance;
@ -69,8 +69,8 @@ namespace ProjectGolan.Vrobot3
info = config.info; info = config.info;
foreach(var info in config.servers) foreach(var server in config.servers)
threads.AddItem(new Thread(bots.AddItem(new Bot(info)).connect)).Start(); threads.AddItem(new Thread(bots.AddItem(new Bot(server)).connect)).Start();
} }
catch(Exception exc) catch(Exception exc)
{ {
@ -91,6 +91,7 @@ namespace ProjectGolan.Vrobot3
File.WriteAllText(dataDir + "/disconnectexcdump.txt", File.WriteAllText(dataDir + "/disconnectexcdump.txt",
exc.ToString()); exc.ToString());
} }
bots.Clear(); bots.Clear();
threads.Clear(); threads.Clear();
} }

View File

@ -39,7 +39,7 @@ namespace ProjectGolan.Vrobot3
public static Random GetRND() public static Random GetRND()
{ {
RNDHash *= DateTime.UtcNow.ToFileTime(); RNDHash *= DateTime.UtcNow.ToFileTime();
Random rnd = new Random(unchecked((int)(RNDHash & 0x7fffffff))); var rnd = new Random(unchecked((int)(RNDHash & 0x7fffffff)));
RNDHash ^= 0x7f8f8f8f8f8f8f8f; RNDHash ^= 0x7f8f8f8f8f8f8f8f;
RNDHash >>= 4; RNDHash >>= 4;
RNDHash += 0x7f0000007f000000; RNDHash += 0x7f0000007f000000;
@ -58,10 +58,7 @@ namespace ProjectGolan.Vrobot3
if(min == 1 && msg == String.Empty) if(min == 1 && msg == String.Empty)
throw new CommandArgumentException(help); throw new CommandArgumentException(help);
if(max == 0) split = max == 0 ? msg.Split(splitseq) : msg.Split(splitseq, max);
split = msg.Split(splitseq);
else
split = msg.Split(splitseq, max);
if(min >= 0 && split.Length < min) if(min >= 0 && split.Length < min)
throw new CommandArgumentException(help); throw new CommandArgumentException(help);
@ -72,7 +69,7 @@ namespace ProjectGolan.Vrobot3
// //
// SetRange // SetRange
// //
public static Double SetRange(Double x, Double min, Double max) public static double SetRange(double x, double min, double max)
=> ((max - min) * x) + min; => ((max - min) * x) + min;
// //
@ -80,15 +77,15 @@ namespace ProjectGolan.Vrobot3
// //
public static String FuzzyRelativeDate(DateTime then, DateTime now) public static String FuzzyRelativeDate(DateTime then, DateTime now)
{ {
TimeSpan span = now.Subtract(then); var span = now.Subtract(then);
if(span.Seconds == 0) if(span.Seconds == 0)
return "now"; return "now";
String denom = span.Days > 0 ? "day" : var denom = span.Days > 0 ? "day" :
span.Hours > 0 ? "hour" : span.Hours > 0 ? "hour" :
span.Minutes > 0 ? "minute" : span.Minutes > 0 ? "minute" :
"second"; "second";
int number; int number;
switch(denom) switch(denom)
@ -100,8 +97,7 @@ namespace ProjectGolan.Vrobot3
case "day": number = span.Days; break; case "day": number = span.Days; break;
} }
return String.Format("{0} {1}{2} ago", number, denom, return $"{number} {denom}{number != 1 ? "s" : String.Empty} ago";
number != 1 ? "s" : String.Empty);
} }
// //
@ -117,11 +113,12 @@ namespace ProjectGolan.Vrobot3
{ {
try try
{ {
byte[] bufp = new byte[maxsize]; var bufp = new byte[maxsize];
int read; var read = 0;
using(var stream = resp.GetResponseStream()) using(var stream = resp.GetResponseStream())
read = stream.Read(bufp, 0, maxsize); if(stream != null)
read = stream.Read(bufp, 0, maxsize);
return Encoding.Default.GetString(bufp, 0, read); return Encoding.Default.GetString(bufp, 0, read);
} }

View File

@ -5,14 +5,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "vrobot3", "vrobot3.csproj",
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86 Debug|AnyCPU = Debug|AnyCPU
Release|x86 = Release|x86 Release|AnyCPU = Release|AnyCPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{83337FF3-3334-42EC-824D-532FF0C973A9}.Debug|x86.ActiveCfg = Debug|x86 {83337FF3-3334-42EC-824D-532FF0C973A9}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU
{83337FF3-3334-42EC-824D-532FF0C973A9}.Debug|x86.Build.0 = Debug|x86 {83337FF3-3334-42EC-824D-532FF0C973A9}.Debug|AnyCPU.Build.0 = Debug|AnyCPU
{83337FF3-3334-42EC-824D-532FF0C973A9}.Release|x86.ActiveCfg = Release|x86 {83337FF3-3334-42EC-824D-532FF0C973A9}.Release|AnyCPU.ActiveCfg = Release|AnyCPU
{83337FF3-3334-42EC-824D-532FF0C973A9}.Release|x86.Build.0 = Release|x86 {83337FF3-3334-42EC-824D-532FF0C973A9}.Release|AnyCPU.Build.0 = Release|AnyCPU
EndGlobalSection EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0 Policies = $0