async def reload_all(ctx): for cog in cogs: try: bot.unload_extension(cog) except commands.ExtensionNotLoaded as e: myrefeldebug.DebugLog(f'Cog {cog} not loaded') bot.load_extension(cog) myrefeldebug.DebugLog(f'Cog {cog} reloaded') await ctx.send(f'Cog {cog} reloaded')
async def reload_cog(self, ctx, *args): if args: argstr = ''.join(args) try: self.bot.unload_extension('cogs.' + argstr) except commands.ExtensionError as e: myrefeldebug.DebugLog(f'Cog {argstr} not loaded') self.bot.load_extension('cogs.' + argstr) await ctx.send(f'Cog {argstr} reloaded') myrefeldebug.DebugLog(f'Cog cogs.{argstr} reloaded') else: await ctx.send('No cog specified') myrefeldebug.DebugLog('No cog specified')
async def giveitem(self, ctx, *args): # Gives an item to a player if len(ctx.message.mentions) != 1: await ctx.send('You must mention exactly one user to give the item to.') return if myrefeldb.GetPlayerData(self, ctx.message.mentions[0].id) != None: if not args: await ctx.send('You must specify an item id.') return itemId = int(args[0]) countToAdd = 1 if len(args) > 2: countToAdd = int(args[1]) if countToAdd < 1: await ctx.send('Can\'t give less than one item!') return itemName = self.bot.database.execute(f'SELECT name FROM items WHERE Id = {itemId};').fetchall() target = ctx.message.mentions[0] if len(itemName) > 0: itemName = itemName[0][0] # Give the item itself itemCount = self.bot.database.execute(f'SELECT count FROM inventory WHERE CharId = {target.id} AND ItemId = {itemId};').fetchall() if len(itemCount) > 0: self.bot.database.execute(f'UPDATE inventory SET count = \'{itemCount[0][0] + countToAdd}\' WHERE CharId = {target.id} AND ItemId = {itemId};') else: self.bot.database.execute(f'INSERT INTO inventory (CharId, ItemId, Count) \ VALUES ({target.id}, {itemId}, {countToAdd});') self.bot.database.commit() await ctx.send(f'Gave {target.mention} {countToAdd} of {itemName}') myrefeldebug.DebugLog(f'{ctx.author} gave {target} {countToAdd} of {itemName}') else: await ctx.send(f'Invalid item id!') else: await ctx.send('The target is not registered!')
async def on_ready(): logFile = open('log', 'w') logFile.close() myrefeldebug.DebugLog(welcomeMessages[random.randint(0, len(welcomeMessages) - 1)]) for cog in cogs: bot.load_extension(cog) status_cycle.start() bot.database = sqlite3.connect('data/myrefel.sqlite') myrefeldb.InitDB(bot.database)
async def register(self, ctx): # Initialises a user's information if myrefeldb.GetPlayerData(self, ctx.author.id) == None: self.bot.database.execute( f'INSERT INTO chars (Id, Name) VALUES ({ctx.author.id}, \'{ctx.message.author.name}\');' ) self.bot.database.commit() await ctx.send('Welcome to Myrefel!') myrefeldebug.DebugLog(f'{ctx.author} registered') else: await ctx.send('You have already registered!')
async def rename(self, ctx, *args): # Changes a player's name if myrefeldb.GetPlayerData(self, ctx.author.id) != None: if not args: await ctx.send('You must specify a name.') return self.bot.database.execute( f'UPDATE chars SET Name = \'{args[0]}\' WHERE Id = {ctx.author.id};' ) self.bot.database.commit() await ctx.send(f'Updated your name to {args[0]}') myrefeldebug.DebugLog( f'{ctx.author} updated their name to {args[0]}') else: await ctx.send('You are not registered!')
async def hug(self, ctx): if len(ctx.message.mentions) > 0: # Get the name of the person being hugged secondPlayerData = myrefeldb.GetPlayerData( self, ctx.message.mentions[0].id) if secondPlayerData != None: secondPlayerName = secondPlayerData[1] else: secondPlayerName = ctx.message.mentions[0].name # Get the name of the person hugging firstPlayerData = myrefeldb.GetPlayerData(self, ctx.author.id) if firstPlayerData != None: firstPlayerName = firstPlayerData[1] else: firstPlayerName = ctx.author.name # Check if it's the same person if firstPlayerData != None and secondPlayerData != None and firstPlayerData[ 0] == secondPlayerData[0]: await ctx.send(f'{firstPlayerName} hugged themself') else: # Check if the huggees are in the same room if firstPlayerData != None and secondPlayerData != None and firstPlayerData[ 2] != secondPlayerData[2]: await ctx.send( f'You can only hug people in the same room!\n(and unregistered people)' ) return # Increment their hug counters if firstPlayerData != None: self.bot.database.execute( f'UPDATE chars SET Hugs = {firstPlayerData[3] + 1} WHERE Id = {firstPlayerData[0]};' ) if secondPlayerData != None: self.bot.database.execute( f'UPDATE chars SET Hugs = {secondPlayerData[3] + 1} WHERE Id = {secondPlayerData[0]};' ) # "Display" the hug await ctx.send(f'{firstPlayerName} hugged {secondPlayerName}') myrefeldebug.DebugLog( f'{firstPlayerName} hugged {secondPlayerName}') else: await ctx.send('You need to mention someone to hug them!')
async def drop(self, ctx, *args): # Drops items target = ctx.author playerData = myrefeldb.GetPlayerData(self, target.id) if playerData != None: if not args: await ctx.send( 'You must specify an item within your inventory!') return itemPosition = int(args[0]) - 1 countToDrop = 1 if len(args) > 1: countToDrop = int(args[1]) if countToDrop < 1: await ctx.send('Can\'t drop less than one item!') return inventory = self.bot.database.execute( f'SELECT ItemId, Count FROM inventory WHERE CharId = {target.id};' ).fetchall() if itemPosition >= len(inventory) or itemPosition < 0: await ctx.send( 'You must specify an item within your inventory!') return itemData = self.bot.database.execute( f'SELECT Name FROM items WHERE Id = {inventory[itemPosition][0]};' ).fetchall()[0] if inventory[itemPosition][1] <= countToDrop: self.bot.database.execute( f'DELETE FROM inventory WHERE CharId = {target.id} AND ItemId = {inventory[itemPosition][0]};' ) countToDrop = inventory[itemPosition][1] else: self.bot.database.execute( f'UPDATE inventory SET count = \'{inventory[itemPosition][1] - countToDrop}\' WHERE CharId = {target.id} AND ItemId = {inventory[itemPosition][0]};' ) await ctx.send(f'Dropped {countToDrop} of {itemData[0]}!') myrefeldebug.DebugLog( f'{target} dropped {countToDrop} of {itemData[0]}') else: await ctx.send(f'You are not registered!')
async def teleport(self, ctx, *args): # Teleports a dev to a room by id if myrefeldb.GetPlayerData(self, ctx.author.id) != None: if not args: await ctx.send('You must specify a room id.') return roomId = int(args[0]) roomName = self.bot.database.execute(f'SELECT name FROM rooms WHERE Id = {roomId};').fetchall() if len(roomName) > 0: roomName = roomName[0][0] # The teleport itself self.bot.database.execute(f'UPDATE chars SET Room = \'{roomId}\' WHERE Id = {ctx.author.id};') self.bot.database.commit() await ctx.send(f'Teleported you to {roomName}') myrefeldebug.DebugLog(f'{ctx.author} teleported to room {roomName}') else: await ctx.send(f'Invalid room id!') else: await ctx.send('You are not registered!')
async def move(self, ctx, *args): playerData = myrefeldb.GetPlayerData(self, ctx.author.id) if playerData != None: connections = self.bot.database.execute( f'SELECT * from roomconns WHERE FirstRoom = {playerData[2]};' ).fetchall() message = '' if not args: # Show the places the player can go if len(connections) > 0: message = 'Available locations:\n' for i in range(0, len(connections)): message += str( i + 1 ) + ': ' + self.bot.database.execute( f'SELECT Name FROM rooms WHERE Id = {connections[i][1]};' ).fetchall()[0][0] + '\n' else: message = 'No available locations' else: # Go to a connected location if int(args[0]) - 1 >= 0 and int( args[0]) - 1 < len(connections): destination = connections[int(args[0]) - 1][1] destinationName = self.bot.database.execute( f'SELECT Name FROM rooms WHERE Id = {destination};' ).fetchall()[0][0] message = 'Moved to ' + destinationName self.bot.database.execute( f'UPDATE chars SET Room = \'{destination}\' WHERE Id = {ctx.message.author.id};' ) self.bot.database.commit() myrefeldebug.DebugLog( f'{ctx.author} moved to {destinationName}') else: message = 'Invalid destination!' await ctx.send(message) else: await ctx.send('You are not registered!')
async def moveother(self, ctx, *args): # Teleports another player if len(ctx.message.mentions) != 1: await ctx.send('You must mention exactly one user to teleport.') return if myrefeldb.GetPlayerData(self, ctx.message.mentions[0].id) != None: if not args: await ctx.send('You must specify a room id.') return roomId = int(args[0]) roomName = self.bot.database.execute(f'SELECT name FROM rooms WHERE Id = {roomId};').fetchall() target = ctx.message.mentions[0] if len(roomName) > 0: roomName = roomName[0][0] # The teleport itself self.bot.database.execute(f'UPDATE chars SET Room = \'{roomId}\' WHERE Id = {target.id};') self.bot.database.commit() await ctx.send(f'Teleported {target.mention} to {roomName}') myrefeldebug.DebugLog(f'{ctx.author} teleported {target} to room {roomName}') else: await ctx.send(f'Invalid room id!') else: await ctx.send('The target is not registered!')
async def reload_all_error(ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.message.author} tried to reload all cogs')
async def giveitem_error(self, ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.author} tried to give an item to a player')
def InitDB(database): worldVersion = database.execute('SELECT Version FROM world WHERE Id = 0').fetchall() if len(worldVersion) <= 0: # Initialise the database myrefeldebug.DebugLog('Initialised database') database.execute('CREATE TABLE rooms (Id INT NOT NULL, \ Description VARCHAR(2048) NOT NULL DEFAULT \'\', \ Name VARCHAR(255) NOT NULL DEFAULT \'\', \ PRIMARY KEY (Id));') database.execute('CREATE TABLE chars (Id INT NOT NULL, \ Name VARCHAR(255) NOT NULL DEFAULT \'\', \ Room INT NOT NULL DEFAULT 0, \ Hugs INT NOT NULL DEFAULT 0, \ PRIMARY KEY (Id), \ CONSTRAINT Room FOREIGN KEY (Room) REFERENCES rooms (Id));') database.execute('CREATE TABLE world (Id INT NOT NULL, \ Version INT NOT NULL DEFAULT 0, \ PRIMARY KEY (Id));') database.execute('CREATE TABLE roomconns (FirstRoom INT NOT NULL DEFAULT 0, \ SecondRoom INT NOT NULL DEFAULT 0, \ CONSTRAINT FirstRoom FOREIGN KEY (FirstRoom) REFERENCES rooms(Id), \ CONSTRAINT SecondRoom FOREIGN KEY (SecondRoom) REFERENCES rooms(Id));') database.execute('CREATE TABLE items (Id INT NOT NULL, \ Name VARCHAR(255) NOT NULL DEFAULT \'\', \ Description VARCHAR(255) NOT NULL DEFAULT \'\');') database.execute('CREATE TABLE inventory (CharId INT NOT NULL DEFAULT 0, \ ItemId INT NOT NULL DEFAULT 0, \ Count INT NOT NULL DEFAULT 0, \ CONSTRAINT CharId FOREIGN KEY (CharId) REFERENCES chars (Id), \ CONSTRAINT ItemId FOREIGN KEY (ItemId) REFERENCES items (Id));') database.execute('INSERT INTO world (Id, Version) VALUES (0, 1010);') AddRooms(database) AddRoomConnections(database) AddItems(database) else: # Update from 1000 to 1001 - adding room descriptions, added tavern room if worldVersion[0][0] == 1000: myrefeldebug.DebugLog('Updated database to 1001') database.execute('ALTER TABLE rooms \ ADD COLUMN Description VARCHAR(2048) NOT NULL DEFAULT \'\';') database.execute('UPDATE world SET Version = 1001 WHERE Id = 0;') database.execute('INSERT INTO rooms (Id, Description) VALUES (0, \'A tavern with no distinguishing features.\');') # Update from 1001 to 1002 - adding names to rooms if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1001: myrefeldebug.DebugLog('Updated database to 1002') database.execute('ALTER TABLE rooms \ ADD COLUMN Name VARCHAR(255) NOT NULL DEFAULT \'\';') database.execute('UPDATE rooms SET Name = \'The Tavern\' WHERE Id = 0;') database.execute('UPDATE world SET Version = 1002 WHERE Id = 0;') # Update from 1002 to 1003 - adding two more rooms if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1002: myrefeldebug.DebugLog('Updated database to 1003') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (1, \'A room outside of reality.\', "Erelyn\'s Room");') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (2, \'The storeroom behind the bar of the tavern.\', \'Tavern Storeroom\');') database.execute('UPDATE world SET Version = 1003 WHERE Id = 0;') # Update from 1003 to 1004 - adding room connections, adding the pit if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1003: myrefeldebug.DebugLog('Updated database to 1004') database.execute('CREATE TABLE roomconns (Id INT NOT NULL, \ FirstRoom INT NOT NULL DEFAULT 0, \ SecondRoom INT NOT NULL DEFAULT 0, \ PRIMARY KEY (Id), \ CONSTRAINT FirstRoom FOREIGN KEY (FirstRoom) REFERENCES rooms(Id), \ CONSTRAINT SecondRoom FOREIGN KEY (SecondRoom) REFERENCES rooms(Id));') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (3, \'A dark, dank pit.\', \'The Pit\');') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (0, 0, 2);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (1, 2, 0);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (2, 2, 3);') database.execute('UPDATE world SET Version = 1004 WHERE Id = 0;') # Update from 1004 to 1005 - adding the flame altar if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1004: myrefeldebug.DebugLog('Updated database to 1005') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (4, \'A corridor lit by purple flames.\', \'Corridor\');') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (5, \'An altar of flame.\nPurple lights dance through the air.\', \'Altar\');') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (3, 3, 4);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (4, 4, 3);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (5, 4, 5);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (6, 5, 4);') database.execute('UPDATE world SET Version = 1005 WHERE Id = 0;') # Update from 1005 to 1006 - adding the flame if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1005: myrefeldebug.DebugLog('Updated database to 1006') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (6, \'Purple flame surrounding you, enveloping your entire being.\', \'Flame\');') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (7, 5, 6);') database.execute('INSERT INTO roomconns (Id, FirstRoom, SecondRoom) \ VALUES (8, 6, 0);') database.execute('UPDATE world SET Version = 1006 WHERE Id = 0;') # Update from 1006 to 1007 - hug counter if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1006: myrefeldebug.DebugLog('Updated database to 1007') database.execute('ALTER TABLE chars \ ADD COLUMN Hugs INT NOT NULL DEFAULT 0;') database.execute('UPDATE world SET Version = 1007 WHERE Id = 0;') # Update from 1007 to 1008 - removing unnecessary column from room connection table if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1007: myrefeldebug.DebugLog('Updated database to 1008') database.execute('ALTER TABLE roomconns \ RENAME TO roomconns_old;') database.execute('CREATE TABLE roomconns (FirstRoom INT NOT NULL DEFAULT 0, \ SecondRoom INT NOT NULL DEFAULT 0, \ CONSTRAINT FirstRoom FOREIGN KEY (FirstRoom) REFERENCES rooms(Id), \ CONSTRAINT SecondRoom FOREIGN KEY (SecondRoom) REFERENCES rooms(Id));') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ SELECT FirstRoom, SecondRoom FROM roomconns_old;') database.execute('DROP TABLE roomconns_old;') database.execute('UPDATE world SET Version = 1008 WHERE Id = 0;') # Update from 1008 to 1009 - adding new rooms for the village if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1008: myrefeldebug.DebugLog('Updated database to 1009') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (7, \'A small village.\', \'The Village\');') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (8, \'The blacksmith of the town.\', \'Blacksmith\');') database.execute('INSERT INTO rooms (Id, Description, Name) \ VALUES (9, \'A store selling all manner of goods.\', \'General Store\');') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (0, 7);') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (7, 0);') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (7, 8);') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (8, 7);') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (7, 9);') database.execute('INSERT INTO roomconns (FirstRoom, SecondRoom) \ VALUES (9, 7);') database.execute('UPDATE world SET Version = 1009 WHERE Id = 0;') # Update from 1009 to 1010 - adding items if database.execute('SELECT Version FROM world WHERE Id = 0').fetchall()[0][0] == 1009: database.execute('CREATE TABLE items (Id INT NOT NULL, \ Name VARCHAR(255) NOT NULL DEFAULT \'\', \ Description VARCHAR(255) NOT NULL DEFAULT \'\');') database.execute('CREATE TABLE inventory (CharId INT NOT NULL DEFAULT 0, \ ItemId INT NOT NULL DEFAULT 0, \ Count INT NOT NULL DEFAULT 0, \ CONSTRAINT CharId FOREIGN KEY (CharId) REFERENCES chars (Id), \ CONSTRAINT ItemId FOREIGN KEY (ItemId) REFERENCES items (Id));') database.execute('INSERT INTO items (Id, Name, Description) \ VALUES (0, \'Test Item\', \'A test item\');') database.execute('UPDATE world SET Version = 1010 WHERE Id = 0;') database.commit()
async def teleport_error(self, ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.author} tried to teleport')
async def reload_db(self, ctx): # Loads any changes to the database that have been made since the bot started importlib.reload(myrefeldb) myrefeldb.InitDB(self.bot.database) myrefeldebug.DebugLog('Loaded database changes') await ctx.send('Loaded database changes')
async def reload_cog_error(self, ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.author} tried to reload a cog')
async def reload_db_error(self, ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.author} tried to load database changes')
async def moveother_error(self, ctx, error): myrefeldebug.DebugLog(str(error)) if isinstance(error, commands.NotOwner): myrefeldebug.DebugLog(f'{ctx.author} tried to move another user')