Skip to content

Commit

Permalink
Merge pull request #4 from Fittiboy/tags_cleanup
Browse files Browse the repository at this point in the history
Clean up the representation of e.tags to a more reasonable format. Makes handling easier.

Resolves #3
  • Loading branch information
Fittiboy authored Aug 1, 2020
2 parents 05ed301 + e83cc94 commit 9c307c7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[bumpversion]
current_version = 1.3.16
current_version = 1.4.1

[bumpversion:file:twitchat/__init__.py]
Binary file added dist/twitchat-1.4.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/twitchat-1.4.0.tar.gz
Binary file not shown.
Binary file added dist/twitchat-1.4.1-py3-none-any.whl
Binary file not shown.
Binary file added dist/twitchat-1.4.1.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion twitchat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""A simple Twitch chat bot that gives you the option to run complicated commands, to an extent that other bots don't allow."""
# __init__.py

__version__ = "1.3.16"
__version__ = "1.4.1"
15 changes: 3 additions & 12 deletions twitchat/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,12 @@ def check_permissions(func):
"""Use this decorator to add a permission check to a command"""
@functools.wraps(func)
def permissions_wrapper(*args, **kwargs):
e = args[1]
if len(args) == 4:
args = list(args)
args.insert(0, args[0].arguments[0])
uid = [dict['value'] for dict in args[1].tags
if dict['key'] == 'user-id'][0]
badges_tag = [dict['value'] for dict in args[1].tags
if dict['key'] == 'badges']
badges_list = []
if badges_tag[0]:
badges_list = badges_tag[0].split(",")
badges_lists_list = [badge.split("/") for badge in badges_list
if badge]

badges = {badge_list[0]: badge_list[1] for badge_list
in badges_lists_list}
uid = e.tags['user-id']
badges = e.tags['badges']

with open('permissions.json') as perms_file:
perms = json.load(perms_file)
Expand Down
78 changes: 50 additions & 28 deletions twitchat/twitchbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,51 +45,73 @@ def on_welcome(self, c, e):
c.privmsg(self.channel, "I am here now :)")
self.reconnect = 1

def on_reconnect(self, c, e):
time.sleep(self.reconnect)
self.reconnect *= 2
c.reconnect()

def on_disconnect(self, c, e):
time.sleep(self.reconnect)
self.reconnect *= 2
c.reconnect()
"""Attempts to reconnect, with increasing time waited in between
attempts.
"""

def on_dccdisconnect(self, c, e):
time.sleep(self.reconnect)
self.reconnect *= 2
c.reconnect()

def on_pubmsg(self, c, e):
"""Tries to execute command if message begins with '!'"""
"""Tries to execute command if message begins with '!'. Also handles
the posting of timed messages and reformats some of the input.
A special case is handled in which the !reload command is called. This
is only allowed for the broadcaster and has to be executed outside of
commands.py. The !reload commands reloads the commands.py module.
Additionally, the unnatural format of e.tags is turned
into a more reasonable dictionary, also containing a dictionary
representation of the badges.
"""

if e.arguments[0][0] == "!":
global cmd_obj
# e.tags comes as a list of dictionaries of the form
# {'key': key, 'value': value}. This line changes that
# to a single dictionary of the more reasonable form {key: value}.
e.tags = {dct['key']: dct['value'] for dct in e.tags}
badges_tag = e.tags.get('badges')
badges_list = []
# The badges come as a string with the format
# 'badge/version,badge/version,badge/version...'.
#
# This code converts the string into a more natural
# dictionary of the form {badge: version, ...}.
if badges_tag:
badges_list = badges_tag.split(",")
badges_list_collection = [badge.split("/") for badge
in badges_list if badge]

badges = {badge_list[0]: badge_list[1] for badge_list
in badges_list_collection}
# Update the badges tag for later use in commands.py
e.tags['badges'] = badges
# Reloads the commands.py module, allowing for modification of
# existing commands while the bot is still running.
if e.arguments[0] == "!reload":
badges_tag = [dict['value'] for dict in e.tags
if dict['key'] == 'badges']
badges_list = []
if badges_tag[0]:
badges_list = badges_tag[0].split(",")
badges_lists_list = [badge.split("/") for badge in badges_list
if badge]

badges = {badge_list[0]: badge_list[1] for badge_list
in badges_lists_list}
for badge, value in badges.items():
if badge == "broadcaster" and value == "1":
global commands
commands = reload(commands)
cmd_obj = commands.Commands()
if badges.get('broadcaster') == '1':
global cmd_obj
global commands
commands = reload(commands)
cmd_obj = commands.Commands()
else:
self.exec_command(c, e, cmd_obj)

else:
# Times messages are only sent after a certain amount of non-timed
# messages. Calling send_timers from here allows counting
# messages. Timed messages should not follow execution of commands
# immediately, so this is only called if no command was used.
self.send_timers(self.timers(), c)

@commands.exec
def exec_command(self, c, e, cmd_obj):
"""Check if command exists and checks for possible cooldown,
then executes if possible"""
"""Check if command exists and checks for cooldown, then exectues if
permitted.
"""

return e, c, self, cmd_obj

def timers(self):
Expand Down

0 comments on commit 9c307c7

Please sign in to comment.