Added SQLAlchemy and started tracking bot state in database.

Moved the config management to its own class.
feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent bae4f0e42a
commit 68d2055707
  1. 1
      .gitignore
  2. 1
      Pipfile
  3. 9
      Pipfile.lock
  4. 3
      config.example.json
  5. 1
      config.py
  6. 12
      database.py
  7. 16
      database_session.py
  8. 40
      webhook-bridge.py

1
.gitignore vendored

@ -1 +1,2 @@
config.json
db.sqlite

@ -12,6 +12,7 @@ requests = "*"
cryptography = "*"
future = "*"
"discord.py" = {git = "https://github.com/Rapptz/discord.py.git"}
sqlalchemy = "*"
[requires]
python_version = "3.6"

9
Pipfile.lock generated

@ -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",

@ -1,5 +1,8 @@
{
"MAIN": {
"WEBHOOK_URL": ""
},
"DATABASE": {
"CONNECTION_STRING": ""
}
}

@ -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)

@ -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

@ -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

@ -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)

Loading…
Cancel
Save