async def echo_handler(reader, writer): data = await reader.readline() message = data.decode() addr = writer.get_extra_info('peername') msg = "Received %r from %r" % (message, addr) print(msg) table = message.replace("\n", "").split(".|.") for i in range(len(table)): table[i] = table[i].split("-|-") table_request = up.lookup(table, "table_request") if table_request == "new": result = await newTable(table) elif table_request == "update": result = await updateTable(table) elif table_request == "delete": result = await deleteTable(table) result = result.encode() print("Send: %r" % result) writer.write(result) await writer.drain() print("Close the client socket") writer.close()
async def deleteTable(newtable): # get the game id from the received id object table_id = up.lookup(newtable, "table_id") channel = bot.get_channel(cred.statuschan) # check if the game with the given id already exists in the database if data.Games().getGame(table_id=table_id) is not None: old = data.Games().deleteGame(newtable) if old is not None: # create a new game room with all the bells and whistles ct = util_discord.gameTable(cred.server1) await ct.deleteTable(old, newtable) await channel.send("Game **" + ct.old_cat_name + "** has been deleted!") return "GameDeleted" # if it already exists, return an error code else: await channel.send("Game with the id **" + table_id + "** does not exist!") return "GameNotFound" return
async def newTable(table): # get the game id from the received id object table_id = up.lookup(table, "table_id") channel = bot.get_channel(cred.statuschan) # check if the game with the given id already exists in the database if data.Games().getGame(table_id=table_id) == None: data.Games().newGame(table) # create a new game room with all the bells and whistles ct = util_discord.gameTable(cred.server1) await ct.createTable(table) await channel.send("New game **" + ct.table_title + "** has been created!") return "GameCreated" # if it already exists, return an error code else: await channel.send("Game with the id **" + table_id + "** already exists!") return "AlreadyExists" return
def deleteGame(self, data): print("deleting") conn, cursor = self.db_conn() # store the table id table_id = up.lookup(data, "table_id") # try to find the game in the table cursor.execute('''SELECT * from fee_games WHERE table_id=?''', (table_id, )) result = cursor.fetchone() newresult = None # store the fetched results in an array if result is not None: newresult = [["table_id", result[0]], ["table_title", result[1]], ["table_system", result[2]], ["table_lead", result[3]], ["table_teaser", result[4]], ["table_start", result[5]], ["table_duration", result[6]]] cursor.execute('''DELETE FROM fee_games WHERE table_id=?''', (table_id, )) conn.commit() #close the connection conn.close() return newresult
async def updateTable(self, old, table): # get all the values from the passed table self.table_id = up.lookup(table, "table_id") self.table_title = up.lookup(table, "table_title") self.table_system = up.lookup(table, "table_system") self.table_lead = up.lookup(table, "table_lead") self.table_teaser = ut.cleanHTML(up.lookup(table, "table_teaser")) self.table_start = up.lookup(table, "table_start") self.table_duration = up.lookup(table, "table_duration") # get the old category name from the fetched table list and convert it into the discord standard notation self.old_table_title = up.lookup(old, "table_title") self.old_table_id = up.lookup(old, "table_id") if len(self.old_table_title) > 22: self.old_cat_name = self.old_table_title[:22] + ".." + "-" + str( self.old_table_id) else: self.old_cat_name = self.old_table_title[:22] + "-" + str( self.old_table_id) # create a string to be used as a name for the table id channel self.table_start_time = datetime.fromtimestamp(int(self.table_start)) self.table_start_text = str(self.table_start_time.hour).zfill(2) + str( self.table_start_time.minute).zfill(2) + "-uhr" self.info = "__Start " + self.table_start_text if len(self.table_title) > 22: self.category_name = self.table_title[:22] + ".." + "-" + str( self.table_id) else: self.category_name = self.table_title[:22] + "-" + str( self.table_id) # get the category as an object self.category = discord.utils.get(self.server.categories, name=self.old_cat_name) if self.category is not None: await self.category.edit(name=self.category_name) else: return "NOTFOUND" # get the status channel as an update await self.infoMessage(self.category) return
async def deleteTable(self, old, table): # get the old category name from the fetched table list and convert it into the discord standard notation self.old_table_title = up.lookup(old, "table_title") self.old_table_id = up.lookup(old, "table_id") if len(self.old_table_title) > 22: self.old_cat_name = self.old_table_title[:22] + ".." + "-" + str( self.old_table_id) else: self.old_cat_name = self.old_table_title[:22] + "-" + str( self.old_table_id) # get the category as an object self.category = discord.utils.get(self.server.categories, name=self.old_cat_name) if self.category is not None: for channel in self.category.channels: await channel.delete() await self.category.delete() else: for category in self.server.categories: if category.name.endswith("-" + str(self.old_table_id)): for channel in category.channels: await channel.delete() await category.delete() return
def updateGame(self, data): # create a connection and a cursor conn, cursor = self.db_conn() # store the table id table_id = up.lookup(data, "table_id") # try to find the game in the table and store the game cursor.execute('''SELECT * from fee_games WHERE table_id=?''', (table_id, )) result = cursor.fetchone() # create a variable for the fetched results newresult = None # store the fetched results in an array if result is not None: newresult = [["table_id", result[0]], ["table_title", result[1]], ["table_system", result[2]], ["table_lead", result[3]], ["table_teaser", result[4]], ["table_start", int(result[5])], ["table_duration", int(result[6])]] values = (up.lookup(data, "table_title"), up.lookup(data, "table_system"), up.lookup(data, "table_lead"), ut.cleanHTML(up.lookup(data, "table_teaser")), int(up.lookup(data, "table_start")), int(up.lookup(data, "table_duration")), int(up.lookup(data, "table_id"))) sql = '''UPDATE fee_games set table_title=?, table_system=?, table_lead=?, table_teaser=?, table_start=?, table_duration=? WHERE table_id =?''' cursor.execute(sql, values) conn.commit() #close the connection conn.close() return newresult
def newGame(self, data): conn, cursor = self.db_conn() values = (int(up.lookup(data, "table_id")), up.lookup(data, "table_title"), up.lookup(data, "table_system"), up.lookup(data, "table_lead"), ut.cleanHTML(up.lookup(data, "table_teaser")), int(up.lookup(data, "table_start")), int(up.lookup(data, "table_duration"))) sql = '''INSERT INTO fee_games(table_id, table_title, table_system, table_lead, table_teaser, table_start, table_duration) VALUES (?,?,?,?,?,?,?)''' cursor.execute(sql, values) conn.commit() # close the connection conn.close() return
async def createTable(self, table): # get all the values from the passed table self.table_id = up.lookup(table, "table_id") self.table_title = up.lookup(table, "table_title") self.table_system = up.lookup(table, "table_system") self.table_lead = up.lookup(table, "table_lead") self.table_teaser = ut.cleanHTML(up.lookup(table, "table_teaser")) self.table_start = up.lookup(table, "table_start") self.table_duration = up.lookup(table, "table_duration") # create a string to be used as a name for the table id channel self.table_start_time = datetime.fromtimestamp(int(self.table_start)) self.table_start_text = str(self.table_start_time.hour).zfill(2) + str( self.table_start_time.minute).zfill(2) + "-uhr" self.info = "__Start " + self.table_start_text # create the category name and shorten it if needed (longer than 22 chars) if len(self.table_title) > 22: self.category_name = self.table_title[:22] + ".." + "-" + str( self.table_id) else: self.category_name = self.table_title[:22] + "-" + str( self.table_id) # create a category for the game name await self.server.create_category_channel(name=self.category_name) # get the category as an object self.category = discord.utils.get(self.server.categories, name=self.category_name) self.role = discord.utils.get(self.server.roles, id=712381285867323414) # set the correct permissions for the category await self.category.set_permissions(self.server.default_role, read_messages=False, send_messages=False) await self.category.set_permissions(self.role, read_messages=True, send_messages=True) # create new text and voice channels in the category overwrites = { self.server.default_role: discord.PermissionOverwrite(read_messages=False, send_messages=False), self.role: discord.PermissionOverwrite(send_messages=False, read_messages=True) } await self.category.create_text_channel(name=self.info, overwrites=overwrites) await self.category.create_text_channel(name="spieltisch") await self.category.create_voice_channel(name="spieltisch") await self.category.create_voice_channel(name="stille kammer") # get the status channel as an update await self.infoMessage(self.category) return