2018-05-21 15:59:35 +00:00
|
|
|
from discord.ext import commands
|
2018-05-26 15:07:17 +00:00
|
|
|
from DatabaseModels import *
|
2018-05-29 14:04:01 +00:00
|
|
|
from BotErrors import *
|
2018-06-03 02:42:31 +00:00
|
|
|
from MinecraftAccountInfoGrabber import *
|
2018-07-15 18:27:11 +00:00
|
|
|
from itertools import zip_longest
|
2018-06-30 15:53:27 +00:00
|
|
|
import configparser
|
2018-07-15 15:56:56 +00:00
|
|
|
import shlex
|
2018-07-01 15:10:10 +00:00
|
|
|
#from WebInterface import *
|
2018-06-03 02:42:31 +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.
|
2018-05-26 15:07:17 +00:00
|
|
|
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
|
|
|
|
2018-07-01 15:10:10 +00:00
|
|
|
Please respect Geoffrey, the bot is very sensitive.
|
2018-07-14 22:25:33 +00:00
|
|
|
|
|
|
|
*You must use ?register before adding entries into Geoffrey*
|
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-06-30 15:07:56 +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)
|
|
|
|
|
2018-06-23 16:08:40 +00:00
|
|
|
# Bot Commands ******************************************************************'
|
|
|
|
|
2018-06-03 02:42:31 +00:00
|
|
|
|
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):
|
2018-06-02 16:11:31 +00:00
|
|
|
error_str = 'Command not found, ding dongs like you can use ?help to see all the commands this bot can do.'
|
2018-05-26 14:53:36 +00:00
|
|
|
elif isinstance(error, commands.UserInputError):
|
2018-06-02 16:11:31 +00:00
|
|
|
error_str = 'Invalid syntax for {} you ding dong, please read ?help {}.'\
|
|
|
|
.format(ctx.invoked_with, ctx.invoked_with)
|
2018-07-08 19:19:40 +00:00
|
|
|
database_interface.database.session.rollback()
|
2018-06-03 02:42:31 +00:00
|
|
|
elif isinstance(error.original, UsernameLookupFailed):
|
|
|
|
error_str = error.original.__doc__
|
2018-07-04 00:05:35 +00:00
|
|
|
elif isinstance(error.original, OverflowError):
|
|
|
|
error_str = 'Wow buddy, that\'s a big number. Please don\'t do that.'
|
2018-07-08 19:19:40 +00:00
|
|
|
database_interface.database.session.rollback()
|
|
|
|
elif isinstance(error.original, PlayerNotFound):
|
|
|
|
error_str = 'Make sure to use ?register first you ding dong.'
|
|
|
|
database_interface.database.session.rollback()
|
2018-07-15 15:56:56 +00:00
|
|
|
elif isinstance(error.original, LocationNameNotUniqueError):
|
2018-07-14 22:25:33 +00:00
|
|
|
error_str = 'An entry in the database already has that name ding dong.'
|
|
|
|
database_interface.database.session.rollback()
|
2018-05-25 20:30:47 +00:00
|
|
|
else:
|
2018-06-30 15:07:56 +00:00
|
|
|
error_str = bad_error_message.format(ctx.invoked_with, 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-07-08 19:19:40 +00:00
|
|
|
@bot.command(pass_context=True)
|
|
|
|
async def register(ctx):
|
|
|
|
'''
|
2018-07-15 18:27:11 +00:00
|
|
|
Registers your Discord and Minecraft account with the the database. You must do this before adding entries to
|
2018-07-08 19:19:40 +00:00
|
|
|
the database.
|
|
|
|
'''
|
|
|
|
|
|
|
|
player_name = get_nickname(ctx.message.author)
|
|
|
|
|
|
|
|
try:
|
|
|
|
database_interface.add_player(player_name, ctx.message.author.id)
|
|
|
|
except LocationInitError:
|
|
|
|
raise commands.UserInputError
|
|
|
|
|
|
|
|
await bot.say('{}, you have been added to the database.'.format(ctx.message.author.mention))
|
|
|
|
|
2018-06-23 16:08:40 +00:00
|
|
|
|
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.
|
2018-07-15 18:27:11 +00:00
|
|
|
The tunnel address is optional.
|
|
|
|
The default dimension is the overworld. Valid options: overworld, nether, end
|
|
|
|
|
|
|
|
?addbase [Base Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Tunnel Color] [Optional Flags]
|
|
|
|
|
|
|
|
Optional Flags:
|
|
|
|
-t [Tunnel Color],[Tunnel Position],[Side]
|
|
|
|
-d [dimension]
|
2018-05-25 16:32:29 +00:00
|
|
|
'''
|
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
flags = get_args_dict(args)
|
|
|
|
tunnel = None
|
|
|
|
dimension = None
|
|
|
|
|
|
|
|
if len(flags) > 0:
|
|
|
|
if '-t' in flags:
|
|
|
|
tunnel = flags['-t']
|
|
|
|
|
|
|
|
if '-d' in flags:
|
|
|
|
dimension = flags['-d']
|
|
|
|
|
2018-05-26 14:53:36 +00:00
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
await bot.say('Test ' + tunnel)
|
2018-05-26 14:53:36 +00:00
|
|
|
try:
|
2018-07-15 18:27:11 +00:00
|
|
|
shop = database_interface.add_shop(ctx.message.author.id, name, x_pos, y_pos, z_pos, tunnel, dimension)
|
2018-05-26 14:53:36 +00:00
|
|
|
except LocationInitError:
|
|
|
|
raise commands.UserInputError
|
2018-05-25 16:32:29 +00:00
|
|
|
|
2018-07-14 22:25:33 +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
|
|
|
|
2018-06-23 17:33:02 +00:00
|
|
|
@bot.command(pass_context=True)
|
2018-07-15 18:27:11 +00:00
|
|
|
async def addshop(ctx, name: str, x_pos: int, y_pos: int, z_pos: int, *args):
|
2018-06-23 17:33:02 +00:00
|
|
|
'''
|
2018-06-23 21:51:00 +00:00
|
|
|
Adds a shop to the database.
|
2018-07-15 18:27:11 +00:00
|
|
|
The tunnel address is optional.
|
|
|
|
The default dimension is the overworld. Valid options: overworld, nether, end
|
|
|
|
|
|
|
|
?addbase [Shop Name] [X Coordinate] [Y Coordinate] [Z Coordinate] [Optional Flags]
|
|
|
|
|
|
|
|
Optional Flags:
|
|
|
|
-t [Tunnel Color],[Tunnel Position],[Side]
|
|
|
|
-d [dimension]
|
2018-06-23 17:33:02 +00:00
|
|
|
'''
|
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
flags = get_args_dict(args)
|
|
|
|
tunnel = None
|
|
|
|
dimension = None
|
|
|
|
|
|
|
|
if len(flags) > 0:
|
|
|
|
if '-t' in flags:
|
|
|
|
tunnel = flags['-t']
|
|
|
|
|
|
|
|
if '-d' in flags:
|
|
|
|
dimension = flags['-d']
|
2018-06-23 17:33:02 +00:00
|
|
|
|
|
|
|
try:
|
2018-07-15 18:27:11 +00:00
|
|
|
shop = database_interface.add_shop(ctx.message.author.id, name, x_pos, y_pos, z_pos, tunnel, dimension)
|
2018-06-23 17:33:02 +00:00
|
|
|
except LocationInitError:
|
|
|
|
raise commands.UserInputError
|
|
|
|
|
2018-07-14 22:25:33 +00:00
|
|
|
await bot.say('{}, your shop named **{}** located at {} has been added'
|
2018-07-08 19:19:40 +00:00
|
|
|
' to the database.'.format(ctx.message.author.mention, shop.name, shop.pos_to_str()))
|
2018-06-23 17:33:02 +00:00
|
|
|
|
2018-05-25 16:32:29 +00:00
|
|
|
|
|
|
|
@bot.command(pass_context=True)
|
2018-06-23 17:33:02 +00:00
|
|
|
async def find(ctx, name: str):
|
2018-05-25 21:06:00 +00:00
|
|
|
'''
|
2018-06-23 21:51:00 +00:00
|
|
|
Finds all the locations a player has in the database.
|
|
|
|
?find [Player name]
|
2018-05-25 16:32:29 +00:00
|
|
|
'''
|
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
try:
|
2018-07-08 19:19:40 +00:00
|
|
|
loc_list = database_interface.find_location_by_owner_name(name)
|
2018-06-30 15:07:56 +00:00
|
|
|
loc_string = loc_list_to_string(loc_list, '{} \n{}')
|
2018-05-25 21:51:15 +00:00
|
|
|
|
2018-07-04 00:05:35 +00:00
|
|
|
await bot.say('{}, **{}** has **{}** locations(s): \n {}'.format(ctx.message.author.mention, name, len(loc_list),
|
2018-06-30 15:07:56 +00:00
|
|
|
loc_string))
|
|
|
|
except PlayerNotFound:
|
|
|
|
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-25 21:33:18 +00:00
|
|
|
@bot.command(pass_context=True)
|
2018-06-23 17:33:02 +00:00
|
|
|
async def delete(ctx, name: str):
|
2018-05-25 21:33:18 +00:00
|
|
|
'''
|
2018-06-23 21:51:00 +00:00
|
|
|
Deletes a location from the database.
|
|
|
|
?delete [Location name]
|
2018-05-25 21:33:18 +00:00
|
|
|
'''
|
2018-05-25 21:51:15 +00:00
|
|
|
|
2018-06-23 16:08:40 +00:00
|
|
|
player_name = get_nickname(ctx.message.author)
|
2018-05-29 14:04:01 +00:00
|
|
|
try:
|
2018-07-08 19:19:40 +00:00
|
|
|
database_interface.delete_base(player_name, name)
|
2018-06-30 15:07:56 +00:00
|
|
|
await bot.say('{}, your location named **{}** has been deleted.'.format(ctx.message.author.mention, name))
|
|
|
|
except (DeleteEntryError, PlayerNotFound):
|
|
|
|
await bot.say('{}, you do not have a location named **{}**.'.format(ctx.message.author.mention, name))
|
2018-05-25 21:33:18 +00:00
|
|
|
|
2018-06-02 17:40:55 +00:00
|
|
|
|
2018-05-26 14:26:24 +00:00
|
|
|
@bot.command(pass_context=True)
|
2018-06-23 17:33:02 +00:00
|
|
|
async def findaround(ctx, x_pos: int, z_pos: int, * args):
|
2018-05-26 14:50:14 +00:00
|
|
|
'''
|
2018-07-04 00:05:35 +00:00
|
|
|
Finds all the locations around a certain point that are registered in the database
|
2018-07-15 18:27:11 +00:00
|
|
|
The radius defaults to 200 blocks if no value is given
|
|
|
|
Default dimension is overworld
|
|
|
|
|
|
|
|
?findaround [X Coordinate] [Z Coordinate] [Optional Flags]
|
|
|
|
|
|
|
|
Optional Flags:
|
|
|
|
-r [radius]
|
|
|
|
-d [dimension]
|
2018-05-26 14:50:14 +00:00
|
|
|
'''
|
|
|
|
|
2018-05-26 14:26:24 +00:00
|
|
|
radius = 200
|
2018-07-15 18:27:11 +00:00
|
|
|
dimension = 'Overworld'
|
|
|
|
|
|
|
|
flags = get_args_dict(args)
|
2018-07-15 15:56:56 +00:00
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
if len(flags) > 0:
|
|
|
|
if '-r' in flags:
|
|
|
|
radius = int(flags['-r'])
|
2018-05-26 14:26:24 +00:00
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
if '-d' in flags:
|
|
|
|
dimension = flags['-d']
|
|
|
|
|
|
|
|
base_list = database_interface.find_location_around(x_pos, z_pos, radius, dimension)
|
2018-05-26 14:26:24 +00:00
|
|
|
|
|
|
|
if len(base_list) != 0:
|
2018-06-23 20:27:00 +00:00
|
|
|
base_string = loc_list_to_string(base_list, '{} \n{}')
|
2018-05-26 14:26:24 +00:00
|
|
|
|
2018-07-04 00:05:35 +00:00
|
|
|
await bot.say('{}, there are {} locations(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:
|
2018-07-04 00:05:35 +00:00
|
|
|
await bot.say('{}, there are no locations within {} blocks of that point'
|
2018-06-02 16:11:31 +00:00
|
|
|
.format(ctx.message.author.mention, radius))
|
2018-05-26 14:26:24 +00:00
|
|
|
|
2018-06-23 20:27:00 +00:00
|
|
|
|
|
|
|
@bot.command(pass_context=True)
|
2018-07-04 00:05:35 +00:00
|
|
|
async def additem(ctx, shop_name: str, item_name: str, amount: int, diamond_price: int):
|
2018-06-23 21:51:00 +00:00
|
|
|
'''
|
2018-07-04 00:05:35 +00:00
|
|
|
Adds an item to a shop's inventory. Amount for diamond price.
|
2018-07-15 18:27:11 +00:00
|
|
|
|
|
|
|
?additem [Shop name] [Item Name] [Amount] [Price]
|
2018-06-23 21:51:00 +00:00
|
|
|
'''
|
2018-06-23 20:27:00 +00:00
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
try:
|
|
|
|
player_name = get_nickname(ctx.message.author)
|
2018-07-08 19:19:40 +00:00
|
|
|
database_interface.add_item(ctx.message.author.id, shop_name, item_name, diamond_price, amount)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
await bot.say('{}, **{}** has been added to the inventory of **{}**.'.format(ctx.message.author.mention,
|
|
|
|
item_name, shop_name))
|
|
|
|
except PlayerNotFound:
|
|
|
|
await bot.say('{}, you don\'t have any shops in the database.'.format(ctx.message.author.mention))
|
|
|
|
except LocationLookUpError:
|
|
|
|
await bot.say('{}, you don\'t have any shops named **{}** in the database.'.format(ctx.message.author.mention,
|
|
|
|
shop_name))
|
2018-06-23 20:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bot.command(pass_context=True)
|
|
|
|
async def selling(ctx, item_name: str):
|
2018-06-23 21:51:00 +00:00
|
|
|
'''
|
|
|
|
Lists all the shops selling an item
|
2018-07-15 18:27:11 +00:00
|
|
|
|
|
|
|
?selling [item]
|
2018-06-23 21:51:00 +00:00
|
|
|
'''
|
2018-07-08 19:19:40 +00:00
|
|
|
shop_list = database_interface.find_shop_selling_item(item_name)
|
2018-06-23 20:27:00 +00:00
|
|
|
|
|
|
|
shop_list_str = loc_list_to_string(shop_list)
|
|
|
|
await bot.say('The following shops sell {}: \n {}'.format(item_name, shop_list_str))
|
|
|
|
|
2018-06-23 16:08:40 +00:00
|
|
|
|
2018-07-01 15:10:10 +00:00
|
|
|
@bot.command(pass_context=True)
|
|
|
|
async def shopinfo(ctx, shop_name: str):
|
|
|
|
'''
|
|
|
|
Lists the information and inventory of a shop
|
2018-07-15 18:27:11 +00:00
|
|
|
|
|
|
|
?shopinfo [Shop Name]
|
2018-07-01 15:10:10 +00:00
|
|
|
'''
|
2018-07-08 19:19:40 +00:00
|
|
|
shop = database_interface.find_shop_by_name(shop_name)[0]
|
|
|
|
inv_list = database_interface.get_shop_inventory(shop)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
|
|
|
item_list = ''
|
|
|
|
for item in inv_list:
|
|
|
|
item_list = item_list + '{}\n'.format(item.__str__())
|
|
|
|
|
|
|
|
await bot.say('{} \n Inventory:\n {}'.format(shop.__str__(), item_list))
|
|
|
|
|
|
|
|
# Helper Functions ************************************************************
|
2018-06-23 16:08:40 +00:00
|
|
|
|
2018-07-01 15:10:10 +00:00
|
|
|
def get_nickname(discord_user):
|
2018-06-23 16:08:40 +00:00
|
|
|
if discord_user.nick is None:
|
2018-06-30 15:07:56 +00:00
|
|
|
name = discord_user.display_name
|
2018-06-23 16:08:40 +00:00
|
|
|
else:
|
2018-06-30 15:07:56 +00:00
|
|
|
name = discord_user.nick
|
|
|
|
|
|
|
|
if name == 'dootb.in ꙩ ⃤':
|
|
|
|
name = 'aeskdar'
|
|
|
|
|
|
|
|
return name
|
2018-06-23 16:08:40 +00:00
|
|
|
|
|
|
|
|
2018-06-23 20:27:00 +00:00
|
|
|
def loc_list_to_string(loc_list, str_format='{}\n{}'):
|
|
|
|
loc_string = ''
|
2018-06-23 16:08:40 +00:00
|
|
|
|
2018-06-23 20:27:00 +00:00
|
|
|
for loc in loc_list:
|
|
|
|
loc_string = str_format.format(loc_string, loc)
|
2018-06-23 16:08:40 +00:00
|
|
|
|
2018-06-23 20:27:00 +00:00
|
|
|
return loc_string
|
2018-06-23 16:08:40 +00:00
|
|
|
|
2018-06-30 15:53:27 +00:00
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
def get_args_dict(args):
|
|
|
|
if len(args) != 0:
|
|
|
|
return dict(zip_longest(*[iter(args)] * 2, fillvalue=""))
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2018-06-30 15:53:27 +00:00
|
|
|
def create_config():
|
|
|
|
config['Discord'] = {'Token': ''}
|
|
|
|
config['SQL'] = {'Dialect+Driver': 'test', 'username': '', 'password':'', 'host': '', 'port': '', 'database':''}
|
|
|
|
|
|
|
|
with open('GeoffreyConfig.ini', 'w') as configfile:
|
|
|
|
config.write(configfile)
|
|
|
|
|
|
|
|
def get_engine_arg(config):
|
|
|
|
driver = config['SQL']['Dialect+Driver']
|
|
|
|
username = config['SQL']['username']
|
|
|
|
password = config['SQL']['password']
|
|
|
|
host = config['SQL']['host']
|
|
|
|
port = config['SQL']['port']
|
|
|
|
database_name = config['SQL']['database']
|
|
|
|
|
|
|
|
engine_args = '{}://{}:{}@{}:{}/{}'
|
|
|
|
|
2018-06-30 16:21:37 +00:00
|
|
|
return engine_args.format(driver, username, password, host, port, database_name)
|
2018-06-30 15:53:27 +00:00
|
|
|
|
|
|
|
|
2018-05-25 16:32:29 +00:00
|
|
|
# Bot Startup ******************************************************************
|
2018-06-23 16:08:40 +00:00
|
|
|
|
|
|
|
|
2018-06-30 15:53:27 +00:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('GeoffreyConfig.ini')
|
|
|
|
|
|
|
|
if len(config.sections()) == 0:
|
|
|
|
create_config()
|
|
|
|
print("GeoffreyConfig.ini generated.")
|
|
|
|
quit(0)
|
|
|
|
else:
|
|
|
|
TOKEN = config['Discord']['Token']
|
|
|
|
|
|
|
|
if config['SQL']['dialect+driver'] == 'Test':
|
2018-07-01 15:10:10 +00:00
|
|
|
engine_arg = 'sqlite:///temp.db'
|
2018-06-30 15:53:27 +00:00
|
|
|
else:
|
|
|
|
engine_arg = get_engine_arg(config)
|
|
|
|
|
2018-07-08 19:19:40 +00:00
|
|
|
database_interface = DiscordDatabaseInterface(engine_arg)
|
2018-07-01 15:10:10 +00:00
|
|
|
#WebInterface('127.0.0.1', 8081, database)
|
2018-07-15 15:56:56 +00:00
|
|
|
|
2018-06-30 15:53:27 +00:00
|
|
|
bot.run(TOKEN)
|
2018-05-21 16:57:20 +00:00
|
|
|
|