Select Git revision
Program.cs 17.12 KiB
using System.Web;
using Discord;
using Discord.WebSocket;
using Lightquark.Types;
using Lightquark.Types.EventBus;
using Lightquark.Types.EventBus.Events;
using Lightquark.Types.EventBus.Messages;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
using Quarkcord.Objects;
namespace Quarkcord;
public class QuarkcordPlugin : IPlugin
{
private static Task Log(LogMessage msg)
{
Console.WriteLine($"[Quarkcord] {msg.ToString()}");
return Task.CompletedTask;
}
private string? _token;
private string? _botUserId;
private Lightquark.Types.Mongo.IUser? _user;
private DiscordSocketClient? _client;
private readonly ManualResetEvent _eventBusGetEvent = new(false);
private IEventBus _eventBus = null!;
private NetworkInformation? _networkInformation;
private MongoClient _mongoClient = null!;
private IMongoDatabase _database = null!;
private IMongoCollection<MessagePair> MessagePairs => _database.GetCollection<MessagePair>("messages");
private IMongoCollection<ChannelPair> ChannelPairs => _database.GetCollection<ChannelPair>("channels");
private List<ChannelPair> _bridgeChannels = null!;
public void Initialize(IEventBus eventBus)
{
Task.Run(async () =>
{
eventBus.Subscribe<BusReadyEvent>(_ => _eventBusGetEvent.Set());
_eventBusGetEvent.WaitOne();
_eventBus = eventBus;
var filePath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "lightquark", "quarkcord");
string mongoConnectionString;
string mongoDb;
if (File.Exists(filePath))
{
var text = (await File.ReadAllTextAsync(filePath)).Trim().Split(";");
_token = text[0];
_botUserId = text[1];
mongoConnectionString = text[2];
mongoDb = text[3];
}
else
{
await File.WriteAllTextAsync(filePath, "INSERT_DISCORD_TOKEN_HERE;INSERT_LQ_BOT_USER_ID_HERE;INSERT_MONGO_CONNECTION_STRING_HERE;INSERT_MONGO_DB_HERE");
_token = "INSERT_DISCORD_TOKEN_HERE";
_botUserId = "INSERT_LQ_BOT_USER_ID_HERE";
mongoConnectionString = "INSERT_MONGO_CONNECTION_STRING_HERE";
mongoDb = "INSERT_MONGO_DB_HERE";
}
if (_token == "INSERT_DISCORD_TOKEN_HERE") throw new Exception($"Please add discord token to {filePath}");
if (_botUserId == "INSERT_LQ_BOT_USER_ID_HERE") throw new Exception($"Please add bot user id to {filePath}");
if (mongoConnectionString == "INSERT_MONGO_CONNECTION_STRING_HERE") throw new Exception($"Please add mongo connection string to {filePath}");
if (mongoDb == "INSERT_MONGO_DB_HERE") throw new Exception($"Please add mongo db to {filePath}");
_mongoClient = new MongoClient(mongoConnectionString);
_database = _mongoClient.GetDatabase(mongoDb);
var channelPairCursor = await ChannelPairs.FindAsync(new BsonDocument());