-
Notifications
You must be signed in to change notification settings - Fork 15
Sourcery Starbot ⭐ refactored iranzo/stampython #29
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,19 +21,18 @@ def init(): | |
| Initializes module | ||
| :return: List of triggers for plugin | ||
| """ | ||
| triggers = ["^/admin"] | ||
| return triggers | ||
| return ["^/admin"] | ||
|
|
||
|
|
||
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
|
Comment on lines
-28
to
+27
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text and stampy.stampy.is_owner_or_admin(message): | ||
| if text.split()[0].lower()[0:6] == "/admin": | ||
| if text.split()[0].lower()[:6] == "/admin": | ||
| admincommands(message) | ||
| return | ||
|
|
||
|
|
@@ -89,8 +88,8 @@ def admincommands(message): | |
| changenmastertoken(message=message) | ||
| elif word == "slave": | ||
| try: | ||
| token = texto.split(' ')[3] | ||
| if token: | ||
| = texto.split(' ')[3]: | ||
| chanlinkslave(message=message, token=token) | ||
| except: | ||
| pass | ||
|
|
@@ -120,8 +119,8 @@ def changenmastertoken(message): | |
| if not stampy.plugin.config.config(key='link-master', default=False, gid=chat_id): | ||
| charset = string.letters + string.digits | ||
| size = 20 | ||
| token = ''.join((random.choice(charset)) for x in range(size)) | ||
| generatedtoken = "%s:%s" % (chat_id, token) | ||
| token = ''.join((random.choice(charset)) for _ in range(size)) | ||
| generatedtoken = f"{chat_id}:{token}" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| stampy.plugin.config.setconfig(key='link-master', gid=chat_id, | ||
| value=generatedtoken) | ||
| logger.debug(msg=_L("Generated token %s for channel %s") % (token, chat_id)) | ||
|
|
@@ -205,21 +204,24 @@ def chanunlink(message): | |
|
|
||
| chat_id = msgdetail["chat_id"] | ||
| message_id = msgdetail["message_id"] | ||
| masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id) | ||
|
|
||
| if masterid: | ||
| # Delete link from slave | ||
| = stampy.plugin.config.config( | ||
| key='link', default=False, gid=chat_id | ||
| ): | ||
| # Delete link from slave | ||
| stampy.plugin.config.deleteconfig(key='link', gid=chat_id) | ||
|
|
||
| # Notify master channel of slave linked | ||
| text = _("Channel *%s* with name *%s* has been unlinked as *SLAVE*") % (chat_id, msgdetail['chat_name']) | ||
| text = _("Channel *%s* with name *%s* has been unlinked as *SLAVE*") % ( | ||
| chat_id, msgdetail['chat_name']) | ||
|
|
||
| stampy.stampy.sendmessage(chat_id=masterid, text=text, | ||
| disable_web_page_preview=True, | ||
| parse_mode="Markdown") | ||
|
|
||
| # Notify slave of master linked | ||
| text = _("This channel has been unliked as *SLAVE* from *MASTER* channel *%s*") % masterid | ||
| text = _( | ||
| "This channel has been unliked as *SLAVE* from *MASTER* channel *%s*") % masterid | ||
| text = text + _("\n\nChannel has also been enabled as running in " | ||
| "isolated mode, use ```/gconfig delete isolated``` to " | ||
| "revert back to global karma") | ||
|
|
@@ -242,14 +244,16 @@ def chanshowslave(message): | |
|
|
||
| chat_id = msgdetail["chat_id"] | ||
| message_id = msgdetail["message_id"] | ||
| masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id) | ||
|
|
||
| if masterid: | ||
| # We've a master channel, report it | ||
| text = _("This channel %s is slave of channel %s") % (chat_id, masterid) | ||
| = stampy.plugin.config.config( | ||
| key='link', default=False, gid=chat_id | ||
| ): | ||
| # We've a master channel, report it | ||
| text = _("This channel %s is slave of channel %s") % ( | ||
| chat_id, masterid) | ||
| else: | ||
| # We nee to check database to see if we've any slave | ||
| sql = "SELECT id from config where key='link' and value='%s'" % chat_id | ||
| sql = f"SELECT id from config where key='link' and value='{chat_id}'" | ||
| cur = stampy.stampy.dbsql(sql=sql) | ||
|
|
||
| text = _("Defined slaves:\n") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,21 +20,20 @@ def init(): | |
| Initializes module | ||
| :return: List of triggers for plugin | ||
| """ | ||
| triggers = ["^/alias"] | ||
| return triggers | ||
| return ["^/alias"] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
|
Comment on lines
-27
to
+26
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| logger = logging.getLogger(__name__) | ||
| logger.debug(msg=_L("Processing plugin: Code: %s") % __file__) | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text: | ||
| if text.split()[0].lower()[0:6] == "/alias": | ||
| = stampy.stampy.getmsgdetail(message)["text"]: | ||
| if text.split()[0].lower()[:6] == "/alias": | ||
| aliascommands(message) | ||
| return | ||
|
|
||
|
|
@@ -125,8 +124,8 @@ def deletealias(word, gid=0): | |
| """ | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| sql = "DELETE FROM alias WHERE key='%s' AND gid='%s';" % (word, gid) | ||
| logger.debug(msg="rmalias: %s for group %s" % (word, gid)) | ||
| sql = f"DELETE FROM alias WHERE key='{word}' AND gid='{gid}';" | ||
| logger.debug(msg=f"rmalias: {word} for group {gid}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| stampy.stampy.dbsql(sql) | ||
| return | ||
|
|
||
|
|
@@ -157,7 +156,7 @@ def listalias(word=False, gid=0): | |
| text = _("%s has an alias %s") % (word, value) | ||
|
|
||
| else: | ||
| sql = "select key,value from alias WHERE gid='%s' ORDER BY key ASC;" % gid | ||
| sql = f"select key,value from alias WHERE gid='{gid}' ORDER BY key ASC;" | ||
|
Comment on lines
-160
to
+159
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| cur = stampy.stampy.dbsql(sql) | ||
| text = _("Defined aliases:\n") | ||
| table = from_db_cursor(cur) | ||
|
|
@@ -177,18 +176,18 @@ def createalias(word, value, gid=0): | |
|
|
||
| logger = logging.getLogger(__name__) | ||
| if getalias(value, gid=gid) == word or word.lower() == value.lower(): | ||
| logger.error(msg=_L("createalias: circular reference %s=%s for gid %s") % (word, value, gid)) | ||
| else: | ||
| if not getalias(word, gid) or getalias(word, gid) == word: | ||
| # Removing duplicates on karma DB and add | ||
| # the previous values | ||
| old = stampy.plugin.karma.getkarma(word=word, gid=gid) | ||
| stampy.plugin.karma.updatekarma(word=word, change=-old, gid=gid) | ||
| stampy.plugin.karma.updatekarma(word=value, change=old, gid=gid) | ||
| sql = "INSERT INTO alias(key, value, gid) VALUES('%s','%s', '%s');" % (word, value, gid) | ||
| logger.debug(msg="createalias: %s=%s for gid %s" % (word, value, gid)) | ||
| stampy.stampy.dbsql(sql) | ||
| return | ||
| logger.error(msg=_L("createalias: circular reference %s=%s for gid %s") % ( | ||
| word, value, gid)) | ||
| elif not getalias(word, gid) or getalias(word, gid) == word: | ||
| # Removing duplicates on karma DB and add | ||
| # the previous values | ||
| old = stampy.plugin.karma.getkarma(word=word, gid=gid) | ||
| stampy.plugin.karma.updatekarma(word=word, change=-old, gid=gid) | ||
| stampy.plugin.karma.updatekarma(word=value, change=old, gid=gid) | ||
| sql = f"INSERT INTO alias(key, value, gid) VALUES('{word}','{value}', '{gid}');" | ||
| logger.debug(msg=f"createalias: {word}={value} for gid {gid}") | ||
| stampy.stampy.dbsql(sql) | ||
| return | ||
| return False | ||
|
|
||
|
|
||
|
|
@@ -205,7 +204,7 @@ def getalias(word, gid=0): | |
| sql = "SELECT key,value FROM alias WHERE key='%s' AND gid='%s';" % string | ||
| cur = stampy.stampy.dbsql(sql) | ||
| value = cur.fetchone() | ||
| logger.debug(msg="getalias: %s for gid %s" % (word, gid)) | ||
| logger.debug(msg=f"getalias: {word} for gid {gid}") | ||
|
Comment on lines
-208
to
+207
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| try: | ||
| # Get value from SQL query | ||
|
|
@@ -218,6 +217,4 @@ def getalias(word, gid=0): | |
| # We can define recursive aliases, so this will return the ultimate one | ||
| if value: | ||
| return getalias(word=value, gid=gid) | ||
| if word: | ||
| return word.lower() | ||
| return False | ||
| return word.lower() if word else False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,16 +28,16 @@ def init(): | |
| return triggers | ||
|
|
||
|
|
||
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| code = None | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text: | ||
| if text.split()[0].lower()[0:6] == "/autok": | ||
| = stampy.stampy.getmsgdetail(message)["text"]: | ||
| if text.split()[0].lower()[:6] == "/autok": | ||
| code = True | ||
| autokcommands(message) | ||
| autokarmawords(message) | ||
|
|
@@ -135,15 +135,11 @@ def getautok(key, gid=0): | |
| """ | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| sql = "SELECT key,value FROM autokarma WHERE key='%s' AND gid='%s';" % (key, gid) | ||
| sql = f"SELECT key,value FROM autokarma WHERE key='{key}' AND gid='{gid}';" | ||
| cur = stampy.stampy.dbsql(sql) | ||
| data = cur.fetchall() | ||
| value = [] | ||
| for row in data: | ||
| # Fill valid values | ||
| value.append(row[1]) | ||
|
|
||
| logger.debug(msg="getautok: %s - %s for gid %s" % (key, value, gid)) | ||
| value = [row[1] for row in data] | ||
| logger.debug(msg=f"getautok: {key} - {value} for gid {gid}") | ||
|
Comment on lines
-138
to
+142
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| return value | ||
|
|
||
|
|
@@ -158,15 +154,11 @@ def getautokeywords(gid=0): | |
| if gid is False: | ||
| sql = "SELECT distinct key FROM autokarma;" | ||
| else: | ||
| sql = "SELECT distinct key FROM autokarma WHERE gid='%s';" % gid | ||
| sql = f"SELECT distinct key FROM autokarma WHERE gid='{gid}';" | ||
| cur = stampy.stampy.dbsql(sql) | ||
| data = cur.fetchall() | ||
| value = [] | ||
| for row in data: | ||
| # Fill valid values | ||
| value.append(row[0]) | ||
|
|
||
| logger.debug(msg="getautokeywords: %s for gid %s" % (value, gid)) | ||
| value = [row[0] for row in data] | ||
| logger.debug(msg=f"getautokeywords: {value} for gid {gid}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| return value | ||
|
|
||
|
|
@@ -184,8 +176,8 @@ def createautok(word, value, gid=0): | |
| if value in getautok(word): | ||
| logger.error(msg=_L("createautok: autok pair %s - %s for gid %s already exists") % (word, value, gid)) | ||
| else: | ||
| sql = "INSERT INTO autokarma(key, value, gid) VALUES('%s','%s', '%s');" % (word, value, gid) | ||
| logger.debug(msg="createautok: %s=%s for gid %s" % (word, value, gid)) | ||
| sql = f"INSERT INTO autokarma(key, value, gid) VALUES('{word}','{value}', '{gid}');" | ||
| logger.debug(msg=f"createautok: {word}={value} for gid {gid}") | ||
|
Comment on lines
-187
to
+180
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| stampy.stampy.dbsql(sql) | ||
| return True | ||
| return False | ||
|
|
@@ -201,8 +193,8 @@ def deleteautok(key, value, gid=0): | |
| """ | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| sql = "DELETE FROM autokarma WHERE key='%s' and value='%s' and gid='%s';" % (key, value, gid) | ||
| logger.debug(msg="rmautok: %s=%s for gid %s" % (key, value, gid)) | ||
| sql = f"DELETE FROM autokarma WHERE key='{key}' and value='{value}' and gid='{gid}';" | ||
| logger.debug(msg=f"rmautok: {key}={value} for gid {gid}") | ||
|
Comment on lines
-204
to
+197
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| stampy.stampy.dbsql(sql) | ||
| return True | ||
|
|
||
|
|
@@ -261,9 +253,7 @@ def autokarmawords(message): | |
| for autok in keywords: | ||
| if autok in text_to_process: | ||
| # If trigger word is there, add the triggered action | ||
| for word in getautok(key=autok, gid=gid): | ||
| wordadd.append(word + "++") | ||
|
|
||
| wordadd.extend(f"{word}++" for word in getautok(key=autok, gid=gid)) | ||
|
Comment on lines
-264
to
+256
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if wordadd: | ||
| # Reduce text in message to just the words we encountered to optimize | ||
| msgdetail["text"] = " ".join(wordadd) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,18 @@ def init(): | |
| Initializes module | ||
| :return: List of triggers for plugin | ||
| """ | ||
| triggers = ["^/quit"] | ||
| return triggers | ||
| return ["^/quit"] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
|
Comment on lines
-24
to
+23
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text and stampy.stampy.is_owner_or_admin(message): | ||
| if text.split()[0].lower()[0:6] == "/quit": | ||
| if text.split()[0].lower()[:6] == "/quit": | ||
| basecommands(message) | ||
| return | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,31 +20,31 @@ def init(): | |
| Initializes module | ||
| :return: List of triggers for plugin | ||
| """ | ||
| triggers = ["^/cn"] | ||
| return triggers | ||
| return ["^/cn"] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def run(message): # do not edit this line | ||
| def run(message): # do not edit this line | ||
| """ | ||
| Executes plugin | ||
| :param message: message to run against | ||
| :return: | ||
| """ | ||
| text = stampy.stampy.getmsgdetail(message)["text"] | ||
| if text: | ||
| = stampy.stampy.getmsgdetail(message)["text"]: | ||
| if text.split()[0].lower() == "/cn": | ||
| cn(message=message) | ||
| return | ||
|
|
||
|
|
||
| def help(message): # do not edit this line | ||
| def help(message): # do not edit this line | ||
| """ | ||
| Returns help for plugin | ||
| :param message: message to process | ||
| :return: help text | ||
| """ | ||
| commandtext = _("Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n") | ||
| return commandtext | ||
| return _( | ||
| "Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n" | ||
| ) | ||
|
Comment on lines
-40
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def cn(message): | ||
|
|
@@ -63,7 +63,7 @@ def cn(message): | |
| message_id = msgdetail["message_id"] | ||
| who_un = msgdetail["who_un"] | ||
|
|
||
| logger.debug(msg=_L("Command: %s by %s" % (texto, who_un))) | ||
| logger.debug(msg=_L(f"Command: {texto} by {who_un}")) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # We might be have been given no command, just stock | ||
| try: | ||
|
|
@@ -74,7 +74,7 @@ def cn(message): | |
| if not command: | ||
| url = "https://api.chucknorris.io/jokes/random" | ||
| else: | ||
| url = "https://api.chucknorris.io/jokes/search?query=%s" % command | ||
| url = f"https://api.chucknorris.io/jokes/search?query={command}" | ||
|
|
||
| text = "``` " | ||
| # we might get more than one result | ||
|
|
@@ -90,10 +90,7 @@ def cn(message): | |
| totalelem = len(result['result']) | ||
| except: | ||
| totalelem = 0 | ||
| if totalelem > 1: | ||
| elem = random.randint(0, totalelem - 1) | ||
| else: | ||
| elem = 0 | ||
| elem = random.randint(0, totalelem - 1) if totalelem > 1 else 0 | ||
| text += result['result'][elem]['value'] | ||
| else: | ||
| text += "Chuck Norris didn't said a word about it." | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
initrefactored with the following changes:inline-immediately-returned-variable)