Implemented sql alchemy
parent
4042052498
commit
248e108ea1
44
MCInfoBot.py
44
MCInfoBot.py
|
@ -1,5 +1,12 @@
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy import Column, Integer, String
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
engine = create_engine('sqlite:///:memory:', echo=True)
|
||||||
|
SQL_Base = declarative_base()
|
||||||
|
|
||||||
TOKEN = ''
|
TOKEN = ''
|
||||||
command_prefix = '?'
|
command_prefix = '?'
|
||||||
|
@ -8,11 +15,12 @@ least he knows where your bases are.'''
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=command_prefix , description=description)
|
bot = commands.Bot(command_prefix=command_prefix , description=description)
|
||||||
|
|
||||||
class Location:
|
class Location(SQL_Base):
|
||||||
name = ''
|
__tablename__ = 'Locations'
|
||||||
x = 0;
|
name = Column(String, primary_key=True)
|
||||||
y = 0;
|
x = Column(Integer)
|
||||||
z = 0;
|
y = Column(Integer)
|
||||||
|
z = Column(Integer)
|
||||||
|
|
||||||
def __init__(self,args) :
|
def __init__(self,args) :
|
||||||
self.name = args[0]
|
self.name = args[0]
|
||||||
|
@ -23,6 +31,15 @@ class Location:
|
||||||
def posToStr(self) :
|
def posToStr(self) :
|
||||||
return '(x=' + str(self.x) + ', y=' + str(self.y) + ', z=' + str(self.z) + ')'
|
return '(x=' + str(self.x) + ', y=' + str(self.y) + ', z=' + str(self.z) + ')'
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "(name= {}, pos={})".format(self.name,posToStr)
|
||||||
|
|
||||||
|
SQL_Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
session = Session()
|
||||||
|
|
||||||
|
|
||||||
#Bot Commands ******************************************************************
|
#Bot Commands ******************************************************************
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
@ -36,12 +53,14 @@ async def test():
|
||||||
await bot.say('I\'m here you ding dong')
|
await bot.say('I\'m here you ding dong')
|
||||||
|
|
||||||
@bot.command(pass_context=True)
|
@bot.command(pass_context=True)
|
||||||
async def addBase(ctx, * args):
|
async def addbase(ctx, * args):
|
||||||
|
'''Add your base to the database.'''
|
||||||
if (len(args) == 4) :
|
if (len(args) == 4) :
|
||||||
try:
|
try:
|
||||||
base = Location(args)
|
base = Location(args)
|
||||||
|
session.add(base)
|
||||||
await bot.say('{}, your base named {} located at {} has been added'
|
await bot.say('{}, your base named {} located at {} has been added'
|
||||||
'to the database.'.format(ctx.message.author.mention, base.name, base.posToStr()))
|
' to the database.'.format(ctx.message.author.mention, base.name, base.posToStr()))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
await bot.say('Invalid syntax, try again (?addbase [name] [x coord] [z coord])')
|
await bot.say('Invalid syntax, try again (?addbase [name] [x coord] [z coord])')
|
||||||
|
|
||||||
|
@ -49,6 +68,15 @@ async def addBase(ctx, * args):
|
||||||
await bot.say('Allows you to add your base location to the database. '
|
await bot.say('Allows you to add your base location to the database. '
|
||||||
'Syntax: ?addbase [Base Name] [X Coordinate] [Z Coordinate]')
|
'Syntax: ?addbase [Base Name] [X Coordinate] [Z Coordinate]')
|
||||||
|
|
||||||
|
@bot.command(pass_context=True)
|
||||||
|
async def findbase(ctx, * args):
|
||||||
|
'''Allows you to find a base in the database.'''
|
||||||
|
base = session.query(Location).filter_by(name=args[0]).first()
|
||||||
|
if (base is not None) :
|
||||||
|
await bot.say('{}, {} is located at {}.'.format(ctx.message.author.mention, base.name, base.posToStr()))
|
||||||
|
else :
|
||||||
|
await bot.say('{}, {} is not in the database'.format(ctx.message.author.mention, args[0]))
|
||||||
|
|
||||||
#Bot Startup ******************************************************************
|
#Bot Startup ******************************************************************
|
||||||
try :
|
try :
|
||||||
file = open('token.dat','r')
|
file = open('token.dat','r')
|
||||||
|
@ -59,5 +87,3 @@ except IOError:
|
||||||
print('Error reading token.dat')
|
print('Error reading token.dat')
|
||||||
|
|
||||||
bot.run(TOKEN)
|
bot.run(TOKEN)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue