Browse Source

Moved intilize table for Module to the main bot class.

Added 'registered' field to the nickinfo table to determine if the nick is registered or not.
tags/3.1.0
Teknikode 3 years ago
parent
commit
c6147894c4
4 changed files with 40 additions and 15 deletions
  1. 12
    0
      Combot/Bot.cs
  2. 1
    1
      Combot/Combot.csproj
  3. 2
    1
      Combot/CreateTable.sql
  4. 25
    13
      Combot/Modules/Module.cs

+ 12
- 0
Combot/Bot.cs View File

@@ -63,9 +63,21 @@ namespace Combot
Database = new Database(serverConfig.Database);
Database.MysqlErrorEvent += HandleMysqlErrorEvent;

InitializeTable();

LoadModules();
}

private void InitializeTable()
{
string sqlPath = Path.Combine(Directory.GetCurrentDirectory(), "CreateTable.sql");
if (File.Exists(sqlPath))
{
string query = File.ReadAllText(sqlPath);
Database.Execute(query);
}
}

/// <summary>
/// Trys to connect to one of the IPs of the given hostname. If the connection was successful, it will login the nick.
/// </summary>

+ 1
- 1
Combot/Combot.csproj View File

@@ -118,7 +118,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Content Include="Modules\CreateTable.sql">
<Content Include="CreateTable.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

Combot/Modules/CreateTable.sql → Combot/CreateTable.sql View File

@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS `servers` (
CREATE TABLE IF NOT EXISTS `channels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`server_id` int(11) NOT NULL,
`requester_id` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=0 ;

@@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS `nickinfo` (
`username` varchar(50) NULL,
`realname` varchar(50) NULL,
`host` varchar(100) NULL,
`registered` boolean NOT NULL DEFAULT false,
`date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=0 ;

+ 25
- 13
Combot/Modules/Module.cs View File

@@ -45,8 +45,6 @@ namespace Combot.Modules

public Module()
{
InitializeTable();

SetDefaults();
ConfigRWLock = new ReaderWriterLockSlim();
ConfigFileRWLock = new ReaderWriterLockSlim();
@@ -56,16 +54,6 @@ namespace Combot.Modules
JsonSettings.Formatting = Formatting.Indented;
}

private void InitializeTable()
{
string sqlPath = Path.Combine(Directory.GetCurrentDirectory(), "CreateTable.sql");
if (File.Exists(sqlPath))
{
string query = File.ReadAllText(sqlPath);
Bot.Database.Execute(query);
}
}

public void HandleCommandEvent(CommandMessage command)
{
// Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
@@ -289,6 +277,7 @@ namespace Combot.Modules

public void AddNick(Nick nick)
{
// Let's see if the nick already is added
string search = "SELECT * FROM `nicks` WHERE " +
"`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
"`nickname` = {1}";
@@ -301,13 +290,25 @@ namespace Combot.Modules
"`nickname` = {1}";
Bot.Database.Execute(insert, new object[] { Bot.ServerConfig.Name, nick.Nickname });
}
// search for the nick's modes (if any)
foreach (Channel channel in Bot.IRC.Channels)
{
if (channel.Nicks.Exists(chn => chn.Nickname == nick.Nickname))
{
Nick foundNick = channel.Nicks.Find(chn => chn.Nickname == nick.Nickname);
nick.AddModes(foundNick.Modes);
nick.AddPrivileges(foundNick.Privileges);
}
}

// Now add that nick's info to the db
AddNickInfo(nick);
}

public void AddNickInfo(Nick nickInfo)
{
int argIndex = 2;
string search = "SELECT * FROM `nickinfo` WHERE " +
string search = "SELECT `nickinfo`.`id` FROM `nickinfo` WHERE " +
"`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `nicks`.`server_id` = `servers`.`id` WHERE `nicks`.`nickname` = {0} AND `servers`.`name` = {1})";
List<object> argList = new List<object>() { nickInfo.Nickname, Bot.ServerConfig.Name };
if (!string.IsNullOrEmpty(nickInfo.Username))
@@ -351,9 +352,20 @@ namespace Combot.Modules
{
insert += ", `host` = {" + argIndex + "}";
argList.Add(nickInfo.Host);
argIndex++;
}
if (nickInfo.Modes.Any() && nickInfo.Modes.Contains(UserMode.r))
{
insert += ", `registered` = {" + argIndex + "}";
argList.Add(true);
}
Bot.Database.Execute(insert, argList.ToArray());
}
else
{
string update = "UPDATE `nickinfo` SET `registered` = {0} WHERE `id` = {1}";
Bot.Database.Execute(update, new object[] { (nickInfo.Modes.Any() && nickInfo.Modes.Contains(UserMode.r)), results.First()["id"] });
}
}

public string GetNickname(int id)

Loading…
Cancel
Save