Compare commits

...

2 Commits

Author SHA1 Message Date
Umiko
3158583f65 adding event handler. trying though.
Some checks failed
Build-nightly / docker (push) Has been cancelled
2025-05-05 10:42:27 +07:00
Umiko
6458f19226 pushing old yt.py instead due to always error. 2025-05-05 10:06:38 +07:00
4 changed files with 80 additions and 28 deletions

View File

@ -44,43 +44,23 @@ class YtService(_Service):
def initialize(self):
self._ydl_config = {
"format": "bestaudio/best",
"outtmpl": "%(title)s.%(ext)s",
"skip_download": True,
"format": "m4a/bestaudio/best[protocol!=m3u8_native]/best",
"socket_timeout": 5,
"logger": logging.getLogger("root"),
"cookiefile": "/home/ttbot/data/cookies.txt",
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}
],
"postprocessor_args": [
"-ar", "44100"
],
"prefer_ffmpeg": True,
"keepvideo": False,
"cookiefile": "/home/ttbot/data/cookies.txt"
}
def download(self, track: Track, file_path: str) -> None:
try:
info = track.extra_info
if not info:
# fallback ke parent class kalo gak ada info
super().download(track, file_path)
return
ydl_opts = self._ydl_config.copy()
ydl_opts["outtmpl"] = file_path.rsplit(".", 1)[0] + ".mp3"
with YoutubeDL(ydl_opts) as ydl:
# Download langsung pakai URL dari info
url = info.get("webpage_url")
if not url:
raise errors.ServiceError("Missing webpage_url in track info")
ydl.download([url])
with YoutubeDL(self._ydl_config) as ydl:
dl = get_suitable_downloader(info)(ydl, self._ydl_config)
dl.download(file_path, info)
except Exception as e:
logging.error(f"Download error: {str(e)}", exc_info=True)
raise errors.ServiceError("Download failed")

View File

@ -1,6 +1,5 @@
This change log is written to find out the changes that have been made by Pandora, and the source code still refers to TTMediaBot.
5/5/2025
Change the download from m4a to mp3, if it's work.
Added new command: e, to add either link or new song to the extended track. if nothing is playing, it plays the track directly.
Fix user rights to upload file, hopefully...

69
dd.py Normal file
View File

@ -0,0 +1,69 @@
import logging
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from bot.TeamTalk import TeamTalk, Event, EventType, MessageType
# Load the watchlist from a file (watchlist.txt)
def load_watchlist():
"""Load the watchlist of prohibited words."""
watchlist = []
try:
with open("watchlist.txt", "r") as f:
watchlist = [line.strip().lower() for line in f.readlines()]
except FileNotFoundError:
logging.error("Watchlist file not found. Please create 'watchlist.txt'.")
return watchlist
# Check if the user's message contains any prohibited words
def contains_prohibited_word(message: str, watchlist: list) -> bool:
"""Check if a message contains any prohibited words from the watchlist."""
for word in watchlist:
if word in message.lower():
return True
return False
# Kick the user and broadcast the reason
def kick_user_and_broadcast(ttclient: TeamTalk, user_name: str) -> None:
"""Kick the user and broadcast a message about the kick."""
ttclient.kick_user(user_name)
broadcast_message = f"{user_name} has been kicked for using prohibited language."
ttclient.broadcast_message(broadcast_message)
logging.info(f"Kicked user: {user_name} for prohibited message.")
# Event handler for a user text message
def on_user_text_message(event: Event, ttclient: TeamTalk):
"""Handle the event when a user sends a text message."""
watchlist = load_watchlist()
message = event.message.text
user_name = event.message.sender.name
# Check if the message contains a prohibited word
if contains_prohibited_word(message, watchlist):
logging.info(f"User {user_name} used a prohibited word. Kicking user...")
kick_user_and_broadcast(ttclient, user_name)
else:
logging.info(f"User {user_name}'s message: '{message}' is clean.")
# Map event types to their respective handler functions
event_handlers = {
EventType.USER_TEXT_MESSAGE: on_user_text_message,
# You can add more event handlers here for different event types.
}
def run_event_handler(event: Event, ttclient: TeamTalk):
"""Run the appropriate event handler based on the event type."""
try:
event_handler = event_handlers.get(event.event_type)
if event_handler:
event_handler(event, ttclient)
else:
logging.info(f"No handler for event type: {event.event_type.name}")
except Exception as e:
logging.error(f"Error in event handling: {e}")

4
watchlist.txt Normal file
View File

@ -0,0 +1,4 @@
anjing
stupid
idiot
fuck