예제 #1
0
        /// <summary>
        /// Loads an existing table. Will throw exception if table does not exist.
        /// </summary>
        /// <param name="bChan"></param>
        /// <param name="plugin"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private async Task <dynamic> Load <T>(BotChannel bChan, string plugin)
        {
            // Get the data stored or save a blank set
            using (SQLiteCommand cmd = new SQLiteCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = Core.Data;
                cmd.CommandText = $"SELECT * FROM \"{TableName(bChan.Key,plugin)}\" WHERE configKey IS @key";
                cmd.Parameters.AddWithValue("@key", bChan.Key);

                var i = await cmd.ExecuteScalarAsync();

                SQLiteDataReader result;
                try
                {
                    result = cmd.ExecuteReader();
                }
                catch (Exception)
                {
                    await Core.LOG(new LogEntry(LOGSEVERITY.ERROR, PLUGINNAME, $"Database query failed hard. ({cmd.CommandText})"));

                    throw;
                }

                result.Read();
                // Deserialize the data
                //string debugfield = result.GetString(1);
                T result3 = JsonConvert.DeserializeObject <T>(result.GetString(1));
                return(result3);
            }
        }
예제 #2
0
        /// <summary>
        /// Returns the saved config or creates a new entry with the object passed then save it to db and return it.
        /// If no table is found it will serielize a fresh instance of the type and save it as a sting into DB.
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="plugin"></param>
        /// <param name="objectType"></param>
        /// <param name="twitch"></param>
        /// <returns></returns>
        public async Task <dynamic> GetConfig <T>(BotChannel bChan, string plugin)
        {
            if (!TableExists(TableName(bChan.Key, plugin), Core.Data))
            {
                // No table so we make one
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection  = Core.Data;
                    cmd.CommandText = $"CREATE TABLE \"{TableName(bChan.Key, plugin)}\" (configKey varchar(30), config TEXT(2048))";
                    cmd.ExecuteNonQuery();
                }
            }
            if (await RowExists(TableName(bChan.Key, plugin), bChan.Key) == false)
            {
                // Create a new instance of the type we need
                var y = (T)System.Activator.CreateInstance(typeof(T));
                // No entry for this config so make one
                using (SQLiteCommand c = new SQLiteCommand())
                {
                    c.CommandType = CommandType.Text;
                    c.Connection  = Core.Data;
                    c.CommandText = $"INSERT INTO \"{TableName(bChan.Key, plugin)}\" VALUES (@key, @info)";
                    c.Parameters.AddWithValue("@key", bChan.Key);
                    c.Parameters.AddWithValue("@info", JsonConvert.SerializeObject(y));
                    c.ExecuteNonQuery();
                    await Core.LOG(new LogEntry(LOGSEVERITY.WARNING, plugin, $"Created config entry ({plugin}::{bChan.Key}) in DB."));
                }
            }
            // Now we should always have a valid entry in the DB so we read and return it
            var qwe = await Load <T>(bChan, plugin);

            return(qwe);
        }
예제 #3
0
 /// <summary>
 /// This base method should be overwritten by custom table creation
 /// </summary>
 /// <param name="botChannel"></param>
 /// <param name="plugin"></param>
 public void TableCreate(BotChannel bChan, string plugin)
 {
     using (SQLiteCommand cmd = new SQLiteCommand())
     {
         cmd.CommandType = CommandType.Text;
         cmd.Connection  = Core.Data;
         cmd.CommandText = $"CREATE TABLE \"{TableName(bChan.Key, plugin)}\" (configKey varchar(30), config TEXT(2048))";
         cmd.ExecuteNonQuery();
     }
 }
예제 #4
0
 /// <summary>
 /// This ONLY serializes and store an object as a string
 /// </summary>
 /// <param name="bChan"></param>
 /// <param name="plugin"></param>
 /// <param name="settings"></param>
 public void SaveBaseSettings(BotChannel bChan, string plugin, object settings)
 {
     using (SQLiteCommand cmd = new SQLiteCommand())
     {
         cmd.CommandType = CommandType.Text;
         cmd.Connection  = Core.Data;
         cmd.CommandText = $"UPDATE \"{TableName(bChan.Key, plugin)}\" SET config = @data WHERE configKey is @key";
         cmd.Parameters.AddWithValue("@data", JsonConvert.SerializeObject(settings));
         cmd.Parameters.AddWithValue("@key", bChan.Key);
         cmd.ExecuteNonQuery();
     }
 }
예제 #5
0
 public async void RowBaseCreate(BotChannel bChan, String plugin, String key, string info)
 {
     using (SQLiteCommand c = new SQLiteCommand())
     {
         c.CommandType = CommandType.Text;
         c.Connection  = Core.Data;
         c.CommandText = $"INSERT INTO \"{TableName(bChan.Key, plugin)}\" VALUES (@key, @info)";
         c.Parameters.AddWithValue("@key", key);
         c.Parameters.AddWithValue("@info", info);
         c.ExecuteNonQuery();
         await Core.LOG(new LogEntry(LOGSEVERITY.WARNING, plugin, $"Created config entry ({plugin}::{key}) in DB."));
     }
 }
예제 #6
0
        /// <summary>
        /// Takes the passed Object, serialize the object and store it as a string under the plugin's DB Table using the Botchannel Key as identifier.
        /// </summary>
        /// <param name="bChan"></param>
        /// <param name="plugin"></param>
        /// <param name="data"></param>
        public async void UpdateConfig <T>(BotChannel bChan, string plugin, Object data)
        {
            SQLiteCommand cmd = new SQLiteCommand
            {
                CommandType = CommandType.Text,
                Connection  = Core.Data,
                CommandText = $"UPDATE \"{TableName(bChan.Key,plugin)}\" SET config = @data WHERE configKey is @key"
            };

            cmd.Parameters.AddWithValue("@data", JsonConvert.SerializeObject(data));
            cmd.Parameters.AddWithValue("@key", bChan.Key);
            cmd.ExecuteNonQuery();
            await Core.LOG(new LogEntry(LOGSEVERITY.WARNING, PLUGINNAME, $"Saved updated config for ({plugin}::{bChan.Key}) in DB."));
        }
예제 #7
0
        /// <summary>
        /// Run this to make sure a config table exists in the database
        /// </summary>
        /// <param name="bChan"></param>
        /// <param name="plugin"></param>
        /// <param name="obj"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task ConfigSetup <T>(BotChannel bChan, string plugin, T obj)
        {
            string tableName = TableName(bChan.Key, plugin);

            // Check if missing the table If so then make it
            if (!TableExists(tableName, Core.Data))
            {
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection  = Core.Data;
                    cmd.CommandText = $"CREATE TABLE \"{tableName}\" (configKey varchar(30), config TEXT(2048))";
                    cmd.ExecuteNonQuery();
                    await Core.LOG(new LogEntry(LOGSEVERITY.INFO, PLUGINNAME, $"Table {tableName} created!!"));
                }
            }
            // Get the data stored or save a blank set
            using (SQLiteCommand cmd = new SQLiteCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = Core.Data;
                cmd.CommandText = $"SELECT * FROM \"{tableName}\" WHERE configKey IS @key";
                cmd.Parameters.AddWithValue("@key", bChan.Key);
                // Serialize the data to store if we dont find any entry
                string info = JsonConvert.SerializeObject(obj, Formatting.None);
                // Check if we already have an entry
                var i = await cmd.ExecuteScalarAsync();

                // If no entry add what we got
                if (i == null)
                {
                    using (SQLiteCommand c = new SQLiteCommand())
                    {
                        c.CommandType = CommandType.Text;
                        c.Connection  = Core.Data;
                        c.CommandText = $"INSERT INTO \"{tableName}\" VALUES (@key, @info)";
                        c.Parameters.AddWithValue("@key", bChan.Key);
                        c.Parameters.AddWithValue("@info", info);
                        c.ExecuteNonQuery();
                        await Core.LOG(new LogEntry(LOGSEVERITY.INFO, PLUGINNAME, $"Created config entry ({plugin}::{bChan.Key}) in DB."));
                    }
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Returns a string from the database that then should be deserialized to the right type.
        /// </summary>
        /// <param name="plugin"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <string> RowBaseRead(BotChannel bChan, string plugin, string key)
        {
            using (SQLiteCommand cmd = new SQLiteCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = Core.Data;
                cmd.CommandText = $"SELECT * FROM \"{TableName(bChan.Key, plugin)}\" WHERE configKey IS @key";
                cmd.Parameters.AddWithValue("@key", key);
                SQLiteDataReader result;
                try
                {
                    result = cmd.ExecuteReader();
                }
                catch (Exception)
                {
                    await Core.LOG(new LogEntry(LOGSEVERITY.WARNING, plugin, $"Database query failed hard. ({cmd.CommandText})"));

                    throw;
                }
                result.Read();
                return(result.GetString(1));
            }
        }
예제 #9
0
 public override void OnBotChannelEntryMergeEvent(MisfitBot_MKII.BotChannel discordGuild, MisfitBot_MKII.BotChannel twitchChannel)
 {
 }
예제 #10
0
 public UnBanEventArguments(BotChannel chan, UserEntry mod, UserEntry vic, int time, bool isTwitch)
 {
     bChan = chan; enforcingUser = mod; bannedUser = vic; timestamp = time; isDiscord = !isTwitch;
 }
예제 #11
0
 public BanEventArguments(BotChannel chan, UserEntry mod, UserEntry vic, int time, int dur, string cause, bool isTwitch)
 {
     bChan = chan; enforcingUser = mod; bannedUser = vic; timestamp = time; duration = dur; reason = cause; isDiscord = !isTwitch;
 }
예제 #12
0
 public BitEventArguments(BotChannel chan, UserEntry usr, int time, int bits, int total, string con, string chatmsg)
 {
     bChan = chan; user = usr; timestamp = time; bitsGiven = bits; bitsTotal = total; context = con; chatmessage = chatmsg;
 }