Geoffrey-Django/Geoffrey.py

198 lines
6.3 KiB
Python
Raw Normal View History

2018-05-21 15:59:35 +00:00
from discord.ext import commands
from DatabaseModels import *
from BotErrors import *
2018-06-03 02:42:31 +00:00
from MinecraftAccountInfoGrabber import *
2018-05-21 21:09:52 +00:00
2018-05-21 16:57:20 +00:00
TOKEN = ''
2018-05-21 15:59:35 +00:00
command_prefix = '?'
2018-05-25 16:32:29 +00:00
description = '''
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/shops are.
2018-05-22 02:41:15 +00:00
Please respect Geoffrey, the bot is very sensitive.w
2018-05-22 02:41:15 +00:00
'''
2018-05-25 16:32:29 +00:00
2018-05-25 22:00:43 +00:00
bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \
2018-05-26 14:26:24 +00:00
'headquarters are working VEWY HAWD to fix this! (Error in command {})'
2018-05-25 22:00:43 +00:00
2018-05-25 16:32:29 +00:00
bot = commands.Bot(command_prefix=command_prefix, description=description, case_insensitive=True)
database = GeoffreyDatabase('sqlite:///:memory:')
2018-05-25 16:32:29 +00:00
2018-06-03 02:42:31 +00:00
2018-05-25 16:32:29 +00:00
# Bot Commands ******************************************************************
2018-05-21 15:59:35 +00:00
@bot.event
async def on_ready():
2018-05-25 16:32:29 +00:00
print('GeoffreyBot')
print('Username: ' + bot.user.name)
print('ID: ' + bot.user.id)
@bot.event
async def on_command_error(error, ctx):
2018-05-25 20:30:47 +00:00
if isinstance(error, commands.CommandNotFound):
error_str = 'Command not found, ding dongs like you can use ?help to see all the commands this bot can do.'
elif isinstance(error, commands.UserInputError):
error_str = 'Invalid syntax for {} you ding dong, please read ?help {}.'\
.format(ctx.invoked_with, ctx.invoked_with)
2018-06-03 02:42:31 +00:00
elif isinstance(error.original, UsernameLookupFailed):
error_str = error.original.__doc__
2018-05-25 20:30:47 +00:00
else:
2018-05-25 22:00:43 +00:00
error_str = bad_error_message.format(ctx.invoked_with)
print(error)
2018-05-25 20:30:47 +00:00
2018-05-25 16:32:29 +00:00
await bot.send_message(ctx.message.channel, error_str)
2018-05-21 15:59:35 +00:00
@bot.command()
async def test():
2018-05-25 21:06:00 +00:00
'''
Checks if the bot is alive.
'''
2018-05-25 16:32:29 +00:00
await bot.say('I\'m here you ding dong')
2018-05-21 15:59:35 +00:00
@bot.command(pass_context=True)
2018-05-25 21:06:00 +00:00
async def addbase(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, * args):
2018-05-25 16:32:29 +00:00
'''
Add your base to the database.
The tunnel address is optional.
?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Tunnel Position]
'''
2018-06-23 15:17:51 +00:00
if ctx.message.author.nick is None:
player_name = ctx.message.author.display_name
else:
player_name = ctx.message.author.nick
expr = Player.name == player_name
player_list = database.query_by_filter(Player, expr)
if len(player_list) == 0:
uuid = grab_UUID(player_name)
expr = Player.id == uuid
player_list = database.query_by_filter(Player, expr)
if len(player_list) == 0:
owner = Player(player_name)
else:
player_list[0].name == player_name
else:
owner = player_list[0]
try:
2018-06-23 15:17:51 +00:00
base = Location(name, x_pos, y_pos, z_pos, owner, args)
except LocationInitError:
raise commands.UserInputError
2018-05-25 16:32:29 +00:00
2018-06-23 15:17:51 +00:00
owner.locations.append(base)
database.add_object(base)
2018-06-23 15:17:51 +00:00
database.add_object(owner)
2018-05-25 16:32:29 +00:00
2018-06-23 15:17:51 +00:00
#expr = Player.id == owner.id
#player_list = database.query_by_filter(Player, expr)
'''
if len(player_list) == 0:
database.add_object(owner)
else:
if player_list[0].name != owner.name:
player_list[0].name = owner.name
database.session.commit()
'''
2018-05-25 16:32:29 +00:00
await bot.say('{}, your base named {} located at {} has been added'
2018-05-25 20:30:47 +00:00
' to the database.'.format(ctx.message.author.mention, base.name, base.pos_to_str()))
2018-05-25 16:32:29 +00:00
@bot.command(pass_context=True)
2018-05-25 21:06:00 +00:00
async def findbase(ctx, name: str):
'''
2018-05-25 21:33:18 +00:00
Finds a base in the database.
2018-05-25 16:32:29 +00:00
?findbase [Player name]
'''
2018-06-03 02:42:31 +00:00
uuid = grab_UUID(name)
2018-06-23 15:17:51 +00:00
expr = Player.id == uuid
p = database.query_by_filter(Player, expr)[0]
expr = Location.owner == p
base_list = database.query_by_filter(Location, expr)
2018-05-25 21:06:00 +00:00
2018-06-23 15:17:51 +00:00
if len(base_list) != 0:
2018-05-26 14:26:24 +00:00
base_string = base_list_string(base_list, '{} \n{}')
2018-05-25 21:06:00 +00:00
2018-05-26 14:26:24 +00:00
await bot.say('{}, {} has {} base(s): \n {}'.format(ctx.message.author.mention, name, len(base_list),
base_string))
2018-05-25 16:32:29 +00:00
else:
await bot.say('{}, the player {} is not in the database'.format(ctx.message.author.mention, name))
2018-05-25 16:32:29 +00:00
2018-05-26 14:26:24 +00:00
def base_list_string(base_list, str_format):
base_string = ''
for base in base_list:
base_string = str_format.format(base_string, base)
return base_string
2018-05-25 21:33:18 +00:00
@bot.command(pass_context=True)
async def deletebase(ctx, name: str):
'''
Deletes a base from the database.
?deletebase [Base name]
'''
2018-06-23 15:17:51 +00:00
uuid = grab_UUID(str(ctx.message.author.nick))
2018-05-25 21:33:18 +00:00
2018-06-23 15:17:51 +00:00
expr = (Location.owner_id == uuid) & (Location.name == name)
try:
database.delete_entry(Location, expr)
2018-05-25 21:33:18 +00:00
await bot.say('{}, your base named "{}" has been deleted.'.format(ctx.message.author.mention, name))
except DeleteEntryError:
2018-05-25 21:33:18 +00:00
await bot.say('{}, you do not have a base named "{}".'.format(ctx.message.author.mention, name))
2018-06-02 17:40:55 +00:00
2018-05-26 14:26:24 +00:00
@bot.command(pass_context=True)
async def findbasearound(ctx, x_pos: int, z_pos: int, * args):
'''
Finds all the base around a certain point that are registered in the database
The Radius argument defaults to 200 blocks if no value is given
?findbasearound [X Coordinate] [Z Coordinate] [Radius]
'''
2018-05-26 14:26:24 +00:00
radius = 200
if len(args) > 0:
try:
radius = int(args[0])
except ValueError:
raise commands.UserInputError
2018-05-26 14:26:24 +00:00
expr = (Location.x < x_pos + radius) & (Location.x > x_pos - radius) & (Location.z < z_pos + radius) & \
(Location.z > z_pos - radius)
base_list = database.query_by_filter(Location, expr)
2018-05-26 14:26:24 +00:00
if len(base_list) != 0:
base_string = base_list_string(base_list, '{} \n{}')
await bot.say('{}, there are {} base(s) within {} blocks of that point: \n {}'.format(
2018-05-26 14:26:24 +00:00
ctx.message.author.mention, len(base_list), radius, base_string))
else:
await bot.say('{}, there are no bases within {} blocks of that point'
.format(ctx.message.author.mention, radius))
2018-05-26 14:26:24 +00:00
2018-05-25 16:32:29 +00:00
# Bot Startup ******************************************************************
try:
file = open('token.dat', 'r')
TOKEN = file.read()
2018-05-21 16:57:20 +00:00
except FileNotFoundError:
2018-05-25 16:32:29 +00:00
print('token.dat not found.')
2018-05-21 16:57:20 +00:00
except IOError:
2018-05-25 16:32:29 +00:00
print('Error reading token.dat')
2018-05-21 16:57:20 +00:00
2018-05-25 16:32:29 +00:00
bot.run(TOKEN)