Example #1
0
 private Quote quoteFromReader(SQLiteDataReader reader)
 {
     Quote quote = new Quote();
     quote.id = (long)reader["id"];
     quote.channel = (string)reader["channel"];
     quote.setTimestamp((int)reader["timestamp"]);
     quote.user = (string)reader["user"];
     quote.text = (string)reader["text"];
     return quote;
 }
Example #2
0
        public BotMessage GetResponse(ResponseContext context)
        {
            string request = context.Message.Text.Length > quoteStart.Length ? context.Message.Text.Substring(quoteStart.Length+1) : "";
            Debug.WriteLine("request: '" + request + "'");
            BotMessage reply = new BotMessage() { ChatHub = context.Message.ChatHub };

            string command = request.Contains(" ") ? request.Substring(0, request.IndexOf(" ")) : request;
            Debug.WriteLine("command: '" + command + "'");

            switch (command)
            {
                case quoteAdd:
                    {
                        string quoteText = request.Substring(request.IndexOf(" ") + 1);

                        if (quoteText.Length > 0)
                        {
                            Quote quote = new Quote
                            {
                                channel = context.Message.ChatHub.Name,
                                user = context.UserNameCache[context.Message.User.ID],
                                timestamp = context.Message.TimeStamp,
                                text = quoteText
                            };

                            if (addQuoteToDb(ref quote))
                            {
                                reply.Text = "Success! Added " + quote.ToString(quoteTimeZone);
                            }
                            else
                            {
                                reply.Text = "Failed to add " + quote.ToString(quoteTimeZone);
                            }
                        }
                        else
                        {
                            reply.Text = "Nope! Not adding empty quote";
                        }

                        break;
                    }

                case quoteDel:
                    {
                        long id = GetRequestQuoteId(request, reply);

                        if (id != -1)
                        {
                            Quote deletedQuote = delQuoteFromDb(id);
                            if (deletedQuote != null)
                            {
                                reply.Text = "Succesfully deleted " + deletedQuote.ToString(quoteTimeZone);
                            }
                            else
                            {
                                reply.Text = "Could not delete quote #" + id;
                            }
                        }

                        break;
                    }

                case quoteGet:
                    {
                        long id = GetRequestQuoteId(request, reply);
                        Quote quote = getQuoteById(id);
                        if (quote != null)
                        {
                            reply.Text = quote.ToString(quoteTimeZone);
                        }
                        else
                        {
                            reply.Text = "Could not find quote #" + id;
                        }
                        break;
                    }

                case quoteRandom:
                    {
                        Quote quote = getRandomQuote();
                        if (quote != null)
                        {
                            reply.Text = quote.ToString(quoteTimeZone);
                        }
                        else
                        {
                            reply.Text = "Could not get random quote";
                        }
                        break;
                    }

                default:
                    Debug.WriteLine("Unsupported quote command '" + command + "' (request '" + request + "')");
                    reply.Text = "Unsupported quote command '" + command + "' (request '" + request + "')";
                    break;
            }

            return reply;
        }
Example #3
0
        private bool addQuoteToDb(ref Quote quote)
        {
            quote.id = -1;
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(
                      "INSERT INTO quotes (channel, timestamp, user, text) values ("
                      + "@channelname, @timestamp, @username, @text);"
                      + " SELECT last_insert_rowid()",
                      dbConn);
                cmd.Parameters.AddWithValue("@channelname", quote.channel);
                //No timezone adjustment for the database, this should be done in the frontend (for the local timezone since some weirdos don't live in CET)
                cmd.Parameters.AddWithValue("@timestamp", quote.timestamp.ToUnix());
                cmd.Parameters.AddWithValue("@username", quote.user);
                cmd.Parameters.AddWithValue("@text", quote.text);

                Debug.WriteLine("Executing sql:");
                Debug.WriteLine(cmd.CommandText);
                quote.id = (long)cmd.ExecuteScalar();
            }
            catch (InvalidOperationException e)
            {
                Debug.WriteLine("InvalidOperationException caught: " + e.ToString());
            }
            return quote.id != -1;
        }