\CayciV2Bot\CayciV2Bot\Core\FunctionsModule.cs
Return Back
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using CayciV2Bot.Models;
namespace CayciV2Bot.Core
{
public class FunctionsModule
{
public bool Log(IGuild guild, IMessageChannel channel, IUser user, int? musicId, string message, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string function = "")
{
try
{
var _dbContext = new Entities.Models();
var dbGuild = _dbContext.DiscordGuild.FirstOrDefault(g => g.DiscordId == guild.Id);
if (dbGuild == null)
{
dbGuild = new DiscordGuild
{
Name = guild.Name,
Description = guild.Description,
DiscordId = guild.Id,
OwnerDiscordId = guild.OwnerId,
CreationDate = guild.CreatedAt.UtcDateTime,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordGuild.Add(dbGuild);
_dbContext.SaveChanges();
}
var dbChannel = _dbContext.DiscordChannel.FirstOrDefault(c => c.DiscordId == channel.Id);
if (dbChannel == null)
{
dbChannel = new DiscordChannel
{
Name = channel.Name,
DiscordId = channel.Id,
Type = channel.GetChannelType().ToString(),
CreationDate = channel.CreatedAt.UtcDateTime,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordChannel.Add(dbChannel);
_dbContext.SaveChanges();
}
var dbUser = _dbContext.DiscordUser.FirstOrDefault(u => u.DiscordId == user.Id);
if (dbUser == null)
{
dbUser = new DiscordUser
{
Username = user.Username,
Discriminator = user.Discriminator,
DiscordId = user.Id,
JoinDate = user.CreatedAt.UtcDateTime,
CreationDate = DateTime.Now,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordUser.Add(dbUser);
_dbContext.SaveChanges();
}
var operation = Path.GetFileNameWithoutExtension(callerFilePath) + "." + function;
var log = new Log
{
UserId = dbUser.Id,
GuildId = dbGuild.Id,
ChannelId = dbChannel.Id,
MusicId = musicId,
Operation = operation,
Message = message,
TimeStamp = DateTime.Now
};
_dbContext.Log.Add(log);
_dbContext.SaveChanges();
return true;
}
catch (Exception ex)
{
LogPrinter(ex.Message);
return false;
}
}
public void LogPrinter(object message, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string function = "")
{
Debug.WriteLine("--**-- " + Path.GetFileNameWithoutExtension(callerFilePath) + "." + function);
Debug.WriteLine(message?.ToString());
Debug.WriteLine("--**-- " + Path.GetFileNameWithoutExtension(callerFilePath) + "." + function);
}
public string RemoveSpecialCharacters(string str)
{
char[] turkishChars = { 'ı', 'ğ', 'İ', 'Ğ', 'ç', 'Ç', 'ş', 'Ş', 'ö', 'Ö', 'ü', 'Ü' };
char[] englishChars = { 'i', 'g', 'I', 'G', 'c', 'C', 's', 'S', 'o', 'O', 'u', 'U' };
for (int counter = 0; counter < turkishChars.Length; counter++)
{
str = str.Replace(turkishChars[counter], englishChars[counter]);
}
const int maxLength = 64;
str = str.Length <= maxLength ? str : str[..maxLength];
return Regex.Replace(str, "[^a-zA-Z0-9 ]+", "", RegexOptions.Compiled);
}
public async Task<bool> SaveFile(string inputFile, byte[] data)
{
try
{
if (!File.Exists(inputFile))
{
await File.WriteAllBytesAsync(inputFile, data);
}
return true;
}
catch (Exception ex)
{
LogPrinter(ex.Message);
return false;
}
}
}
}
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using CayciV2Bot.Models;
namespace CayciV2Bot.Core
{
public class FunctionsModule
{
public bool Log(IGuild guild, IMessageChannel channel, IUser user, int? musicId, string message, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string function = "")
{
try
{
var _dbContext = new Entities.Models();
var dbGuild = _dbContext.DiscordGuild.FirstOrDefault(g => g.DiscordId == guild.Id);
if (dbGuild == null)
{
dbGuild = new DiscordGuild
{
Name = guild.Name,
Description = guild.Description,
DiscordId = guild.Id,
OwnerDiscordId = guild.OwnerId,
CreationDate = guild.CreatedAt.UtcDateTime,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordGuild.Add(dbGuild);
_dbContext.SaveChanges();
}
var dbChannel = _dbContext.DiscordChannel.FirstOrDefault(c => c.DiscordId == channel.Id);
if (dbChannel == null)
{
dbChannel = new DiscordChannel
{
Name = channel.Name,
DiscordId = channel.Id,
Type = channel.GetChannelType().ToString(),
CreationDate = channel.CreatedAt.UtcDateTime,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordChannel.Add(dbChannel);
_dbContext.SaveChanges();
}
var dbUser = _dbContext.DiscordUser.FirstOrDefault(u => u.DiscordId == user.Id);
if (dbUser == null)
{
dbUser = new DiscordUser
{
Username = user.Username,
Discriminator = user.Discriminator,
DiscordId = user.Id,
JoinDate = user.CreatedAt.UtcDateTime,
CreationDate = DateTime.Now,
UniqueId = Guid.NewGuid()
};
_dbContext.DiscordUser.Add(dbUser);
_dbContext.SaveChanges();
}
var operation = Path.GetFileNameWithoutExtension(callerFilePath) + "." + function;
var log = new Log
{
UserId = dbUser.Id,
GuildId = dbGuild.Id,
ChannelId = dbChannel.Id,
MusicId = musicId,
Operation = operation,
Message = message,
TimeStamp = DateTime.Now
};
_dbContext.Log.Add(log);
_dbContext.SaveChanges();
return true;
}
catch (Exception ex)
{
LogPrinter(ex.Message);
return false;
}
}
public void LogPrinter(object message, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string function = "")
{
Debug.WriteLine("--**-- " + Path.GetFileNameWithoutExtension(callerFilePath) + "." + function);
Debug.WriteLine(message?.ToString());
Debug.WriteLine("--**-- " + Path.GetFileNameWithoutExtension(callerFilePath) + "." + function);
}
public string RemoveSpecialCharacters(string str)
{
char[] turkishChars = { 'ı', 'ğ', 'İ', 'Ğ', 'ç', 'Ç', 'ş', 'Ş', 'ö', 'Ö', 'ü', 'Ü' };
char[] englishChars = { 'i', 'g', 'I', 'G', 'c', 'C', 's', 'S', 'o', 'O', 'u', 'U' };
for (int counter = 0; counter < turkishChars.Length; counter++)
{
str = str.Replace(turkishChars[counter], englishChars[counter]);
}
const int maxLength = 64;
str = str.Length <= maxLength ? str : str[..maxLength];
return Regex.Replace(str, "[^a-zA-Z0-9 ]+", "", RegexOptions.Compiled);
}
public async Task<bool> SaveFile(string inputFile, byte[] data)
{
try
{
if (!File.Exists(inputFile))
{
await File.WriteAllBytesAsync(inputFile, data);
}
return true;
}
catch (Exception ex)
{
LogPrinter(ex.Message);
return false;
}
}
}
}