async def prefix_channel(self, ctx, *, txt: str): """Change the Bots Prefix for the current Channel""" channel = ctx.channel for c in ctx.message.channel_mentions: channel = c txt = txt.replace(channel.mention, '').replace('#' + channel.name, '') sql = "INSERT INTO `prefix_channel` (`guild`, `prefix`, `channel`) VALUES (%s, %s, %s)" update_sql = "UPDATE `prefix_channel` SET prefix=%s WHERE guild=%s AND channel=%s" check = "SELECT * FROM `prefix_channel` WHERE guild={0} AND channel={1}" check = check.format(ctx.guild.id, channel.id) q = await self.cursor.execute(check) result = await q.fetchall() if not result: await self.cursor.execute(sql, (ctx.guild.id, txt, channel.id)) await ctx.send( "\N{WHITE HEAVY CHECK MARK} Set bot prefix to \"{0}\" for {1}". format(txt, channel.mention)) else: await self.cursor.execute(update_sql, (txt, ctx.guild.id, channel.id)) await ctx.send( "\N{WHITE HEAVY CHECK MARK} Updated bot prefix to \"{0}\" for {1}" .format(txt, channel.mention)) query_prefix.invalidate(self.bot, ctx.channel.id, channel=True)
async def prefix_reset_all(self, ctx): await self.cursor.execute( "DELETE FROM `prefix_channel` WHERE guild={0}".format(ctx.guild.id) ) await self.cursor.execute( "DELETE FROM `prefix` WHERE guild={0}".format(ctx.guild.id)) query_prefix.invalidate(self.bot, ctx.guild.id) for channel in ctx.guild.channels: query_prefix.invalidate(self.bot, channel.id, channel=True) await ctx.send( "\N{WARNING SIGN} Reset all custom guild prefix settings!")
async def prefix_delete(self, ctx, *, txt: str): """Delete a guild prefix""" sql = 'SELECT id FROM `prefix` WHERE guild=%s AND prefix=%s' q = await self.cursor.execute(sql, (ctx.guild.id, txt)) check = await q.fetchone() if not check: await ctx.send('\N{NO ENTRY} Prefix does not exists.') else: sql = "DELETE FROM `prefix` WHERE id=%s" await self.cursor.execute(sql, (check['id'], )) await ctx.send('\N{WHITE HEAVY CHECK MARK} Removed prefix.') query_prefix.invalidate(self.bot, ctx.guild.id)
async def prefix_reset(self, ctx): """Reset All Custom Set Prefixes For the Bot""" check = "SELECT * FROM `prefix` WHERE guild={0}".format(ctx.guild.id) q = await self.cursor.execute(check) result = await q.fetchall() if not result: await ctx.send( "\N{NO ENTRY} Current guild does **not** have a custom prefix set!" ) else: sql = "DELETE FROM `prefix` WHERE guild={0}".format(ctx.guild.id) await self.cursor.execute(sql) await ctx.send( "\N{HEAVY EXCLAMATION MARK SYMBOL} **Reset guild prefix**\nThis does not reset channel prefixes, run \"all\" after reset to reset all prefixes *or* \"channels\" to reset all custom channel prefixes." ) query_prefix.invalidate(self.bot, ctx.guild.id)
async def prefix(self, ctx, *, txt: str = None): """Change the Bots Prefix for the Guild""" if txt is None: clause = f"WHERE guild={ctx.guild.id}" sql = f"SELECT * FROM `prefix` {clause} AND d" q = await self.cursor.execute(sql) result = await q.fetchone() sql = f"SELECT * FROM `prefix_channel` {clause} AND channel={ctx.channel.id}" q = await self.cursor.execute(sql) result2 = await q.fetchone() if not result: guild_prefix = '.' else: guild_prefix = result['prefix'] if not result2: channel_prefix = None else: channel_prefix = result2['prefix'] msg = "Guild Prefix: `{0}`\n".format(guild_prefix) if channel_prefix != None: msg += "**Current** Channel Prefix: `{0}`".format( channel_prefix) await ctx.send(msg) else: q = await self.cursor.execute( 'SELECT id FROM `prefix` WHERE guild=%s AND prefix=%s', (ctx.guild.id, txt)) if await q.fetchone(): return await ctx.send('\N{NO ENTRY} Prefix already exists.') sql = "SELECT guild FROM `prefix` WHERE guild={0} AND d".format( ctx.guild.id) q = await self.cursor.execute(sql) if not await q.fetchone(): sql = "INSERT INTO `prefix` (`guild`, `prefix`, `d`) VALUES (%s, %s, %s)" await self.cursor.execute(sql, (ctx.guild.id, txt, 1)) await ctx.send( "\N{WHITE HEAVY CHECK MARK} Set bot prefix to \"{0}\" for the guild." .format(txt)) else: update_sql = "UPDATE `prefix` SET prefix=%s WHERE guild=%s" await self.cursor.execute(update_sql, (txt, ctx.guild.id)) await ctx.send( "\N{WHITE HEAVY CHECK MARK} Updated bot prefix to \"{0}\" for the guild." .format(txt)) query_prefix.invalidate(self.bot, ctx.guild.id)
async def prefix_reset_channels(self, ctx): check = "SELECT * FROM `prefix_channel` WHERE guild={0}".format( ctx.guild.id) q = await self.cursor.execute(check) result = await q.fetchall() if not result: await ctx.send( "\N{NO ENTRY} Guild does **not** reset a custom prefix set for any channel!\nMention the channel after \"reset channel\" for a specific channel." ) else: sql = "DELETE FROM `prefix_channel` WHERE guild={0}".format( ctx.guild.id) await self.cursor.execute(sql) await ctx.send( "\N{HEAVY EXCLAMATION MARK SYMBOL} Reset all channels custom prefixes!" ) for channel in ctx.guild.channels: query_prefix.invalidate(self.bot, channel.id, channel=True)
async def prefix_add(self, ctx, *, txt: str): """Add an extra guild prefix for the bot""" if txt == '.' or txt == ctx.guild.me.mention: return await ctx.send('\N{NO ENTRY} Cannot add default prefix.') sql = 'SELECT COUNT(id) FROM `prefix` WHERE guild={0}'.format( ctx.guild.id) q = await self.cursor.execute(sql) if (await q.fetchone())['COUNT(id)'] >= 20: return await ctx.send( '\N{WARNING SIGN} Maximum number of prefixes reached (>= 20).') sql = 'SELECT id FROM `prefix` WHERE guild=%s AND prefix=%s' q = await self.cursor.execute(sql, (ctx.guild.id, txt)) if await q.fetchone(): await ctx.send('\N{NO ENTRY} Prefix already exists.') else: sql = "INSERT INTO `prefix` (`guild`, `prefix`, `d`) VALUES (%s, %s, %s)" await self.cursor.execute(sql, (ctx.guild.id, txt, 0)) await ctx.send('\N{WHITE HEAVY CHECK MARK} Added guild prefix.') query_prefix.invalidate(self.bot, ctx.guild.id)
async def prefix_reset_channel(self, ctx, channel: discord.TextChannel = None): if channel is None: channel = ctx.channel check = "SELECT * FROM `prefix_channel` WHERE guild={0} AND channel={1}".format( ctx.guild.id, channel.id) q = await self.cursor.execute(check) result = await q.fetchall() if not result: await ctx.send( "\N{NO ENTRY} {0} does **not** have a custom prefix Set!\nMention the channel after \"reset channel\" for a specific channel." .format(channel.mention)) else: sql = "DELETE FROM `prefix_channel` WHERE guild={0} AND channel={1}".format( ctx.guild.id, channel.id) await self.cursor.execute(sql) await ctx.send( "\N{HEAVY EXCLAMATION MARK SYMBOL} Reset {0}'s prefix!\nThis does **not** reset all custom channel prefixes, \"reset channels\" to do so." .format(channel.mention)) query_prefix.invalidate(self.bot, ctx.channel.id, channel=True)