Skip to content

Commit

Permalink
Completed #19
Browse files Browse the repository at this point in the history
  • Loading branch information
thelabcat committed Aug 12, 2024
1 parent dd64ac6 commit c6d453b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 166 deletions.
41 changes: 20 additions & 21 deletions src/rumchat_actor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def eat_some_cheese(message, actor):
import textwrap
import time
import threading
from cocorum import RumbleAPI, utils
from cocorum import RumbleAPI, utils as crutils
from cocorum.ssechat import SSEChat
import selenium
from selenium import webdriver
Expand All @@ -47,8 +47,7 @@ def eat_some_cheese(message, actor):
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from . import actions, commands, misc, utils
from .localvars import *
from . import actions, commands, misc, utils, static

class RumbleChatActor():
"""Actor that interacts with Rumble chat"""
Expand Down Expand Up @@ -116,7 +115,7 @@ def __init__(self, init_message = "Hello, Rumble!", ignore_users = ["TheRumbleBo
assert self.api_stream, "No stream ID was passed and you are not live"

self.stream_id = self.api_stream.stream_id
self.stream_id_b10 = utils.stream_id_36_to_10(self.stream_id)
self.stream_id_b10 = crutils.stream_id_36_to_10(self.stream_id)

#Get SSE chat and empty the mailbox
self.ssechat = SSEChat(stream_id = self.stream_id)
Expand All @@ -131,7 +130,7 @@ def __init__(self, init_message = "Hello, Rumble!", ignore_users = ["TheRumbleBo
#Get browser
self.driver = webdriver.Firefox(options = options)
self.driver.minimize_window()
self.driver.get(CHAT_URL.format(stream_id_b10 = self.ssechat.stream_id_b10))
self.driver.get(static.URI.chat_popout.format(stream_id_b10 = self.ssechat.stream_id_b10))
assert "Chat" in self.driver.title

#Sign in to chat, unless we are already. While there is a sign-in button...
Expand All @@ -143,7 +142,7 @@ def __init__(self, init_message = "Hello, Rumble!", ignore_users = ["TheRumbleBo
if first_time:
#We have any credentials, wait for the sign in dialouge and enter what wa have
if "username" in kwargs or "password" in kwargs:
WebDriverWait(self.driver, BROWSER_WAIT_TIMEOUT).until(
WebDriverWait(self.driver, static.Driver.wait_timeout).until(
EC.visibility_of_element_located((By.ID, "login-username")),
"Timed out waiting for sign-in dialouge"
)
Expand All @@ -164,7 +163,7 @@ def __init__(self, init_message = "Hello, Rumble!", ignore_users = ["TheRumbleBo
input("Please log in at the browser, then press enter here.")

#Wait for signed in loading to complete
WebDriverWait(self.driver, BROWSER_WAIT_TIMEOUT).until(
WebDriverWait(self.driver, static.Driver.wait_timeout).until(
EC.element_to_be_clickable((By.ID, "chat-message-text-input")),
"Timed out waiting for chat message field to become usable"
)
Expand Down Expand Up @@ -281,21 +280,21 @@ def streamer_main_page_url(self):
if self.api_stream:
#We know our channel ID from the API
if self.rum_api.channel_id:
self.__streamer_main_page_url = CHANNEL_URL.format(channel_name = f"c-{self.rum_api.channel_id}")
self.__streamer_main_page_url = static.URI.channel_page.format(channel_name = f"c-{self.rum_api.channel_id}")

#Is a channel stream and on the API but API is not for channel, use the user page instead
else:
self.__streamer_main_page_url = USER_URL.format(username = self.streamer_username)
self.__streamer_main_page_url = static.URI.user_page.format(username = self.streamer_username)

#Is not an API stream and we don't know the username
elif not self.__streamer_username:
while not (specified := input("Enter streamer main page URL: ")).startswith(RUMBLE_BASE_URL):
while not (specified := input("Enter streamer main page URL: ")).startswith(static.URI.rumble_base):
pass
self.__streamer_main_page_url = specified

#Not a channel stream, go by username
else:
self.__streamer_main_page_url = USER_URL.format(username = self.streamer_username)
self.__streamer_main_page_url = static.URI.user_page.format(username = self.streamer_username)

return self.__streamer_main_page_url

Expand All @@ -309,25 +308,25 @@ def get_sign_in_button(self):

def send_message(self, text):
"""Send a message in chat (splits across lines if necessary)"""
text = BOT_MESSAGE_PREFIX + text
text = static.Message.bot_prefix + text
assert "\n" not in text, "Message cannot contain newlines"
assert len(text) < MAX_MULTIMESSAGE_LEN, "Message is too long"
for subtext in textwrap.wrap(text, width = MAX_MESSAGE_LEN):
assert len(text) < static.Message.max_multi_len, "Message is too long"
for subtext in textwrap.wrap(text, width = static.Message.max_len):
self.outbox.append(subtext)
print("💬:", subtext)

def _sender_loop(self):
"""Constantly check our outbox and send any messages in it"""
while self.keep_running:
#We have messages to send and it is time to send one
if self.outbox and time.time() - self.last_message_send_time > SEND_MESSAGE_COOLDOWN:
if self.outbox and time.time() - self.last_message_send_time > static.Message.send_cooldown:
self.__send_message(self.outbox.pop(0))
time.sleep(0.1)

def __send_message(self, text):
"""Send a message in chat"""
assert len(text) < MAX_MESSAGE_LEN, \
f"Message with prefix cannot be longer than {MAX_MESSAGE_LEN} characters"
assert len(text) < static.Message.max_len, \
f"Message with prefix cannot be longer than {static.Message.max_len} characters"

self.sent_messages.append(text)
self.last_message_send_time = time.time()
Expand Down Expand Up @@ -403,7 +402,7 @@ def delete_message(self, message):
del_bttn.click()

#Wait for the confirmation to appear
WebDriverWait(self.driver, BROWSER_WAIT_TIMEOUT).until(
WebDriverWait(self.driver, static.Driver.wait_timeout).until(
EC.alert_is_present(),
"Timed out waiting for deletion confirmation dialouge to appear"
)
Expand All @@ -420,7 +419,7 @@ def mute_by_message(self, message, mute_level = "5"):

timeout_bttn = self.driver.find_element(
By.XPATH,
f"//button[@class='{MUTE_LEVELS[mute_level]}']"
f"//button[@class='{static.Moderation.mute_levels[mute_level]}']"
)

timeout_bttn.click()
Expand Down Expand Up @@ -469,11 +468,11 @@ def quit(self):
def __run_if_command(self, message):
"""Check if a message is a command, and run it if so"""
#Not a command
if not message.text.startswith(COMMAND_PREFIX):
if not message.text.startswith(static.Message.command_prefix):
return

#Get command name
name = message.text.split()[0].removeprefix(COMMAND_PREFIX)
name = message.text.split()[0].removeprefix(static.Message.command_prefix)

#Is not a valid command
if name not in self.chat_commands:
Expand Down
11 changes: 6 additions & 5 deletions src/rumchat_actor/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import threading
import time
import talkey
from .localvars import *
from . import static

try:
import ollama
OLLAMA_IMPORTED = True
Expand All @@ -25,13 +26,13 @@ def ollama_message_moderate(message, actor):
return True

#User has an immunity badge
if True in [badge in message.user.badges for badge in STAFF_BADGES]:
if True in [badge in message.user.badges for badge in static.Moderation.staff_badges]:
print(f"{message.user.username} is staff, skipping LLM check.")
return True

#Get the LLM verdict
response = ollama.chat(model = OLLAMA_MODEL, messages = [
{"role" : "system", "content" : LLM_MODERATOR_SYS_MESSAGE},
response = ollama.chat(model = static.AutoModerator.llm_model, messages = [
{"role" : "system", "content" : static.AutoModerator.llm_sys_prompt},
{"role" : "user", "content" : message.text},
])

Expand Down Expand Up @@ -103,7 +104,7 @@ def __init__(self, actor, messages: iter, delay = 60, in_between = 0):
self.actor = actor
assert len(messages) > 0, "List of messages to send cannot be empty"
self.messages = messages
assert delay > SEND_MESSAGE_COOLDOWN, "Cannot send timed messages that frequently"
assert delay > static.Message.send_cooldown, "Cannot send timed messages that frequently"
self.delay = delay
self.in_between = in_between

Expand Down
Loading

0 comments on commit c6d453b

Please sign in to comment.