async def on_ready(): from discord import Game bot_dir = utils.get_dir() for server in bot.servers: server_db = bot_dir + 'db/{}.db'.format(server.id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS ADMINS(ID TEXT)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS IDX_ADMIN_ID ON ADMINS(ID)") cursor.execute( "CREATE TABLE IF NOT EXISTS SETTINGS(NAME TEXT, VALUE TEXT)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS IDX_SETTING_NAME ON SETTINGS(NAME)" ) cursor.close() connection.commit() connection.close() print('Connected to server {}'.format(server.id)) bot_db = utils.get_dir() + 'bot.db' connection = sqlite3.connect(bot_db) cursor = connection.cursor() cursor.execute("SELECT NAME FROM EXTENSIONS") extensions_ = [ext[0] for ext in cursor.fetchall()] for ext in extensions_: try: ext_path = "gally.extensions.{}.ext".format(ext) bot.load_extension(ext_path) print("Extension '{}' loaded".format(ext)) except ClientException: cursor.execute( "DELETE FROM EXTENSIONS WHERE NAME LIKE '{}'".format(ext)) print("Extension '{}' could not be loaded".format(ext)) cursor.close() connection.close() await bot.change_presence(game=Game(name='\\help'))
async def replace_card(self, context, *args): """ Replace all the taboo words of a card. If the card does not exists it is added to the database instead. Usage: \taboo replcard <card_name> <taboo1> <taboo2> <taboo3> <taboo4> """ if len(args) == 0: await self.bot.say("No arguments passed.") elif len(args) < 5: await self.bot.say("You need at least 4 taboo words to add a new card.") else: card = args[0].upper().strip() taboo_ = [word.upper().strip() for word in args[1:]] server_id = context.message.server.id server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute( "REPLACE INTO CARDS(CARD, TABOO) values(?, ?)", (card, '|'.join(taboo_))) await self.bot.say(embed=utils.get_embed( TabooGame.format_card((card, '|'.join(taboo_))), 'Card replaced.' )) cursor.close() connection.commit() connection.close()
async def delete_card(self, context, *args): """ Removes a card from the database. Usage: \taboo delcard <card_name> """ if len(args) < 1: await self.bot.say("No arguments passed.") else: card = args[0].upper().strip() server_id = context.message.server.id server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute("SELECT TABOO FROM CARDS WHERE CARD LIKE '{}'".format(card)) taboo_ = cursor.fetchone() taboo_ = taboo_[0] if taboo_ else None if taboo_ is not None: cursor.execute("DELETE FROM CARDS WHERE CARD LIKE '{}'".format(card)) await self.bot.say(embed=utils.get_embed( TabooGame.format_card((card, taboo_)), 'Card deleted.' )) else: await self.bot.say(embed=utils.get_embed( "The card {} does not exists in the database.".format(card) )) cursor.close() connection.commit() connection.close()
async def add_card(self, context, *args): """ Add a new card to the database. Usage: \taboo addcard <card_name> <taboo1> <taboo2> <taboo3> <taboo4> ... """ if len(args) == 0: await self.bot.say("No arguments passed.") elif len(args) < 5: await self.bot.say("You need at least 4 taboo words to add a new card.") else: card = args[0].upper().strip() taboo_ = [str(word).upper().strip() for word in args[1:]] server_id = context.message.server.id server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute("SELECT CARD FROM CARDS WHERE CARD LIKE '{}'".format(card)) if cursor.fetchone() is not None: await self.bot.say("The card {} already exists in the database".format(card)) else: cursor.execute( "INSERT INTO CARDS VALUES(?, ?)", (card, '|'.join(taboo_))) await self.bot.say(embed=utils.get_embed( TabooGame.format_card((card, '|'.join(taboo_))), "Card added." )) cursor.close() connection.commit() connection.close()
async def add_admin(context): """ Add a bot administrator. Admins only. Usage: \addadmin <@user> """ mentions = context.message.mentions server_id = context.message.server.id if not len(mentions): await bot.say('No arguments passed after the command') return admins = utils.get_admins(server_id) server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() for mention in mentions: if mention.id in admins: bot.say("{} is already an admin.".format(mention.mention)) else: cursor.execute("REPLACE INTO ADMINS(ID) VALUES('{}')".format( mention.id)) await bot.say("{} was added to the admin list".format( mention.mention)) cursor.close() connection.commit() connection.close()
async def remove_admin(context): """ Remove a bot administrator. Admins only. Usage: \removeadmin <@user> """ mentions = context.message.mentions server_id = context.message.server.id if not len(mentions): await bot.say('No arguments passed after the command') return admins = utils.get_admins(server_id) server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() for mention in mentions: if mention.id not in admins: bot.say(embed=utils.get_embed("{} was not an admin.".format( mention.mention))) else: cursor.execute("DELETE FROM ADMINS WHERE ID LIKE '{}'".format( mention.id)) await bot.say(embed=utils.get_embed( "{} was removed from the admin list".format(mention.mention))) cursor.close() connection.commit() connection.close()
async def load_extension(ext_name: str): """ Loads an extension. Bot owner only. """ if not os.path.exists('gally/extensions/{}/ext.py'.format(ext_name)): await bot.say(embed=utils.get_embed( "The extension '{}' could not be loaded `ext.py` does not exists.". format(ext_name))) return if os.path.exists('gally/extensions/{}/requirements.txt'.format(ext_name)): pip.main([ 'install', '-r', 'gally/extensions/{}/requirements.txt'.format(ext_name) ]) ext_path = 'gally.extensions.{}.ext'.format(ext_name) bot_db = utils.get_dir() + 'bot.db' connection = sqlite3.connect(bot_db) connection.execute( "REPLACE INTO EXTENSIONS(NAME) VALUES('{}')".format(ext_name)) connection.commit() connection.close() bot.load_extension(ext_path) await bot.say( embed=utils.get_embed("Extension `{}` loaded".format(ext_name)))
async def list_conf(context): """ List the bot settings. Admins only. Usage: \listconf """ server_db = utils.get_dir() + 'db/{}.db'.format(context.message.server.id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute("SELECT * FROM SETTINGS") settings = cursor.fetchall() message = '```' for setting, value in settings: message += '\n{:<20} - {:<20}'.format(setting, value) message += '\n```' await bot.say(embed=utils.get_embed(message, 'Configuration')) cursor.close() connection.close()
def setup(bot: Bot): for server in bot.servers: server_db = utils.get_dir() + 'db/{}.db'.format(server.id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute( "CREATE TABLE IF NOT EXISTS QUOTES(QUOTE TEXT, AUTHOR TEXT, TIME INT)" ) cursor.close() connection.commit() connection.close() bot.add_cog(Quotes(bot))
def setup(bot: Bot): import json for server in bot.servers: server_db = utils.get_dir() + 'db/{}.db'.format(server.id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute( "INSERT INTO SETTINGS(NAME, VALUE)" "SELECT 'TABOO_CHANNEL', 'NONE'" "WHERE NOT EXISTS(SELECT NAME FROM SETTINGS WHERE NAME LIKE 'TABOO_CHANNEL')" ) cursor.execute( "INSERT INTO SETTINGS(NAME, VALUE)" "SELECT 'TABOO_ROUNDS', '1'" "WHERE NOT EXISTS(SELECT NAME FROM SETTINGS WHERE NAME LIKE 'TABOO_ROUNDS')" ) cursor.execute( "INSERT INTO SETTINGS(NAME, VALUE)" "SELECT 'TABOO_SECONDS', '120'" "WHERE NOT EXISTS(SELECT NAME FROM SETTINGS WHERE NAME LIKE 'TABOO_SECONDS')" ) cursor.execute("CREATE TABLE IF NOT EXISTS CARDS(CARD TEXT, TABOO TEXT)") cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS IDX_TABOO_CARD ON CARDS(CARD)") cursor.execute("SELECT * FROM CARDS") if not cursor.fetchall(): with open('gally/extensions/taboo/taboo_cards.json', 'r') as file_: cards = json.load(file_) for card in cards: cursor.execute("INSERT INTO CARDS VALUES(?, ?)", (card, cards[card])) cursor.close() connection.commit() connection.close() bot.add_cog(Taboo(bot))
async def new(self, context): """ Starts a new game. When used will wait 5 minutes for other people to join. Usage: \taboo new """ server_id = context.message.server.id channel = context.message.channel taboo_game = None if server_id not in self.games else self.games[server_id] if taboo_game is None: server_db = utils.get_dir() + 'db/{}.db'.format(server_id) connection = sqlite3.connect(server_db) cursor = connection.cursor() cursor.execute("SELECT * FROM CARDS") cards = cursor.fetchall() cursor.execute("SELECT VALUE FROM SETTINGS WHERE NAME LIKE 'TABOO_SECONDS'") seconds = cursor.fetchone() cursor.execute("SELECT VALUE FROM SETTINGS WHERE NAME LIKE 'TABOO_ROUNDS'") rounds = cursor.fetchone() cursor.close() connection.close() seconds = int(seconds[0]) if seconds else 120 rounds = int(rounds[0]) if rounds else 1 taboo_game = TabooGame( self.bot, server_id, channel, cards, self.del_game, rounds, seconds ) taboo_game.add_player(context.message.author.id) self.games[server_id] = taboo_game elif taboo_game.playing: await self.bot.say("Can't start a new game when there's one taking place.") else: await self.bot.say("A game was already created and will start soon.")
def main(): parser = argparse.ArgumentParser( description='A simple discord bot for playing taboo.') parser.add_argument('-o', '--owner', help="The bot's owner's id", required=True) parser.add_argument('-t', '--token', help="The bot's token", required=True) args = parser.parse_args() owner_id = args.owner token = args.token if not utils.is_user_id(owner_id): print('Argument passed to OWNER is not a valid user id.') return bot_db = utils.get_dir() + 'bot.db' connection = sqlite3.connect(bot_db) cursor = connection.cursor() cursor.execute( "CREATE TABLE IF NOT EXISTS SETTINGS(NAME TEXT, VALUE TEXT)") cursor.execute("CREATE TABLE IF NOT EXISTS EXTENSIONS(NAME TEXT)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS IDX_SETTING ON SETTINGS(NAME)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS IDX_EXT ON EXTENSIONS(NAME)") cursor.execute("REPLACE INTO SETTINGS VALUES(?, ?)", ('OWNER', owner_id)) cursor.close() connection.commit() connection.close() bot.run(token)
async def unload_extension(ext_name): """ Unloads and extension. Bot owner only. """ if not os.path.exists('gally/extensions/{}/ext.py'.format(ext_name)): await bot.say(embed=utils.get_embed( "The extension '{}' could not be loaded `ext.py` does not exists.". format(ext_name))) return ext_path = 'gally.extensions.{}.ext'.format(ext_name) bot_db = utils.get_dir() + 'bot.db' connection = sqlite3.connect(bot_db) connection.execute( "DELETE FROM EXTENSIONS WHERE NAME LIKE '{}'".format(ext_name)) connection.commit() connection.close() bot.unload_extension(ext_path) await bot.say( embed=utils.get_embed("Extension `{}` unloaded".format(ext_name)))