mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-11 18:10:37 +01:00
Bot twitch
This commit is contained in:
@@ -6,38 +6,44 @@ from twitchAPI.twitch import Twitch
|
|||||||
from twitchAPI.type import AuthScope, ChatEvent
|
from twitchAPI.type import AuthScope, ChatEvent
|
||||||
from twitchAPI.chat import Chat, ChatEvent, ChatMessage, EventData
|
from twitchAPI.chat import Chat, ChatEvent, ChatMessage, EventData
|
||||||
|
|
||||||
CLIENT_ID= 'TODO'
|
from database.helpers import ConfigurationHelper
|
||||||
CLIENT_SECRET='TODO'
|
|
||||||
|
from webapp import webapp
|
||||||
|
|
||||||
USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]
|
USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]
|
||||||
|
|
||||||
CHANNEL = "#TODO"
|
|
||||||
|
|
||||||
ACCESS_TOKEN = 'TODO'
|
|
||||||
REFRESH_TOKEN = 'TODO'
|
|
||||||
|
|
||||||
|
|
||||||
async def _onReady(ready_event: EventData):
|
async def _onReady(ready_event: EventData):
|
||||||
logging.info('Twitch bot ready')
|
logging.info('Twitch bot ready')
|
||||||
await ready_event.chat.join_room(CHANNEL)
|
with webapp.app_context():
|
||||||
|
await ready_event.chat.join_room(ConfigurationHelper().getValue('twitch_channel'))
|
||||||
|
|
||||||
async def _onMessage(msg: ChatMessage):
|
async def _onMessage(msg: ChatMessage):
|
||||||
print(f'in {msg.room.name}, {msg.user.name} said: {msg.text}')
|
logging.info(f'in {msg.room.name}, {msg.user.name} said: {msg.text}')
|
||||||
|
|
||||||
|
# commande qui repond "bonjour" a "!hello"
|
||||||
async def _helloCommand(msg: ChatMessage):
|
async def _helloCommand(msg: ChatMessage):
|
||||||
await msg.reply(f'Bonjour {msg.user.name}')
|
await msg.reply(f'Bonjour {msg.user.name}')
|
||||||
|
|
||||||
|
def _isConfigured() -> bool:
|
||||||
|
helper = ConfigurationHelper()
|
||||||
|
return helper.getValue('twitch_client_id') != None and helper.getValue('twitch_client_secret') != None and helper.getValue('twitch_access_token') != None and helper.getValue('twitch_refresh_token') != None and helper.getValue('twitch_channel') != None
|
||||||
|
|
||||||
class TwitchBot() :
|
class TwitchBot() :
|
||||||
|
|
||||||
async def _connect(self):
|
async def _connect(self):
|
||||||
self.twitch = await Twitch(CLIENT_ID, CLIENT_SECRET)
|
with webapp.app_context():
|
||||||
await self.twitch.set_user_authentication(ACCESS_TOKEN, USER_SCOPE, REFRESH_TOKEN)
|
if _isConfigured() :
|
||||||
self.chat = await Chat(self.twitch)
|
helper = ConfigurationHelper()
|
||||||
self.chat.register_event(ChatEvent.READY, _onReady)
|
self.twitch = await Twitch(helper.getValue('twitch_client_id'), helper.getValue('twitch_client_secret'))
|
||||||
self.chat.register_event(ChatEvent.MESSAGE, _onMessage)
|
await self.twitch.set_user_authentication(helper.getValue('twitch_access_token'), USER_SCOPE, helper.getValue('twitch_refresh_token'))
|
||||||
# chat.register_event(ChatEvent.SUB, on_sub)
|
self.chat = await Chat(self.twitch)
|
||||||
self.chat.register_command('hello', _helloCommand)
|
self.chat.register_event(ChatEvent.READY, _onReady)
|
||||||
self.chat.start()
|
self.chat.register_event(ChatEvent.MESSAGE, _onMessage)
|
||||||
|
# chat.register_event(ChatEvent.SUB, on_sub)
|
||||||
|
self.chat.register_command('hello', _helloCommand)
|
||||||
|
self.chat.start()
|
||||||
|
else:
|
||||||
|
logging.info("Twitch is not configured")
|
||||||
|
|
||||||
def begin(self):
|
def begin(self):
|
||||||
asyncio.run(self._connect())
|
asyncio.run(self._connect())
|
||||||
|
|||||||
Reference in New Issue
Block a user