Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable starboard threshold #25

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
23 changes: 15 additions & 8 deletions tle/cogs/starboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

_STAR = '\N{WHITE MEDIUM STAR}'
_STAR_ORANGE = 0xffaa10
_STAR_THRESHOLD = 5


class StarboardCogError(commands.CommandError):
Expand All @@ -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])
Expand All @@ -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])
Expand Down Expand Up @@ -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:
Expand All @@ -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 <nr> 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')
Expand Down
2 changes: 2 additions & 0 deletions tle/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
26 changes: 19 additions & 7 deletions tle/util/db/user_db_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,'
Expand Down Expand Up @@ -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):
Expand Down