123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Combot.Databases;
- using Combot.IRCServices.Messaging;
-
- namespace Combot.Modules.Plugins
- {
- public class Introductions : Module
- {
- public override void Initialize()
- {
- Bot.CommandReceivedEvent += HandleCommandEvent;
- Bot.IRC.Message.JoinChannelEvent += HandleJoinEvent;
- }
-
- public override void ParseCommand(CommandMessage command)
- {
- Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
- switch (foundCommand.Name)
- {
- case "Introduction":
- string method = command.Arguments["Method"];
- switch (method.ToLower())
- {
- case "add":
- AddIntroduction(command);
- break;
- case "edit":
- EditIntroduction(command);
- break;
- case "del":
- DeleteIntroduction(command);
- break;
- case "view":
- ViewIntroduction(command);
- break;
- }
- break;
- }
- }
-
- private void HandleJoinEvent(object sender, JoinChannelInfo info)
- {
- if (!Bot.ServerConfig.ChannelBlacklist.Contains(info.Channel)
- && !Bot.ServerConfig.NickBlacklist.Contains(info.Nick.Nickname)
- && !ChannelBlacklist.Contains(info.Channel)
- && !NickBlacklist.Contains(info.Nick.Nickname))
- {
- List<Dictionary<string, object>> results = GetIntroductionList(info.Channel, info.Nick.Nickname);
- if (results.Any())
- {
- Random randNum = new Random();
- int index = randNum.Next(0, results.Count);
- Dictionary<string, object> intro = results[index];
- Bot.IRC.SendPrivateMessage(info.Channel, string.Format("\u200B{0}", intro["message"]));
- }
- }
- }
-
- private void AddIntroduction(CommandMessage command)
- {
- string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
- List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
-
- if (results.Count < Convert.ToInt32(GetOptionValue("Max Introductions")))
- {
- AddChannel(channel);
- AddNick(command.Nick.Nickname);
- string query = "INSERT INTO `introductions` SET " +
- "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
- "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
- "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
- "`message` = {5}, " +
- "`date_added` = {6}";
- Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, Bot.ServerConfig.Name, command.Nick.Nickname, command.Arguments["Message"], command.TimeStamp });
- string introMessage = string.Format("Added introduction. You now have \u0002{0}\u0002 introductions set.", results.Count + 1);
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
- }
- else
- {
- string maxMessage = "You already have the maximum number of introductions for this channel. Delete one before trying to add another.";
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, maxMessage);
- }
- }
-
- /* Returns the parsed ID field if valid, otherwise returns 0 */
- private int HasValidIntroductionID(CommandMessage command)
- {
- string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
- int num = 0;
- int ret = 0;
- List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
-
- if (int.TryParse(command.Arguments["ID"], out num))
- {
- if (results.Count >= num && num > 0)
- {
- ret = num;
- }
- }
-
- return ret;
- }
-
- private void EditIntroduction(CommandMessage command)
- {
- string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
- List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
- int num = HasValidIntroductionID( command );
-
- if (num > 0){
- int id = Convert.ToInt32(results[num - 1]["id"]);
- string query = "UPDATE `introductions` SET " +
- "`message` = {0} " +
- "WHERE `id` = {1}";
- Bot.Database.Execute(query, new object[] { command.Arguments["Message"], id });
- string introMessage = string.Format("Introduction #\u0002{0}\u0002 is now: {1}", num, command.Arguments["Message"]);
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
- }
- else
- {
- string invalid = "Invalid introduction ID.";
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
- }
- }
-
- private void DeleteIntroduction(CommandMessage command)
- {
- string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
- List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
- int num = HasValidIntroductionID( command );
-
- if (num > 0){
- int id = Convert.ToInt32(results[num - 1]["id"]);
- string query = "DELETE FROM `introductions` " +
- "WHERE `id` = {0}";
- Bot.Database.Execute(query, new object[] { id });
- string introMessage = string.Format("Introduction #\u0002{0}\u0002 has been deleted.", num);
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
- }
- else
- {
- string invalid = "Invalid introduction ID.";
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
- }
- }
-
- private void ViewIntroduction(CommandMessage command)
- {
- string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
- List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
- int num = 0;
-
- if (command.Arguments.ContainsKey("ID"))
- {
- num = HasValidIntroductionID( command );
- if ( num > 0 ){
- string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", num, results[num - 1]["message"]);
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
- }
- else
- {
- string invalid = "Invalid introduction ID.";
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
- }
- }
- else
- {
- if (results.Any())
- {
- for (int i = 0; i < results.Count; i++)
- {
- string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", i + 1, results[i]["message"]);
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
- }
- }
- else
- {
- string invalid = "You do not have any introductions set.";
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
- }
- }
- }
-
- private List<Dictionary<string, object>> GetIntroductionList(string channel, string nickname)
- {
- // Check to see if they have reached the max number of introductions
- string search = "SELECT `introductions`.`id`, `introductions`.`message` FROM `introductions` " +
- "INNER JOIN `nicks` " +
- "ON `introductions`.`nick_id` = `nicks`.`id` " +
- "INNER JOIN `channels` " +
- "ON `introductions`.`channel_id` = `channels`.`id` " +
- "INNER JOIN `servers` " +
- "ON `nicks`.`server_id` = `servers`.`id` " +
- "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
- return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
- }
- }
- }
|