Skip to content

Instantly share code, notes, and snippets.

@hburn7
Last active March 29, 2019 00:24
Show Gist options
  • Save hburn7/56be28df754555400fed347d290f3653 to your computer and use it in GitHub Desktop.
Save hburn7/56be28df754555400fed347d290f3653 to your computer and use it in GitHub Desktop.
ServerMessageLogs.cs : ServerMessageLog.cs :: UserAccounts.cs : UserAccount.cs
[
{
"Username": "Stage#0001",
"ID": 146092837723832320,
"IsInServers": [
"Stage's Sanctuary",
"Kaguya Support",
"Spring Tranquility osu! Tournament 2019"
],
"IsInServerIDs": [
407648193392803851,
546880579057221644,
461347676148072448
],
"Points": 2720,
"EXP": 669,
"Rep": 0,
"LastReceivedEXP": "2019-03-28T18:22:42.6597016-04:00",
"LastReceivedTimelyPoints": "2019-03-26T20:59:37.8414544-04:00",
"LastGivenRep": "0001-01-01T00:00:00",
"LevelNumber": 5,
"OsuUsername": null,
"Blacklisted": 0,
"TotalDailyGambleWinnings": 0,
"LifetimeGambleWins": 0.0,
"LifetimeGambleLosses": 0.0,
"LifetimeGambles": 0.0,
"LifetimeEliteRolls": 0
},...]
public async Task InitializeAsync(DiscordSocketClient client)
{
_client = client;
_service = new CommandService();
_service.AddTypeReader(typeof(List<SocketGuildUser>), new ListSocketGuildUserTR());
await _service.AddModulesAsync(
Assembly.GetExecutingAssembly(),
_services);
_client.MessageReceived += HandleCommandAsync;
_client.MessageReceived += MessageCache;
_client.MessageDeleted += LoggingDeletedMessage;
}
...
private async Task MessageCache(SocketMessage s) //Work on this in the morning xd
{
var msg = s as SocketUserMessage;
if (msg == null) return;
Console.WriteLine("Pass 1");
SocketCommandContext context = new SocketCommandContext(_client, msg);
Console.WriteLine("Pass 2");
var currentLog = ServerMessageLogs.GetLog(context.Guild.Id);
Console.WriteLine("Pass 3");
currentLog.AddMessage(msg.Content); //Stops here
Console.WriteLine("Pass 4");
ServerMessageLogs.SaveServerLogging();
Console.WriteLine("Pass 5");
await msg.Channel.SendMessageAsync("Test complete");
Console.WriteLine("Pass 6");
}
...{
...{
public static void SaveUserAccounts(IEnumerable<UserAccount> accounts, string filePath) //Saves user accounts. Required after an edit (adding points for example)
{
string json = JsonConvert.SerializeObject(accounts, Formatting.Indented);
File.WriteAllText(filePath, json);
}
...
public static void SaveServerLogging(IEnumerable<ServerMessageLog> logs, string filePath)
{
string json = JsonConvert.SerializeObject(logs, Formatting.Indented);
File.WriteAllText(filePath, json);
}
...
//Get all userAccounts
public static IEnumerable<UserAccount> LoadUserAccounts(string filePath) // Add exception handling
{
if (!File.Exists(filePath)) return null;
string json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<List<UserAccount>>(json);
}
...
public static IEnumerable<ServerMessageLog> LoadServerMessageLogs(string filepath)
{
if (!File.Exists(filepath)) return null;
string json = File.ReadAllText(filepath);
return JsonConvert.DeserializeObject<List<ServerMessageLog>>(json);
}
...}
...}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Discord_Bot.Core.Server_Files
{
public class ServerMessageLog
{
public ulong ID { get; set; }
public string ServerName { get; set; }
public List<string> LastThousandMessages { get; set; }
public ServerMessageLog(ulong id)
{
ID = id;
ServerName = "";
LastThousandMessages = new List<string>();
}
public void AddMessage(string content)
{
LastThousandMessages.Add(content);
}
}
}
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Discord_Bot.Core.Server_Files
{
public static class ServerMessageLogs
{
private static List<ServerMessageLog> serverMessageLogs;
private static string logFile = "Resources/servermessagelogs.json";
static ServerMessageLogs()
{
if(DataStorage2.SaveExists(logFile))
{
serverMessageLogs = DataStorage2.LoadServerMessageLogs(logFile).ToList();
}
else
{
serverMessageLogs = new List<ServerMessageLog>();
SaveServerLogging();
}
}
public static ServerMessageLog GetLog(ulong id)
{
return GetOrCreateLog(id);
}
private static ServerMessageLog GetOrCreateLog(ulong id)
{
var result = from a in serverMessageLogs
where a.ID == id
select a;
var serverMessageLog = result.FirstOrDefault();
if (serverMessageLogs == null) serverMessageLog = CreateLog(id);
return serverMessageLog;
}
private static ServerMessageLog CreateLog(ulong id)
{
var newLog = new ServerMessageLog(id);
serverMessageLogs.Add(newLog);
SaveServerLogging();
return newLog;
}
public static void SaveServerLogging()
{
DataStorage2.SaveServerLogging(serverMessageLogs, logFile);
}
}
}
{...{...
public double LifetimeGambleLosses { get; set; }
public double LifetimeGambles { get; set; }
public int LifetimeEliteRolls { get; set; }
public UserAccount(ulong id)
{
ID = id;
IsInServers = new List<string>();
IsInServerIDs = new List<ulong>();
Points = 0;
EXP = 0;
Rep = 0;
Blacklisted = 0;
LifetimeGambleWins = 0;
LifetimeGambleLosses = 0;
LifetimeEliteRolls = 0;
}
public void AddSName(string server)
{
IsInServers.Add(server);
}
...}...}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.WebSocket;
namespace Discord_Bot.Core.UserAccounts
{
public static class UserAccounts
{
private static List<UserAccount> accounts;
private static string accountsFile = "Resources/accounts.json";
static UserAccounts()
{
if(DataStorage2.SaveExists(accountsFile))
{
accounts = DataStorage2.LoadUserAccounts(accountsFile).ToList();
}
else
{
accounts = new List<UserAccount>();
SaveAccounts();
}
}
public static List<UserAccount> GetAllAccounts()
{
return accounts;
}
public static void SaveAccounts()
{
DataStorage2.SaveUserAccounts(accounts, accountsFile);
}
public static UserAccount GetAccount(SocketUser user)
{
return GetOrCreateAccount(user.Id);
}
public static UserAccount GetAuthor()
{
return GetOrCreateAccount(146092837723832320);
}
private static UserAccount GetOrCreateAccount(ulong id)
{
var result = from a in accounts
where a.ID == id
select a;
var account = result.FirstOrDefault();
if(account == null) account = CreateUserAccount(id);
return account;
}
private static UserAccount CreateUserAccount(ulong id)
{
var newAccount = new UserAccount(id);
accounts.Add(newAccount);
SaveAccounts();
return newAccount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment