Teekeks / pyTwitchAPI

A Python 3.7 compatible implementation of the Twitch API, EventSub, PubSub and Chat
https://pytwitchapi.dev
MIT License
242 stars 36 forks source link

I think there are text #266

Closed chamfist closed 9 months ago

chamfist commented 9 months ago

import sys

sys.path.append('/home/cham/.local/lib/python3.9/site-packages')
from twitchAPI import Twitch
from twitchAPI.helper import first
from twitchAPI.eventsub import EventSub
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope, ChatEvent
from twitchAPI.chat import Chat, EventData, ChatMessage, ChatSub, ChatCommand
import asyncio

EVENTSUB_URL = 'hugiugkjhkjhukhkjh'

APP_ID =‘hujojhiohtyfhkijioj'
APP_SECRET = 'giuguhjtyffuygihiuhoihjo'
USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT, AuthScope.MODERATOR_READ_FOLLOWERS]
TARGET_CHANNEL = 'chamfist'

async def on_follow(data: dict):
    await Chat.send_message(TARGET_CHANNEL,"フォローありがとう!!")
    # our event happend, lets do things with the data we got!
    print(data)

# this will be called when the event READY is triggered, which will be on bot start
async def on_ready(ready_event: EventData):
    print('Bot is ready for work, joining channels')
    # join our target channel, if you want to join multiple, either call join for each individually
    # or even better pass a list of channels as the argument
    await ready_event.chat.join_room(TARGET_CHANNEL)
    # you can do other bot initialization things in here

# this will be called whenever a message in a channel was send by either the bot OR another user
async def on_message(msg: ChatMessage):
    if "こん" in msg.text:
        await Chat.send_message(TARGET_CHANNEL, "こん!!")
    print(f'in {msg.room.name}, {msg.user.name} said: {msg.text}')

# this will be called whenever the !reply command is issued
async def test_command(cmd: ChatCommand):
    if len(cmd.parameter) == 0:
        await cmd.reply('you did not tell me what to reply with')
    else:
        await cmd.reply(f'{cmd.user.name}: {cmd.parameter}')

async def sens(cmd: ChatCommand):
    await Chat.send_message(TARGET_CHANNEL, "dpi1600sens3.9")

async def want_join(cmd: ChatCommand):
    await Chat.send_message(TARGET_CHANNEL, "https://discord.gg/DcADz9pd5w ちゃむ@ついっち#3531")

# this is where we set up the bot
async def run():
    # set up twitch api instance and add user authentication with some scopes
    twitch = await Twitch(APP_ID, APP_SECRET)
    user = await first(twitch.get_users(logins=TARGET_CHANNEL))
    await twitch.set_user_authentication("iubbuihiuhoijoijio", USER_SCOPE, “jnkoihuhiuhunljioijijljlnuih")

    event_sub = EventSub(EVENTSUB_URL, APP_ID, 207, twitch)
    await event_sub.unsubscribe_all()
    event_sub.start()
    await event_sub.listen_channel_follow_v2(user.id, user.id, on_follow)
    # create chat instance
    chat = await Chat(twitch)

    # register the handlers for the events you want

    # listen to when the bot is done starting up and ready to join channels
    chat.register_event(ChatEvent.READY, on_ready)
    # listen to chat messages
    chat.register_event(ChatEvent.MESSAGE, on_message)

    # you can directly register commands and their handlers, this will register the !reply command
    chat.register_command('reply', test_command)
    chat.register_command('sens', sens)
    chat.register_command('join', want_join)

    # we are done with our setup, lets start this bot up!
    chat.start()

    # lets run till we press enter in the console
    try:
        input('press ENTER to stop\n')
    finally:
        # now we can close the chat bot and the twitch api client
        chat.stop()
        await event_sub.stop()
        await twitch.close()

# lets run our setup
asyncio.run(run())

I'm creating a chatbot, but an error occurs and I can't proceed. When it reserve こん I got an error

  File "/home/cham/chamtwitch/main.py", line 37, in on_message
    await Chat.send_message(TARGET_CHANNEL, "kon!!")
TypeError: send_message() missing 1 required positional argument: 'text'

I think there are text… I can’t understand why…

Teekeks commented 9 months ago

await Chat.send_message(TARGET_CHANNEL, "こん!!")

in your on_message, that should be msg.chat.send_message(TARGET_CHANNEL, "こん!!")