Skip to content

Commit

Permalink
Everything should be implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
MJHeijster committed Jun 4, 2022
1 parent 501673b commit a73514b
Show file tree
Hide file tree
Showing 36 changed files with 489 additions and 90 deletions.
50 changes: 50 additions & 0 deletions SharedCode/Settings/BotSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ public Application(IConfiguration configuration)
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");
}
/// <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>
/// Class Commands.
/// </summary>
Expand All @@ -141,6 +185,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 @@ -163,6 +208,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
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
65 changes: 63 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,60 @@ 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.Read();
version = (long)reader[0];
}
}
}
for (long i = version; i < databaseVersion; ++i)
UpdateVersion(i, 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.sql");
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);
}
}
}
}
44 changes: 42 additions & 2 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 : 28-05-2022
// ***********************************************************************
// <copyright file="UserHandler.cs">
// Copyright © 2022
Expand Down Expand Up @@ -72,6 +72,46 @@ 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('#');
string command = $"INSERT INTO OldUsers Id = {userId}," +
$"Username = {user[0]}," +
$"Discrim = {user[1]}," +
$"DateTime = {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>
Expand All @@ -94,7 +134,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

0 comments on commit a73514b

Please sign in to comment.