-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.cs
66 lines (56 loc) · 2.47 KB
/
DB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using StackExchange.Redis;
using StackExchange.Redis.Extensions.System.Text.Json;
using StackExchange.Redis.Extensions.Core;
using System.Text.Json;
using DSharpPlus.Entities;
using Ardalis.Result;
using Cerberus.VRChat;
namespace Cerberus.Database {
public class DatabaseMiddleware {
private ConnectionMultiplexer _connection;
private IDatabaseAsync _db;
public static async Task<DatabaseMiddleware> ConnectAsync(string config) {
ConnectionMultiplexer connection = await ConnectionMultiplexer.ConnectAsync(config);
DatabaseMiddleware db = new DatabaseMiddleware {
_connection = connection,
_db = connection.GetDatabase()
};
return db;
}
public bool Connected() => _connection.IsConnected;
public async Task<bool> RegisterReactionListenerAsync(Reactionlistener listener) {
return await _db.StringSetAsync(listener.MessageId + "-" + listener.Emoji.Name, listener.RoleId.ToString());
}
public async Task<Reactionlistener> FetchReactionListenerAsync(ulong messageId, DiscordEmoji emoji) {
RedisValue res = await _db.StringGetAsync(messageId + "-" + emoji.Name);
if (res.IsNullOrEmpty) {
throw new DatabseException("DB Query returned null");
}
return new Reactionlistener {
MessageId = messageId,
Emoji = emoji,
RoleId = (ulong) res
};
}
public async Task<bool> InsertVrchatPairAsync(DiscordMember member, VRChatUser vrchatUser) {
return await _db.StringSetAsync(member.Id.ToString(), vrchatUser.id);
}
public async Task<Result<string>> FetchVrchatUserAsync(DiscordMember member) {
RedisValue res = await _db.StringGetAsync(member.Id.ToString());
if (res.IsNullOrEmpty) {
return Result<string>.NotFound();
}
return (string) res;
}
}
[System.Serializable]
public class DatabseException : System.Exception
{
public DatabseException() { }
public DatabseException(string message) : base(message) { }
public DatabseException(string message, System.Exception inner) : base(message, inner) { }
protected DatabseException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}