diff --git a/tle/cogs/starboard.py b/tle/cogs/starboard.py index 903876be..b3eb548b 100644 --- a/tle/cogs/starboard.py +++ b/tle/cogs/starboard.py @@ -10,7 +10,6 @@ _STAR = '\N{WHITE MEDIUM STAR}' _STAR_ORANGE = 0xffaa10 -_STAR_THRESHOLD = 5 class StarboardCogError(commands.CommandError): @@ -27,7 +26,7 @@ def __init__(self, bot): async def on_raw_reaction_add(self, payload): if str(payload.emoji) != _STAR or payload.guild_id is None: return - res = cf_common.user_db.get_starboard(payload.guild_id) + res = cf_common.user_db.get_starboard_channel(payload.guild_id) if res is None: return starboard_channel_id = int(res[0]) @@ -40,7 +39,7 @@ async def on_raw_reaction_add(self, payload): async def on_raw_message_delete(self, payload): if payload.guild_id is None: return - res = cf_common.user_db.get_starboard(payload.guild_id) + res = cf_common.user_db.get_starboard_channel(payload.guild_id) if res is None: return starboard_channel_id = int(res[0]) @@ -88,7 +87,11 @@ async def check_and_add_to_starboard(self, starboard_channel_id, payload): reaction_count = sum(reaction.count for reaction in message.reactions if str(reaction) == _STAR) - if reaction_count < _STAR_THRESHOLD: + res = cf_common.user_db.get_starboard_threshold(payload.guild_id) + threshold = constants.STARBOARD_THRESHOLD + if res is not None: + threshold = res[0] + if reaction_count < threshold: return lock = self.locks.get(payload.guild_id) if lock is None: @@ -110,13 +113,17 @@ async def starboard(self, ctx): @starboard.command(brief='Set starboard to current channel') @commands.has_role(constants.TLE_ADMIN) - async def here(self, ctx): - """Set the current channel as starboard.""" - res = cf_common.user_db.get_starboard(ctx.guild.id) + async def here(self, ctx, threshold: int = constants.STARBOARD_THRESHOLD): + """Set the current channel as starboard. + - Type ;starboard here to set threshold""" + res = cf_common.user_db.get_starboard_channel(ctx.guild.id) if res is not None: raise StarboardCogError('The starboard channel is already set. Use `clear` before ' 'attempting to set a different channel as starboard.') - cf_common.user_db.set_starboard(ctx.guild.id, ctx.channel.id) + if( threshold < 1 ): + raise StarboardCogError('Star threshold cannot be lower than 1.') + + cf_common.user_db.set_starboard(ctx.guild.id, ctx.channel.id, threshold) await ctx.send(embed=discord_common.embed_success('Starboard channel set')) @starboard.command(brief='Clear starboard settings') diff --git a/tle/constants.py b/tle/constants.py index 6106ef43..b20901ce 100644 --- a/tle/constants.py +++ b/tle/constants.py @@ -25,3 +25,5 @@ TLE_ADMIN = os.environ.get('TLE_ADMIN', 'Admin') TLE_MODERATOR = os.environ.get('TLE_MODERATOR', 'Moderator') + +STARBOARD_THRESHOLD = int(os.environ.get('STARBOARD_THRESHOLD', 5)) diff --git a/tle/util/db/user_db_conn.py b/tle/util/db/user_db_conn.py index 2767c1b2..c78e819c 100644 --- a/tle/util/db/user_db_conn.py +++ b/tle/util/db/user_db_conn.py @@ -176,10 +176,16 @@ def create_tables(self): ''') self.conn.execute( 'CREATE TABLE IF NOT EXISTS starboard (' - 'guild_id TEXT PRIMARY KEY,' - 'channel_id TEXT' + 'guild_id TEXT PRIMARY KEY,' + 'channel_id TEXT,' + 'star_threshold INTEGER NOT NULL' ')' ) + + columns = [i[1] for i in self.conn.execute('PRAGMA table_info(starboard)')] + if 'star_threshold' not in columns: + self.conn.execute('ALTER TABLE starboard ADD COLUMN star_threshold INTEGER NOT NULL') + self.conn.execute( 'CREATE TABLE IF NOT EXISTS starboard_message (' 'original_msg_id TEXT PRIMARY KEY,' @@ -555,17 +561,23 @@ def clear_reminder_settings(self, guild_id): self.conn.execute(query, (guild_id,)) self.conn.commit() - def get_starboard(self, guild_id): + def get_starboard_channel(self, guild_id): query = ('SELECT channel_id ' 'FROM starboard ' 'WHERE guild_id = ?') return self.conn.execute(query, (guild_id,)).fetchone() + + def get_starboard_threshold(self, guild_id): + query = ('SELECT star_threshold ' + 'FROM starboard ' + 'WHERE guild_id = ?') + return self.conn.execute(query, (guild_id,)).fetchone() - def set_starboard(self, guild_id, channel_id): + def set_starboard(self, guild_id, channel_id, star_threshold): query = ('INSERT OR REPLACE INTO starboard ' - '(guild_id, channel_id) ' - 'VALUES (?, ?)') - self.conn.execute(query, (guild_id, channel_id)) + '(guild_id, channel_id, star_threshold) ' + 'VALUES (?, ?, ?)') + self.conn.execute(query, (guild_id, channel_id, star_threshold)) self.conn.commit() def clear_starboard(self, guild_id):