'添加自定义事件'
This commit is contained in:
65
main.py
65
main.py
@@ -1,24 +1,65 @@
|
||||
import asyncio
|
||||
from TikTokLive import TikTokLiveClient
|
||||
from TikTokLive.events import ConnectEvent, CommentEvent
|
||||
from TikTokLive.client.logger import LogLevel
|
||||
from TikTokLive.events import ConnectEvent, GiftEvent
|
||||
|
||||
# Create the client
|
||||
client: TikTokLiveClient = TikTokLiveClient(unique_id="@isaackogz")
|
||||
client: TikTokLiveClient = TikTokLiveClient(
|
||||
unique_id="@tv_asahi_news"
|
||||
)
|
||||
|
||||
|
||||
# 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 {event.like_count} likes from {event.user.unique_id}!")
|
||||
print(f"Received {event.like_count} 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}\"")
|
||||
# Cannot have a streak
|
||||
elif not event.gift.streakable:
|
||||
print(f"{event.user.unique_id} sent \"{event.gift.name}\"")
|
||||
|
||||
|
||||
# 检查用户直播是否在线
|
||||
@client.on(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 on_comment(event: CommentEvent) -> None:
|
||||
print(f"{event.user.nickname} -> {event.comment}")
|
||||
async def check_loop():
|
||||
# Run 24/7
|
||||
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__':
|
||||
# Run the client and block the main thread
|
||||
# await client.start() to run non-blocking
|
||||
client.logger.setLevel(LogLevel.INFO.value)
|
||||
asyncio.run(check_loop())
|
||||
# Set the login session ID token BEFORE connecting
|
||||
# client.web.set_session("session-id-here")
|
||||
|
||||
# Connect
|
||||
client.run()
|
||||
Reference in New Issue
Block a user