IM GONNA COOOOOD
AAAAAAAAAAAA I'M COODING AAAAAAAAAAAAAAAAAAAAAAAAAAAA
This commit is contained in:
183
Kehyeedra3/Services/DatabaseService.cs
Normal file
183
Kehyeedra3/Services/DatabaseService.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MySql.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Kehyeedra3.Services.Models;
|
||||
using Discord;
|
||||
using System.Timers;
|
||||
|
||||
namespace Kehyeedra3.Services
|
||||
{
|
||||
public class DatabaseService
|
||||
{/*
|
||||
private readonly string ConnectionString;
|
||||
private MySqlConnection Connection;
|
||||
public DatabaseService(string host, ushort port, string user, string password, string database)
|
||||
{
|
||||
ConnectionString = $"Server={host};Port={port};Database={database};Uid={user};Pwd={password}";
|
||||
}
|
||||
private async Task ConnectOrCreateAsync()
|
||||
{
|
||||
if (Connection == null)
|
||||
{
|
||||
Connection = new MySqlConnection(ConnectionString);
|
||||
}
|
||||
if (Connection.State == System.Data.ConnectionState.Open)
|
||||
return;
|
||||
else
|
||||
{
|
||||
await Connection.OpenAsync();
|
||||
return;
|
||||
}
|
||||
}
|
||||
public async Task<bool> CreateUserAsync(IUser user)
|
||||
{
|
||||
await ConnectOrCreateAsync();
|
||||
|
||||
MySqlCommand command = new MySqlCommand("INSERT INTO `users` (UserID, Avatar, UName) VALUES (@uid, @avatar, @uname);"); //userinfo
|
||||
|
||||
command.Parameters.AddWithValue("@uid", user.Id);
|
||||
command.Parameters.AddWithValue("@avatar", user.GetAvatarUrl());
|
||||
command.Parameters.AddWithValue("@uname", user.Username);
|
||||
|
||||
command.Connection = Connection;
|
||||
try
|
||||
{
|
||||
await command.ExecuteScalarAsync();
|
||||
command.Dispose();
|
||||
return true;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
command.Dispose();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> DoesUserExistAsync(IUser user)
|
||||
{
|
||||
await ConnectOrCreateAsync();
|
||||
|
||||
var command = new MySqlCommand("SELECT 1 FROM `users` WHERE UserID = @userId;");
|
||||
|
||||
command.Connection = Connection;
|
||||
|
||||
command.Parameters.AddWithValue("@userId", user.Id);
|
||||
|
||||
return Convert.ToBoolean(await command.ExecuteScalarAsync());
|
||||
}
|
||||
public async Task<DatabaseUser> GetUserAsync(ulong userID)
|
||||
{
|
||||
var command = new MySqlCommand("SELECT * FROM `users` WHERE UserID = @userId;");
|
||||
|
||||
command.Connection = Connection;
|
||||
command.Parameters.AddWithValue("@userId", userID);
|
||||
|
||||
var result = await command.ExecuteReaderAsync();
|
||||
if (result.HasRows)
|
||||
{
|
||||
while(await result.ReadAsync())
|
||||
{
|
||||
return new DatabaseUser //userinfo
|
||||
{
|
||||
UserID = ulong.Parse(result["UserId"].ToString()),
|
||||
Avatar = result["Avatar"].ToString(),
|
||||
UName = result["UName"].ToString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public async Task<bool> CreateReminderAsync(ulong rUserID, string rMessage, ulong rSend)
|
||||
{
|
||||
await ConnectOrCreateAsync();
|
||||
|
||||
MySqlCommand command = new MySqlCommand("INSERT INTO `reminders` (UserID, RMessage, RSend) VALUES (@uid, @rmsg, @rsend);"); //reminderinfo
|
||||
|
||||
command.Parameters.AddWithValue("@uid", rUserID);
|
||||
command.Parameters.AddWithValue("@rmsg", rMessage);
|
||||
command.Parameters.AddWithValue("@rsend", rSend);
|
||||
command.Connection = Connection;
|
||||
try
|
||||
{
|
||||
await command.ExecuteScalarAsync();
|
||||
command.Dispose();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
command.Dispose();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public async Task<bool> DoesReminderExistAsync(IUser user)
|
||||
{
|
||||
await ConnectOrCreateAsync();
|
||||
|
||||
var command = new MySqlCommand("SELECT 1 FROM `reminders` WHERE UserID = @userId;");
|
||||
|
||||
command.Connection = Connection;
|
||||
|
||||
command.Parameters.AddWithValue("@userId", user.Id);
|
||||
|
||||
return Convert.ToBoolean(await command.ExecuteScalarAsync());
|
||||
}
|
||||
public async Task<DatabaseReminder> GetReminderAsync(ulong userID)
|
||||
{
|
||||
var command = new MySqlCommand("SELECT * FROM `reminders` WHERE UserID = @userId;");
|
||||
|
||||
command.Connection = Connection;
|
||||
command.Parameters.AddWithValue("@userId", userID);
|
||||
|
||||
var result = await command.ExecuteReaderAsync();
|
||||
if (result.HasRows)
|
||||
{
|
||||
while (await result.ReadAsync())
|
||||
{
|
||||
return new DatabaseReminder //reminderinfo
|
||||
{
|
||||
rUserID = ulong.Parse(result["rUserID"].ToString()),
|
||||
rMessage = result["rMessage"].ToString(),
|
||||
rSend = ulong.Parse(result["rSend"].ToString())
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public async Task<List<DatabaseReminder>> GetAllReminderAsync()
|
||||
{
|
||||
var command = new MySqlCommand("SELECT * FROM `reminders`;");
|
||||
|
||||
command.Connection = Connection;
|
||||
|
||||
var Reminders = new List<DatabaseReminder>();
|
||||
var result = await command.ExecuteReaderAsync();
|
||||
if (result.HasRows)
|
||||
{
|
||||
while (await result.ReadAsync())
|
||||
{
|
||||
Reminders.Add (new DatabaseReminder //reminderinfo
|
||||
{
|
||||
rUserID = ulong.Parse(result["rUserID"].ToString()),
|
||||
rMessage = result["rMessage"].ToString(),
|
||||
rSend = ulong.Parse(result["rSend"].ToString())
|
||||
});
|
||||
}
|
||||
return Reminders;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
11
Kehyeedra3/Services/Models/Reminder.cs
Normal file
11
Kehyeedra3/Services/Models/Reminder.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Kehyeedra3.Services.Models
|
||||
{
|
||||
public class Reminder
|
||||
{
|
||||
public ulong Id { get; set; }
|
||||
public ulong Created { get; set; } = 0;
|
||||
public ulong Send { get; set; } = 0;
|
||||
public ulong UserId { get; set; } = 0;
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
23
Kehyeedra3/Services/Models/User.cs
Normal file
23
Kehyeedra3/Services/Models/User.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace Kehyeedra3.Services.Models
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public ulong Id { get; set; } = 0;
|
||||
public string Avatar { get; set; } = null;
|
||||
public string Username { get; set; } = null;
|
||||
public long Money { get; set; } = 0;
|
||||
public ulong LastMine { get; set; } = 0;
|
||||
|
||||
public bool GrantMoney(User bank, long amount)
|
||||
{
|
||||
if(bank.Money > amount)
|
||||
{
|
||||
Money += amount % bank.Money;
|
||||
bank.Money -= amount;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Kehyeedra3/Services/ReminderService.cs
Normal file
42
Kehyeedra3/Services/ReminderService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kehyeedra3.Services.Models;
|
||||
|
||||
namespace Kehyeedra3.Services
|
||||
{
|
||||
public class ReminderService
|
||||
{
|
||||
private static async Task SendReminderAsync(Reminder reminder)
|
||||
{
|
||||
var dmchannel = await Bot._bot.GetUser(reminder.UserId).GetOrCreateDMChannelAsync();
|
||||
await dmchannel.SendMessageAsync(reminder.Message);
|
||||
}
|
||||
public async Task Tick()
|
||||
{
|
||||
using (var Database = new ApplicationDbContextFactory().CreateDbContext())
|
||||
{
|
||||
List<Reminder> toRemove = new List<Reminder>();
|
||||
while (true)
|
||||
{
|
||||
if(Database.Reminders.Any() && Bot._bot != null && Bot._bot.Shards.All(x=>x.ConnectionState == Discord.ConnectionState.Connected))
|
||||
{
|
||||
foreach(var x in Database.Reminders)
|
||||
{
|
||||
if (x.Send <= DateTime.UtcNow.ToYeedraStamp())
|
||||
{
|
||||
await SendReminderAsync(x).ConfigureAwait(false);
|
||||
toRemove.Add(x);
|
||||
}
|
||||
}
|
||||
Database.Reminders.RemoveRange(toRemove);
|
||||
await Database.SaveChangesAsync().ConfigureAwait(false);
|
||||
toRemove.Clear();
|
||||
}
|
||||
await Task.Delay(250);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user