From a1d25336b79c05b472926eb9fbe0fc8b38335e23 Mon Sep 17 00:00:00 2001 From: fzorb Date: Fri, 4 Oct 2024 22:17:09 +0300 Subject: [PATCH] add trigger stuff --- .env.example | 2 +- bot.rb | 1 + sample.sql | 13 +++++++++++-- systems/trigger.rb | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 systems/trigger.rb diff --git a/.env.example b/.env.example index 03baf6c..b91b7c8 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,5 @@ TOKEN= CLIENT_ID= PREFIX=; ADMIN= - +BOT_ID= FORBIDDEN_WORDS=word1,word2 diff --git a/bot.rb b/bot.rb index 452bbe0..a365cfb 100644 --- a/bot.rb +++ b/bot.rb @@ -29,4 +29,5 @@ systems.each { |i| require_relative "#{i}" } bot.include! QuoteSystem bot.include! NetworkModule +bot.include! TriggerSystem bot.run diff --git a/sample.sql b/sample.sql index a6edaf9..9758bb6 100644 --- a/sample.sql +++ b/sample.sql @@ -13,8 +13,6 @@ /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - - -- Dumping structure for table jon.quotes CREATE TABLE IF NOT EXISTS "quotes" ( "id" INTEGER NOT NULL, @@ -26,6 +24,17 @@ CREATE TABLE IF NOT EXISTS "quotes" ( -- Data exporting was unselected. +-- Dumping structure for table jon.triggers +CREATE TABLE IF NOT EXISTS "triggers" ( + "id" INTEGER NOT NULL, + "trigger" TEXT NOT NULL DEFAULT '', + "response" TEXT NOT NULL DEFAULT '', + "server" INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY ("id") +); + +-- Data exporting was unselected. + /*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */; /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; diff --git a/systems/trigger.rb b/systems/trigger.rb new file mode 100644 index 0000000..be13e4f --- /dev/null +++ b/systems/trigger.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'discordrb' +require 'sqlite3' +require 'dotenv' + +Dotenv.load("#{__dir__}/../.env") + +module TriggerSystem + db = SQLite3::Database.new("jon.sqlite") + extend Discordrb::EventContainer + message do |event| + if event.message.author.id == ENV["BOT_ID"] + return + end + triggers = db.execute("SELECT * FROM triggers WHERE server=?", [event.message.channel.server.id]) + for trigger in triggers do + if event.message.content.include? trigger[1] + event.channel.send_message(trigger[2]) + end + end + nil + end + + extend Discordrb::Commands::CommandContainer + command :addtrigger do |event, *message| + if event.author.id != event.channel.server.owner.id + event.channel.send_message("Only the owner can setup triggers.") + return + end + input = message[0..].join(' ').split("#") + db.execute('INSERT INTO "triggers" ("trigger", "response", "server") VALUES (?,?,?);', [input[0], input[1], event.channel.server.id]) + event.channel.send_message("Trigger added") + nil + end +end \ No newline at end of file