Compare commits

2 Commits
main ... dev

Author SHA1 Message Date
eric
157b7bfbe4 'sss' 2025-08-03 16:45:06 +08:00
eric
fde4a0e676 '添加自定义事件' 2025-08-03 16:38:57 +08:00

68
main.py
View File

@@ -1,24 +1,68 @@
import asyncio
from TikTokLive import TikTokLiveClient from TikTokLive import TikTokLiveClient
from TikTokLive.events import ConnectEvent, CommentEvent from TikTokLive.client.logger import LogLevel
from TikTokLive.events import ConnectEvent, GiftEvent,JoinEvent, LikeEvent
# Create the client client: TikTokLiveClient = TikTokLiveClient(
client: TikTokLiveClient = TikTokLiveClient(unique_id="@isaackogz") unique_id="@jejaksikemal3"
)
# Listen to an event with a decorator! # 加入直播间
@client.on(JoinEvent)
async def on_join(event: JoinEvent) -> None:
client.logger.info(f"User {event.user.unique_id} joined the live room!")
print(f"加入直播间User {event.user.unique_id} joined the live room!")
# You can also access the user object directly
# print(event.user)
# 点赞
@client.on(LikeEvent)
async def on_like(event: LikeEvent) -> None:
client.logger.info(f"Received likes from {event.user.unique_id}!")
print(f"点赞===Received likes from {event.user.unique_id}!")
# You can also access the user object directly
# print(event.user)
# 监听礼物打赏
@client.on(GiftEvent)
async def on_gift(event: GiftEvent):
client.logger.info("Received a gift!")
# Can have a streak and streak is over
if event.gift.streakable and not event.streaking:
print(f"{event.user.unique_id} sent {event.repeat_count}x \"{event.gift.name}\"")
print('连续礼物打赏')
# Cannot have a streak
elif not event.gift.streakable:
print(f"{event.user.unique_id} sent \"{event.gift.name}\"")
print('礼物打赏')
# 检查用户直播是否在线
@client.on(ConnectEvent) @client.on(ConnectEvent)
async def on_connect(event: ConnectEvent): async def on_connect(event: ConnectEvent):
print(f"Connected to @{event.unique_id} (Room ID: {client.room_id}") client.logger.info(f"Connected to @{event.unique_id}!")
print(f"Connected to @{event.unique_id}!")
# Or, add it manually via "client.add_listener()" async def check_loop():
async def on_comment(event: CommentEvent) -> None: # Run 24/7
print(f"{event.user.nickname} -> {event.comment}") while True:
# Check if they're live
while not await client.is_live():
client.logger.info("Client is currently not live. Checking again in 60 seconds.")
await asyncio.sleep(60) # Spamming the endpoint will get you blocked
# Connect once they become live
client.logger.info("Requested client is live!")
await client.connect()
client.add_listener(CommentEvent, on_comment)
if __name__ == '__main__': if __name__ == '__main__':
# Run the client and block the main thread client.logger.setLevel(LogLevel.INFO.value)
# await client.start() to run non-blocking asyncio.run(check_loop())
# Set the login session ID token BEFORE connecting
# client.web.set_session("session-id-here")
# Connect
client.run() client.run()