Skip to content

Commit fcd1ef1

Browse files
authored
Merge pull request #42 from CactusBot/release-v0.3.5
Release v0.3.5
2 parents 140ae4c + 8cd0b22 commit fcd1ef1

File tree

5 files changed

+26
-71
lines changed

5 files changed

+26
-71
lines changed

beam.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def __init__(self, debug="INFO", **kwargs):
2929

3030
self.message_id = 0
3131
self.csrf_token = None
32+
self.quiet = None
33+
self.bot_id = None
34+
self.channel_id = None
3235

3336
self.http_session = Session()
3437

@@ -136,10 +139,14 @@ def get_chat_users(self, id):
136139
def connect(self, channel_id, bot_id, quiet=False):
137140
"""Connect to a Beam chat through a websocket."""
138141

142+
self.channel_id = channel_id
143+
self.bot_id = bot_id
144+
self.quiet = quiet
145+
139146
self.connection_information = {
140-
"channel_id": channel_id,
141-
"bot_id": bot_id,
142-
"quiet": quiet
147+
"channel_id": self.channel_id,
148+
"bot_id": self.bot_id,
149+
"quiet": self.quiet
143150
}
144151

145152
chat = self.get_chat(channel_id)
@@ -165,8 +172,6 @@ def connect(self, channel_id, bot_id, quiet=False):
165172
def authenticate(self, *args):
166173
"""Authenticate session to a Beam chat through a websocket."""
167174

168-
backoff = 0
169-
170175
future = args[-1]
171176
if future.exception() is None:
172177
self.websocket = future.result()
@@ -181,12 +186,11 @@ def authenticate(self, *args):
181186
self.read_chat(self.handle)
182187
else:
183188
self.logger.error("There was an issue connecting.")
184-
self.logger.error("Trying again in {} seconds.".format(backoff))
189+
self.logger.error("Trying again in 10 seconds.")
185190

186-
time.sleep(min(2**backoff, 60))
187-
backoff += 1
191+
time.sleep(10)
188192

189-
self.authenticate(*args)
193+
self.connect(self.channel_id, self.bot_id, self.quiet)
190194

191195
def send_message(self, *args, method="msg"):
192196
"""Send a message to a Beam chat through a websocket."""

cactus.py

+1-34
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def __init__(self, **kwargs):
5454
self.debug = kwargs.get("debug", False)
5555

5656
self.config_file = kwargs.get("config_file", "data/config.json")
57-
self.stats_file = kwargs.get("stats_file", "data/stats.json")
5857
self.database = kwargs.get("database", "data/data.db")
5958

6059
self.quiet = kwargs.get("quiet", False)
@@ -90,23 +89,6 @@ def load_config(self, filename):
9089
raise FileNotFoundError("Configuration file not found.")
9190
exit()
9291

93-
def load_stats(self, filename):
94-
"""Load statistics file."""
95-
96-
self.logger.warning("Statistics are not yet implemented.")
97-
return dict()
98-
99-
if exists(filename):
100-
self.stats_file = filename
101-
self.logger.info("Statistics file found. Loading...")
102-
with open(filename) as stats:
103-
self.stats = load(stats)
104-
return self.stats
105-
else:
106-
self.logger.warn("Statistics file not found. Creating...")
107-
copyfile("data/stats-template.json", "data/stats.json")
108-
self.logger.info("Statistics file created.")
109-
11092
def update_config(self, keys, value):
11193
"""Update configuration file value."""
11294

@@ -119,33 +101,18 @@ def update_config(self, keys, value):
119101
self.config = config_data
120102
return self.config
121103

122-
def update_stats(self, keys, value):
123-
"""Update statistics file value."""
124-
125-
self.logger.warning("Statistics are not yet implemented.")
126-
return
127-
128-
with open(self.stats_file, 'r') as stats:
129-
stats_data = load(stats)
130-
reduce(lambda d, k: d[k], keys.split('.')[:-1], stats_data)[
131-
keys.split('.')[-1]] = value
132-
with open(self.stats_file, 'w+') as stats:
133-
dump(stats_data, stats, indent=2, sort_keys=True)
134-
self.stats = stats_data
135-
return self.stats
136-
137104
def run(self, *args, **kwargs):
138105
"""Run bot."""
139106

140107
self.logger.info(cactus_art)
141108
self._init_database(self.database)
142109
self.load_config(filename=self.config_file)
143-
self.load_stats(filename=self.stats_file)
144110
self.started = True
145111

146112
while self.config.get("autorestart") or not self.started:
147113
try:
148114
self.bot_data = self.login(**self.config["auth"])
115+
self.bot_name = self.config["auth"]["username"]
149116
self.logger.info("Authenticated as: {}.".format(
150117
self.bot_data["username"]))
151118

data/stats-template.json

-3
This file was deleted.

messages.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from beam import Beam
66
from models import (Command, User, session, CommandCommand, QuoteCommand,
77
CubeCommand, SocialCommand, UptimeCommand, PointsCommand,
8-
TemmieCommand, FriendCommand, SpamProtCommand, ProCommand,
9-
SubCommand, RepeatCommand)
8+
TemmieCommand, FriendCommand, SpamProtCommand,
9+
RepeatCommand)
1010

1111

1212
class MessageHandler(Beam):
@@ -38,8 +38,6 @@ def _init_commands(self):
3838
"friend": FriendCommand(self.get_channel),
3939
"points": PointsCommand(self.config["points"]["name"]),
4040
"spamprot": SpamProtCommand(self.update_config),
41-
"pro": ProCommand(),
42-
"sub": SubCommand(),
4341
"cube": CubeCommand(),
4442
"temmie": TemmieCommand()
4543
}
@@ -89,7 +87,8 @@ def message_handler(self, data):
8987
session.commit()
9088

9189
mod_roles = ("Owner", "Staff", "Founder", "Global Mod", "Mod")
92-
if not (data["user_roles"][0] in mod_roles or user.friend):
90+
if not (data["user_roles"][0] in mod_roles or user.friend or
91+
self.bot_name.lower() == data["user_name"].lower()):
9392
if (len(parsed) > self.config["spam_protection"].get(
9493
"maximum_message_length", 256)):
9594
self.remove_message(data["id"])

models.py

+8-20
Original file line numberDiff line numberDiff line change
@@ -491,20 +491,22 @@ def __call__(self, args, data):
491491
if len(args) < 2:
492492
return "Not enough arguments."
493493
elif len(args) == 2:
494-
match = re.match(r'@?([A-Za-z0-9]{,32})', args[1])
495-
if match is None:
494+
user = re.match(r'@?([\w_-]*[a-z][\w_-]*', args[1])
495+
if user is None:
496496
return "Invalid username '{}'.".format(args[1])
497497

498-
id = self.get_channel(args[1])
499-
if id.get("statusCode") == 404:
498+
channel_id = self.get_channel(user.group())
499+
500+
if channel_id.get("statusCode") == 404:
500501
return "User has not entered this channel."
501502

502-
query = session.query(User).filter_by(id=id["user"]["id"]).first()
503+
query = session.query(User).filter_by(
504+
id=channel_id["user"]["id"]).first()
503505
if query:
504506
query.friend = not query.friend
505507
session.commit()
506508
return "{}ed @{} as a friend.".format(
507-
["Remov", "Add"][query.friend], args[1])
509+
["Remov", "Add"][query.friend], user.group())
508510
else:
509511
return "User has not entered this channel."
510512
elif len(args) > 2:
@@ -557,17 +559,3 @@ def __call__(self, args, data=None):
557559
return "Invalid true/false: '{}'.".format(args[2])
558560
return "Invalid argument: '{}'.".format(args[1])
559561
return "Not enough arguments."
560-
561-
562-
class ProCommand(Command):
563-
564-
@role_specific("Pro", reply="pro")
565-
def __call__(self, args=None, data=None):
566-
return "I'm such a Pro! B)"
567-
568-
569-
class SubCommand(Command):
570-
571-
@role_specific("Subscriber", reply="sub")
572-
def __call__(self, args=None, data=None):
573-
return "I'm a subscriber! :salute"

0 commit comments

Comments
 (0)