diff --git a/.gitignore b/.gitignore index d344ba6..60c069f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.json +db.sqlite diff --git a/Pipfile b/Pipfile index a74b0da..c3a81bb 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,7 @@ requests = "*" cryptography = "*" future = "*" "discord.py" = {git = "https://github.com/Rapptz/discord.py.git"} +sqlalchemy = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index 01dc33f..a37ca8d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "32ddb27c51c3258cd2abbf1de5432269e16fdced75a4b251ca3a1ed8d62c0cfb" + "sha256": "d86ac26059caa9dc880cea720fdbc3726de055c74589031e68afa03b55a0500c" }, "pipfile-spec": 6, "requires": { @@ -143,6 +143,13 @@ ], "version": "==1.11.0" }, + "sqlalchemy": { + "hashes": [ + "sha256:c5951d9ef1d5404ed04bae5a16b60a0779087378928f997a294d1229c6ca4d3e" + ], + "index": "pypi", + "version": "==1.2.12" + }, "urllib3": { "hashes": [ "sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf", diff --git a/config.example.json b/config.example.json index 4785475..4dd9296 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,8 @@ { "MAIN": { "WEBHOOK_URL": "" + }, + "DATABASE": { + "CONNECTION_STRING": "" } } diff --git a/config.py b/config.py index 2b18337..b2a7057 100644 --- a/config.py +++ b/config.py @@ -7,6 +7,7 @@ class Configuration(object): self._config = json.load(f) if self._config: self.webhook_url = self._config["MAIN"]["WEBHOOK_URL"] + self.database_connection_string = self._config["DATABASE"]["CONNECTION_STRING"] else: print("error reading config") exit(1) diff --git a/database.py b/database.py new file mode 100644 index 0000000..0247a6d --- /dev/null +++ b/database.py @@ -0,0 +1,12 @@ +from sqlalchemy import Column, String, Integer, Date +from sqlalchemy.ext.declarative import declarative_base +from database_session import Base + +class DiscordChannel(Base): + __tablename__ = 'discord_channels' + + id = Column(Integer, primary_key=True) + channel_id = Column(Integer) + + def __init__(self, channel_id): + self.channel_id = channel_id \ No newline at end of file diff --git a/database_session.py b/database_session.py new file mode 100644 index 0000000..3388913 --- /dev/null +++ b/database_session.py @@ -0,0 +1,16 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +_engine = None +Base = declarative_base() + +def initialize(config): + global _engine + _connection_string = config.database_connection_string + _engine = create_engine(_connection_string) + Base.metadata.create_all(_engine) + +def get_session(): + Session = sessionmaker(bind=_engine)() + return Session \ No newline at end of file diff --git a/webhook-bridge.py b/webhook-bridge.py index 86ca762..b893561 100755 --- a/webhook-bridge.py +++ b/webhook-bridge.py @@ -9,6 +9,8 @@ import requests import json from optparse import OptionParser from config import Configuration +from database import DiscordChannel +import database_session from minecraft import authentication from minecraft.exceptions import YggdrasilError @@ -72,6 +74,8 @@ def main(): WEBHOOK_URL = config.webhook_url + database_session.initialize(config) + if options.offline: print("Connecting in offline mode...") connection = Connection( @@ -153,7 +157,41 @@ def main(): @discord_bot.event async def on_message(message): - if not message.author.bot: + this_channel = message.channel.id + if message.content.startswith("mc!chathere"): + session = database_session.get_session() + channels = session.query(DiscordChannel).filter_by(channel_id=this_channel).all() + print(channels) + if not channels: + new_channel = DiscordChannel(this_channel) + session.add(new_channel) + session.commit() + session.close() + del session + msg = "The bot will now start chatting here! To stop this, run `mc!stopchathere`." + await discord_bot.send_message(message.channel, msg) + else: + msg = "The bot is already chatting in this channel! To stop this, run `mc!stopchathere`." + await discord_bot.send_message(message.channel, msg) + return + + elif message.content.startswith("mc!stopchathere"): + session = database_session.get_session() + channels = session.query(DiscordChannel).all() + deleted = session.query(DiscordChannel).filter_by(channel_id=this_channel).delete() + session.commit() + session.close() + print(deleted) + if deleted < 1: + msg = "The bot was not chatting here!" + await discord_bot.send_message(message.channel, msg) + return + else: + msg = "The bot will no longer here!" + await discord_bot.send_message(message.channel, msg) + return + + elif not message.author.bot: await discord_bot.delete_message(message) packet = serverbound.play.ChatPacket() packet.message = "{}: {}".format(message.author.name, message.content)