Skip to content

Commit

Permalink
ops: Create the database service
Browse files Browse the repository at this point in the history
  • Loading branch information
Seniru committed May 14, 2023
1 parent 2aa7d38 commit e4da29f
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ COPY . .

RUN pip install --no-cache-dir -r requirements.txt

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh

CMD ["./script.bash"]
42 changes: 42 additions & 0 deletions database/CustomCommands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- MySQL dump 10.13 Distrib 8.0.33, for Linux (x86_64)
--
-- Host: sql9.freesqldatabase.com Database: sql9617855
-- ------------------------------------------------------
-- Server version 5.5.62-0ubuntu0.14.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `CustomCommands`
--

DROP TABLE IF EXISTS `CustomCommands`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `CustomCommands` (
`name` varchar(15) NOT NULL,
`runner` varchar(20) NOT NULL,
`source` varchar(40) NOT NULL,
`author` bigint(20),
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `CustomCommands`
--

LOCK TABLES `CustomCommands` WRITE;
/*!40000 ALTER TABLE `CustomCommands` DISABLE KEYS */;
INSERT INTO `CustomCommands` VALUES ('8ball','cpython-3.8.9','https://pastebin.com/raw/ExMLuAgv','544776631672242176'),('coinflip','cpython-3.8.9','https://pastebin.com/raw/f8v5EGQF','544776631672242176'),('death','cpython-3.8.9','https://pastebin.com/raw/nLtRfDhs','544776631672242176'),('dice','cpython-3.8.9','https://pastebin.com/raw/LmxRAKE1','544776631672242176'),('dog','cpython-3.8.9','https://pastebin.com/raw/bijXTmat','544776631672242176'),('embed','cpython-3.8.9','https://pastebin.com/raw/AbPX16PA','522972601488900097'),('frog','cpython-3.8.9','https://pastebin.com/raw/bwpCSAvi','544776631672242176'),('graph','cpython-3.8.9','https://pastebin.com/raw/YH0YyPAk','522972601488900097'),('howto','cpython-3.8.9','https://pastebin.com/raw/amMuvd1B','522972601488900097'),('no','cpython-3.8.9','https://pastebin.com/raw/tvprgEqC','522972601488900097'),('order','cpython-3.8.9','https://pastebin.com/raw/B38n572h','544776631672242176'),('percentage','cpython-3.8.9','https://pastebin.com/raw/QghKi731','544776631672242176'),('raw','cpython-3.8.9','https://pastebin.com/raw/Mqq8TkXT','522972601488900097'),('ricardo','cpython-3.8.9','https://pastebin.com/raw/3LRJ8Q6A','522972601488900097'),('tex','cpython-3.8.9','https://pastebin.com/raw/hE2W5YKs','522972601488900097'),('worldclock','cpython-3.8.9','https://pastebin.com/raw/yBueDaSJ','522972601488900097'),('yes','cpython-3.8.9','https://pastebin.com/raw/UguBK6yg','544776631672242176');
/*!40000 ALTER TABLE `CustomCommands` ENABLE KEYS */;
UNLOCK TABLES;
11 changes: 11 additions & 0 deletions database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM mysql:5.7

WORKDIR /database

ADD *.sql /docker-entrypoint-initdb.d/
ADD /run-til-midnight.sh /
RUN chmod +x /run-til-midnight.sh

EXPOSE 3306
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["/run-til-midnight.sh", "mysqld"]
4 changes: 4 additions & 0 deletions database/ModData.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS `Warnings` (
`member` bigint(20) NOT NULL
`reason` varchar(100) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
18 changes: 18 additions & 0 deletions database/Qotd.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS `QOTD` (
`id` int NOT NULL,
`question` VARCHAR(200) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `QOTData` (
`kind` CHAR(5) NOT NULL,
`value` VARCHAR(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `QOTData`
VALUES('latest', '$current_time')
ON DUPLICATE KEY IGNORE;

INSERT INTO `QOTData`
VALUES('index', '0')
ON DUPLICATE KEY IGNORE;
8 changes: 8 additions & 0 deletions database/run-til-midnight.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
while true
do
# Run until midnight
midnight=$(date -d 'tomorrow 00:05:00' +%s)
now=$(date +%s)
seconds=$(($midnight - $now))
timeout --preserve-status ${seconds}s "$@"
done
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
version: "3.3"
services:
db:
container_name: db
build: ./database
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root
volumes:
- my-db:/var/lib/mysql

bot:
container_name: bot
depends_on:
- db
build: .
env_file: ./.env
working_dir: /src
volumes:
- ./:/src
volumes:
my-db:
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiohttp==3.8.4
aiomysql==0.1.1
aiosignal==1.3.1
aiotfm==1.4.8
async-timeout==4.0.2
Expand All @@ -10,7 +11,9 @@ docstring-parser==0.15
frozenlist==1.3.3
idna==3.4
multidict==6.0.4
mysqlclient==2.1.1
PyMySQL==1.0.3
requests==2.30.0
urllib3<2
urllib3==2.0.2
w3lib==2.1.1
yarl==1.9.2
2 changes: 2 additions & 0 deletions script.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

sh /wait-for-it.sh "db:3306" --timeout=60

function run() {
python -u src/main.py || run
}
Expand Down
15 changes: 3 additions & 12 deletions src/bots/Discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ async def on_message(self, message):
}))

await cmd["f"](args[1:], message, self)
elif args[0] in self.ccmds:
else:



ccmd = self.ccmds[args[0]]
code = requests.get(ccmd["source"]).content.decode("utf-8")
Expand Down Expand Up @@ -119,17 +121,6 @@ async def on_message(self, message):
}))

async def on_interaction(self, interaction: discord.Interaction):
#await interaction.end(content = "** **")
#if cmd_name in commands and commands[cmd_name]["discord"]:
# cmd = commands[cmd_name]
# interaction.reply = self.main_guild.get_channel(interaction.channel.id).send
# interaction.send = self.main_guild.get_channel(interaction.channel.id).send
# interaction.options = list(map(lambda o: o.value, interaction.command.options))
# interaction.mentions = list(
# map(
# lambda m: self.main_guild.get_member(int(re.match(r".*?(\d+).*", m)[1])),
# filter(lambda o: re.match(r"^<@!?(\d+)>$", o), interaction.options)
# ))
interaction = utils.MockInteraction(interaction, self)

if interaction.type == discord.InteractionType.application_command:
Expand Down
46 changes: 32 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import os
import asyncio

from bots.Transformice import Transformice
#sys.path.append("discordslashcommands")
from bots.Discord import Discord

print("[INFO][SYSTME] Starting...")
import aiomysql
async def test():
import json
aiomysql.create_pool()
conn = await aiomysql.connect(host="db", user="user", db="db", password="password", port=3306)
cur = await conn.cursor()

await cur.execute("select * from test;")
r = await cur.fetchall()
print(r)
await conn.commit()
await cur.close()
conn.close()

loop = asyncio.get_event_loop()

discord = Discord()
tfm = Transformice(os.getenv("USERNAME"), os.getenv("PASSWORD"), loop, discord)

discord.set_tfm_instance(tfm)

print("[INFO][DISCORD] Starting...")
loop.create_task(discord.start(os.getenv("DISCORD")))
tfm.run()
loop.run_forever()
loop.run_until_complete(test())
loop.run_forever()
#from bots.Transformice import Transformice
##sys.path.append("discordslashcommands")
#from bots.Discord import Discord
#
#print("[INFO][SYSTME] Starting...")
#
#loop = asyncio.get_event_loop()
#
#discord = Discord()
#tfm = Transformice(os.getenv("USERNAME"), os.getenv("PASSWORD"), loop, discord)
#
#discord.set_tfm_instance(tfm)
#
#print("[INFO][DISCORD] Starting...")
#loop.create_task(discord.start(os.getenv("DISCORD")))
##tfm.run()
#loop.run_forever()

0 comments on commit e4da29f

Please sign in to comment.