Skip to content

Commit

Permalink
Merge pull request #49 from MJHeijster/develop
Browse files Browse the repository at this point in the history
Merging dead chat check and admin commands.
  • Loading branch information
MJHeijster committed Jun 5, 2022
2 parents a6655d0 + 15cc735 commit 0e60c48
Show file tree
Hide file tree
Showing 37 changed files with 761 additions and 104 deletions.
64 changes: 63 additions & 1 deletion SharedCode/Settings/BotSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public Application(IConfiguration configuration)
ShowDiscrim = configuration.GetValue<bool>("Application:ShowDiscrim");
ShowAvatar = configuration.GetValue<bool>("Application:ShowAvatar");
NicksFileManual = configuration.GetValue<string>("Application:NicksFileManual");
DeadChatAfter = configuration.GetValue<int>("Application.DeadChatAfter");
}

/// <summary>
Expand Down Expand Up @@ -118,6 +119,61 @@ public Application(IConfiguration configuration)
/// </summary>
/// <value>The nicks file manual.</value>
public string NicksFileManual { get; set; }
/// <summary>
/// Gets or sets the time after which the chat is considered dead.
/// </summary>
/// <value>The time after which the chat is considered dead.</value>
public int DeadChatAfter { get; set; }
}

/// <summary>
/// Class AdminCommands.
/// </summary>
public class AdminCommands
{
/// <summary>
/// Initializes a new instance of the <see cref="AdminCommands"/> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public AdminCommands(IConfiguration configuration)
{
AdminUserId = configuration.GetValue<ulong>("Discord:Commands:AdminCommands:AdminUserId");
AllowServerAdmins = configuration.GetValue<bool>("Discord:Commands:AdminCommands:AllowServerAdmins");
LinkUserCommand = configuration.GetValue<string>("Discord:Commands:AdminCommands:LinkUserCommand");
OverrideUsernameCommand = configuration.GetValue<string>("Discord:Commands:AdminCommands:OverrideUsernameCommand");
RemoveOverrideUsernameCommand = configuration.GetValue<string>("Discord:Commands:AdminCommands:RemoveOverrideUsernameCommand");
CreateOldUserCommand = configuration.GetValue<string>("Discord:Commands:AdminCommands:CreateOldUserCommand");
}
/// <summary>
/// Gets or sets the admin user identifier.
/// </summary>
/// <value>The admin user identifier.</value>
public ulong AdminUserId { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [server admins are allowed to use these commands].
/// </summary>
/// <value><c>true</c> if [server admins are allowed to use these commands]; otherwise, <c>false</c>.</value>
public bool AllowServerAdmins { get; set; }
/// <summary>
/// Gets or sets the link user command.
/// </summary>
/// <value>The link user command.</value>
public string LinkUserCommand { get; set; }
/// <summary>
/// Gets or sets the override username command.
/// </summary>
/// <value>The override username command.</value>
public string OverrideUsernameCommand { get; set; }
/// <summary>
/// Gets or sets the remove override username command.
/// </summary>
/// <value>The remove override username command.</value>
public string RemoveOverrideUsernameCommand { get; set; }
/// <summary>
/// Gets or sets the create old user command.
/// </summary>
/// <value>The create old user command.</value>
public string CreateOldUserCommand { get; set; }
}

/// <summary>
Expand All @@ -135,6 +191,7 @@ public Commands(IConfiguration configuration)
Exclude = configuration.GetValue<string>("Discord:Commands:Exclude");
Include = configuration.GetValue<string>("Discord:Commands:Include");
Stats = new Stats(configuration);
AdminCommands = new AdminCommands(configuration);
}

/// <summary>
Expand All @@ -157,6 +214,11 @@ public Commands(IConfiguration configuration)
/// </summary>
/// <value>The stats.</value>
public Stats Stats { get; set; }
/// <summary>
/// Gets or sets the admin commands.
/// </summary>
/// <value>The admin commands.</value>
public AdminCommands AdminCommands { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -289,7 +351,7 @@ public PushOver(IConfiguration configuration)
/// </summary>
/// <value><c>true</c> if [use pushover]; otherwise, <c>false</c>.</value>

[JsonIgnore]
[JsonIgnore]
public bool UsePushover { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion StatBot/Classes/LogFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 12-11-2017
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 15-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="LogFile.cs">
// Copyright © 2022
Expand Down
66 changes: 64 additions & 2 deletions StatBot/Database/DatabaseHandlers/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
// Created : 17-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 30-05-2022
// ***********************************************************************
// <copyright file="DatabaseHandler.cs">
// Copyright © 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
using Discord.WebSocket;
using Microsoft.Data.Sqlite;
using StatBot.Handlers;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -26,6 +28,10 @@ namespace StatBot.Database.DatabaseHandlers
/// </summary>
internal class DatabaseHandler
{
/// <summary>
/// The database version
/// </summary>
private static int databaseVersion = 1;
/// <summary>
/// Creates the database.
/// </summary>
Expand All @@ -35,7 +41,7 @@ internal static void CreateDatabase()
{
try
{
new SqliteConnection("Data Source=Database\\Statbot.db");
new SqliteConnection("Data Source=Database\\Statbot.db");
}
catch { }
using var con = new SqliteConnection("Data Source=Database\\Statbot.db");
Expand All @@ -48,5 +54,61 @@ internal static void CreateDatabase()
}
}
}
/// <summary>
/// Updates the database.
/// </summary>
/// <param name="logHandler">The log handler.</param>
/// <param name="client">The client.</param>
internal static void UpdateDatabase(LogHandler logHandler, DiscordSocketClient client)
{
long version = 0;
try
{
new SqliteConnection("Data Source=Database\\Statbot.db");
}
catch { }
string command = $"SELECT Version from Database";
using (var connection = new SqliteConnection("Data Source=Database\\Statbot.db;"))
{
connection.Open();
using (var cmd = new SqliteCommand(command, connection))
{
var reader = cmd.ExecuteReader();
if (reader != null &&
reader.HasRows)
{
reader.Read();
version = (long)reader[0];
}
}
}
for (long i = version; i < databaseVersion; i++)
UpdateVersion(i+1, logHandler, client);
}

/// <summary>
/// Updates the version in the database.
/// </summary>
/// <param name="version">The version.</param>
/// <param name="logHandler">The log handler.</param>
/// <param name="client">The client.</param>
private static void UpdateVersion(long version, LogHandler logHandler, DiscordSocketClient client)
{
using var con = new SqliteConnection($"Data Source=Database\\Statbot.db");
try
{
con.Open();
var sqlScript = File.ReadAllText($"Database\\UpdateScripts\\{version}.sql");
using (var cmd = new SqliteCommand(sqlScript, con))
{
cmd.ExecuteNonQuery();

}
}
catch (Exception e)
{
logHandler.LogMessage($"Database upgrade exception {e.Message}", client);
}
}
}
}
49 changes: 46 additions & 3 deletions StatBot/Database/DatabaseHandlers/UserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 17-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 05-06-2022
// ***********************************************************************
// <copyright file="UserHandler.cs">
// Copyright © 2022
Expand Down Expand Up @@ -72,9 +72,52 @@ public static void ExcludeFromStats(ulong userId, bool exclude)
}
}
/// <summary>
/// Overrides the username.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="username">The username.</param>
public static void OverrideUsername(ulong userId, string username)
{
string command = $"UPDATE Users SET OverrideName = '{username}' WHERE Id={userId}";
using (var connection = new SqliteConnection("Data Source=Database\\Statbot.db;"))
{
connection.Open();
using (var cmd = new SqliteCommand(command, connection))
{
cmd.ExecuteNonQuery();

}
}
}

/// <summary>
/// Adds the old username.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="username">The username.</param>
public static void AddOldUsername(ulong userId, string username)
{
var user = username.Split('#');
var oldUsers = GetOldUsers(userId);
if (!oldUsers.Any(c => c.UserName == user[0] && c.Discrim == user[1]))
{
string command = $"INSERT into OldUsers (Id, Username, Discrim, DateTimeChanged) " +
$"values ({userId},'{user[0]}','{user[1]}',DATETIME('now'))";
using (var connection = new SqliteConnection("Data Source=Database\\Statbot.db;"))
{
connection.Open();
using (var cmd = new SqliteCommand(command, connection))
{
cmd.ExecuteNonQuery();

}
}
}
}
/// <summary>
/// Gets the users.
/// </summary>
/// <param name="includingOld">if set to <c>true</c> [including old].</param>
/// <param name="includingOld">if set to <c>true</c> [including old users].</param>
/// <returns>List&lt;User&gt;.</returns>
public static List<User> GetUsers(bool includingOld = false)
{
Expand All @@ -94,7 +137,7 @@ public static List<User> GetUsers(bool includingOld = false)
{
oldUsers = GetOldUsers(id);
}
users.Add(new User(id, rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], oldUsers));
users.Add(new User(id, rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], oldUsers));

}
}
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Attachment.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Channel.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Database.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Embed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Embed.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/EmbedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="EmbedType.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Emoji.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Emoji.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/EmojiUsed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="EmojiUsed.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/MentionedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="MentionedUser.cs">
// Copyright © 2022
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Message.cs">
// Copyright © 2022
Expand Down
4 changes: 2 additions & 2 deletions StatBot/Database/Entities/OldUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 17-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="OldUser.cs">
// Copyright © 2022
Expand Down Expand Up @@ -38,7 +38,7 @@ public class OldUser
public string Discrim;

/// <summary>
/// Initializes a new instance of the <see cref="OldUser"/> class.
/// Initializes a new instance of the <see cref="OldUser" /> class.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="userName">Name of the user.</param>
Expand Down
2 changes: 1 addition & 1 deletion StatBot/Database/Entities/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 16-05-2022
//
// Last Modified By : Jeroen Heijster
// Last Modified On : 17-05-2022
// Last Modified On : 18-05-2022
// ***********************************************************************
// <copyright file="Server.cs">
// Copyright © 2022
Expand Down
Loading

0 comments on commit 0e60c48

Please sign in to comment.