Added global error handling
parent
60846c81d1
commit
ee3bfb0fee
122
MCInfoBot.py
122
MCInfoBot.py
|
@ -8,29 +8,46 @@ from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
TOKEN = ''
|
TOKEN = ''
|
||||||
command_prefix = '?'
|
command_prefix = '?'
|
||||||
description = '''Geoffrey is an inside joke none of you will understand, at
|
description = '''
|
||||||
least he knows where your bases are.
|
Geoffrey started his life as inside joke none of you will understand.
|
||||||
|
At some point, she was to become an airhorn bot. Now, they know where your bases are.
|
||||||
|
|
||||||
Please respect Geoffrey, the bot is very sensitive.
|
Please respect Geoffrey, the bot is very sensitive.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
engine = create_engine('sqlite:///:memory:', echo=True)
|
engine = create_engine('sqlite:///:memory:', echo=True)
|
||||||
SQL_Base = declarative_base()
|
SQL_Base = declarative_base()
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=command_prefix , description=description)
|
bot = commands.Bot(command_prefix=command_prefix, description=description, case_insensitive=True)
|
||||||
|
|
||||||
class TunnelDirection(enum.Enum) :
|
|
||||||
|
class DataBaseError(Exception):
|
||||||
|
'''Base class for exceptions in this module.'''
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class LocationInitError(DataBaseError):
|
||||||
|
'''Error in initializing Location'''
|
||||||
|
|
||||||
|
|
||||||
|
class LocationLookUpError(DataBaseError) :
|
||||||
|
'''Error in finding location in database'''
|
||||||
|
|
||||||
|
|
||||||
|
class TunnelDirection(enum.Enum):
|
||||||
North = 'green'
|
North = 'green'
|
||||||
East = 'blue'
|
East = 'blue'
|
||||||
South = 'red'
|
South = 'red'
|
||||||
West = 'yellow'
|
West = 'yellow'
|
||||||
|
|
||||||
|
|
||||||
class Player(SQL_Base):
|
class Player(SQL_Base):
|
||||||
__tablename__ = 'Players'
|
__tablename__ = 'Players'
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
in_game_name = Column(String)
|
in_game_name = Column(String)
|
||||||
|
|
||||||
def __init__(self,name) :
|
def __init__(self, name):
|
||||||
self.in_game_name = name
|
self.in_game_name = name
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,38 +63,44 @@ class Location(SQL_Base):
|
||||||
direction = Column(Enum(TunnelDirection))
|
direction = Column(Enum(TunnelDirection))
|
||||||
owner = Column(String, ForeignKey('Players.in_game_name'))
|
owner = Column(String, ForeignKey('Players.in_game_name'))
|
||||||
|
|
||||||
def __init__(self,args,owner) :
|
def __init__(self, args, owner):
|
||||||
|
try:
|
||||||
self.name = args[0]
|
self.name = args[0]
|
||||||
self.x = int(args[1])
|
self.x = int(args[1])
|
||||||
self.y = int(args[2])
|
self.y = int(args[2])
|
||||||
self.z = int(args[3])
|
self.z = int(args[3])
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
|
|
||||||
if (len(args) >= 5) :
|
if (len(args) >= 5):
|
||||||
self.tunnelNumber = int(args[5])
|
self.tunnelNumber = int(args[5])
|
||||||
self.direction = strToTunnelDirection(args[4])
|
self.direction = strToTunnelDirection(args[4])
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
raise LocationInitError
|
||||||
|
|
||||||
def posToStr(self) :
|
def posToStr(self):
|
||||||
return '(x= {}, y= {}, z= {})'.format(self.x, self.y, self.z)
|
return '(x= {}, y= {}, z= {})'.format(self.x, self.y, self.z)
|
||||||
def netherTunnelAddrToStr(self) :
|
|
||||||
|
def netherTunnelAddrToStr(self):
|
||||||
return '{} {}'.format(self.direction.value.title(), self.tunnelNumber)
|
return '{} {}'.format(self.direction.value.title(), self.tunnelNumber)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if (self.direction is not None) :
|
if (self.direction is not None):
|
||||||
return "Name: {}, Position: {}, Tunnel: {}".format(self.name,self.posToStr(),self.netherTunnelAddrToStr())
|
return "Name: {}, Position: {}, Tunnel: {}".format(self.name, self.posToStr(), self.netherTunnelAddrToStr())
|
||||||
else :
|
else:
|
||||||
return "Name: {}, Position: {}".format(self.name,self.posToStr())
|
return "Name: {}, Position: {}".format(self.name, self.posToStr())
|
||||||
|
|
||||||
|
|
||||||
SQL_Base.metadata.create_all(engine)
|
SQL_Base.metadata.create_all(engine)
|
||||||
|
|
||||||
Session = sessionmaker(bind=engine)
|
Session = sessionmaker(bind=engine)
|
||||||
session = Session()
|
session = Session()
|
||||||
|
|
||||||
def strToTunnelDirection(str) :
|
|
||||||
|
def strToTunnelDirection(str):
|
||||||
str = str.lower()
|
str = str.lower()
|
||||||
if (str == TunnelDirection.North.value) :
|
if (str == TunnelDirection.North.value):
|
||||||
return TunnelDirection.North
|
return TunnelDirection.North
|
||||||
elif (str == TunnelDirection.East.value) :
|
elif (str == TunnelDirection.East.value):
|
||||||
return TunnelDirection.East
|
return TunnelDirection.East
|
||||||
elif (str == TunnelDirection.South.value):
|
elif (str == TunnelDirection.South.value):
|
||||||
return TunnelDirection.South
|
return TunnelDirection.South
|
||||||
|
@ -86,76 +109,67 @@ def strToTunnelDirection(str) :
|
||||||
else:
|
else:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
#Bot Commands ******************************************************************
|
|
||||||
|
# Bot Commands ******************************************************************
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('GeoffreyBot')
|
print('GeoffreyBot')
|
||||||
print('Username: ' + bot.user.name)
|
print('Username: ' + bot.user.name)
|
||||||
print('ID: ' + bot.user.id)
|
print('ID: ' + bot.user.id)
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_command_error(error, ctx):
|
||||||
|
error_str = 'Error in command {}, please use ?help {}'.format(ctx.invoked_with, ctx.invoked_with)
|
||||||
|
await bot.send_message(ctx.message.channel, error_str)
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def test():
|
async def test():
|
||||||
'''Check if the bot is alive.'''
|
'''Check if the bot is alive.'''
|
||||||
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.
|
'''
|
||||||
|
Add your base to the database.
|
||||||
|
The tunnel address is optional.
|
||||||
?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Tunnel Position]
|
?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Tunnel Position]
|
||||||
'''
|
'''
|
||||||
if (len(args) == 4) :
|
|
||||||
try:
|
|
||||||
owner = Player(str(ctx.message.author.nick))
|
owner = Player(str(ctx.message.author.nick))
|
||||||
base = Location(args,owner.in_game_name)
|
base = Location(args, owner.in_game_name)
|
||||||
|
|
||||||
session.add(owner)
|
session.add(owner)
|
||||||
session.add(base)
|
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:
|
|
||||||
await bot.say('Invalid syntax, try again (?addbase [name] [x coord] [z coord])')
|
|
||||||
elif(len(args) == 6) :
|
|
||||||
try:
|
|
||||||
owner = Player(str(ctx.message.author.nick))
|
|
||||||
base = Location(args,owner.in_game_name)
|
|
||||||
|
|
||||||
session.add(owner)
|
|
||||||
session.add(base)
|
|
||||||
|
|
||||||
await bot.say('{}, your base named {} located at {} has been added'
|
|
||||||
' to the database.'.format(ctx.message.author.mention, base.name, base.posToStr()))
|
|
||||||
except ValueError:
|
|
||||||
await bot.say('Invalid syntax, try again (?addbase [name] [x coord] [z coord] [Tunnel Color] [Tunnel Position])')
|
|
||||||
|
|
||||||
else :
|
|
||||||
await bot.say('Allows you to add your base location to the database. '
|
|
||||||
'Syntax: ?addbase [Base Name] [X Coordinate] [Z Coordinate]')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bot.command(pass_context=True)
|
@bot.command(pass_context=True)
|
||||||
async def findbase(ctx, * args):
|
async def findbase(ctx, *args):
|
||||||
'''Allows you to find a base in the database.
|
'''Allows you to find a base in the database.
|
||||||
?findbase [Player name]
|
?findbase [Player name]
|
||||||
'''
|
'''
|
||||||
if (len(args) > 0) :
|
search_key = args[0]
|
||||||
|
baseList = session.query(Location).filter_by(owner=search_key).all()
|
||||||
|
|
||||||
baseList = session.query(Location).filter_by(owner=args[0]).all()
|
if baseList is not None:
|
||||||
|
await bot.say('{}, {} has {} base(s):'.format(ctx.message.author.mention, args[0], len(baseList)))
|
||||||
if (baseList is not None) :
|
|
||||||
await bot.say('{}, {} has {} base(s):'.format(ctx.message.author.mention,args[0], len(baseList)))
|
|
||||||
for base in baseList:
|
for base in baseList:
|
||||||
await bot.say(base)
|
await bot.say(base)
|
||||||
else :
|
else:
|
||||||
await bot.say('{}, {} is not in the database'.format(ctx.message.author.mention, args[0]))
|
await bot.say('{}, {} is not in the database'.format(ctx.message.author.mention, args[0]))
|
||||||
else :
|
|
||||||
await bot.say('Allows you to add your base location to the database. '
|
|
||||||
'Syntax: ?findbase [Base Name] ')
|
|
||||||
|
|
||||||
#Bot Startup ******************************************************************
|
|
||||||
try :
|
|
||||||
file = open('token.dat','r')
|
# Bot Startup ******************************************************************
|
||||||
|
try:
|
||||||
|
file = open('token.dat', 'r')
|
||||||
TOKEN = file.read()
|
TOKEN = file.read()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('token.dat not found.')
|
print('token.dat not found.')
|
||||||
|
|
Loading…
Reference in New Issue