Как узнать, активен ли стример Twitch, и сообщить об этом? Discord.py

Сейчас я пишу бота Discord, поэтому я хотел знать, как сохранить имя стримера в отдельном файле, чтобы при запуске бот отправлял сообщение об этом в определенном канале.

Вот что я пробовал:

import os
import json
import discord
import requests
from discord.ext import tasks, commands
from twitchAPI.twitch import Twitch
from discord.utils import get

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='$', intents=intents)

Аутентификация с помощью twitch API:

client_id = os.getenv('client_id')
client_secret = os.getenv('Dweller_token')
twitch = Twitch(client_id, client_secret)
twitch.authenticate_app([])
TWITCH_STREAM_API_ENDPOINT_V5 = "https://api.twitch.tv/dweller/streams/{}"
API_HEADERS = {
  'Client-ID': client_id,
  'Accept': 'application/vnd.twitchtv.v5+json',
}

Возвращает True, если онлайн, False, если нет:

def checkuser(user):
    try:
        userid = twitch.get_users(logins=[user])['data'][0]['id']
        url = TWITCH_STREAM_API_ENDPOINT_V5.format(userid)
        try:
            req = requests.Session().get(url, headers=API_HEADERS)
            jsondata = req.json()
            if 'stream' in jsondata:
                if jsondata['stream'] is not None:
                    return True
                else:
                    return False
        except Exception as e:
            print("Error checking user: ", e)
            return False
    except IndexError:
        return False

Событие бота. Всегда проверяет, активен ли стример. Отправляет сообщение, если это так. И добавляет конкретную роль стримеру, если он жив:

@bot.event
async def on_ready():
    # Defines a loop that will run every 10 seconds (checks for live users every 10 seconds).
    @tasks.loop(seconds=10)
    async def live_notifs_loop():
        # Opens and reads the json file
        with open('streamers.json', 'r') as file:
            streamers = json.loads(file.read())
        # Makes sure the json isn't empty before continuing.
        if streamers is not None:
            # Gets the guild, 'twitch streams' channel, and streaming role.
            guild = bot.get_guild(1234567890)
            channel = bot.get_channel(1234567890)
            role = get(guild.roles, id=1234567890)
            # Loops through the json and gets the key,value which in this case is the user_id and twitch_name of
            # every item in the json.
            for user_id, twitch_name in streamers.items():
                # Takes the given twitch_name and checks it using the checkuser function to see if they're live.
                # Returns either true or false.
                status = checkuser(twitch_name)
                # Gets the user using the collected user_id in the json
                user = bot.get_user(int(user_id))
                # Makes sure they're live
                if status is True:
                    # Checks to see if the live message has already been sent.
                    async for message in channel.history(limit=200):
                        # If it has, break the loop (do nothing).
                        if str(user.mention) in message.content and "is now streaming" in message.content:
                            break
                        # If it hasn't, assign them the streaming role and send the message.
                        else:
                            # Gets all the members in your guild.
                            async for member in guild.fetch_members(limit=None):
                                # If one of the id's of the members in your guild matches the one from the json and
                                # they're live, give them the streaming role.
                                if member.id == int(user_id):
                                    await member.add_roles(role)
                            # Sends the live notification to the 'twitch streams' channel then breaks the loop.
                            await channel.send(
                                f":red_circle: **LIVE**\n{user.mention} is now streaming on Twitch!"
                                f"\nhttps://www.twitch.tv/{twitch_name}")
                            print(f"{user} started streaming. Sending a notification.")
                            break
                # If they aren't live do this:
                else:
                    # Gets all the members in your guild.
                    async for member in guild.fetch_members(limit=None):
                        # If one of the id's of the members in your guild matches the one from the json and they're not
                        # live, remove the streaming role.
                        if member.id == int(user_id):
                            await member.remove_roles(role)
                    # Checks to see if the live notification was sent.
                    async for message in channel.history(limit=200):
                        # If it was, delete it.
                        if str(user.mention) in message.content and "is now streaming" in message.content:
                            await message.delete()
    # Start your loop.
    live_notifs_loop.start()

Команда, добавляющая выбранные стримеры в json файл:

# Command to add Twitch usernames to the json.
@bot.command(name='addtwitch', help='Adds your Twitch to the live notifs.', pass_context=True)
async def add_twitch(ctx, twitch_name):
    # Opens and reads the json file.
    with open('streamers.json', 'r') as file:
        streamers = json.loads(file.read())
    
    # Gets the users id that called the command.
    user_id = ctx.author.id
    # Assigns their given twitch_name to their discord id and adds it to the streamers.json.
    streamers[user_id] = twitch_name
    
    # Adds the changes we made to the json file.
    with open('streamers.json', 'w') as file:
        file.write(json.dumps(streamers))
    # Tells the user it worked.
    await ctx.send(f"Added {twitch_name} for {ctx.author} to the notifications list.")

print('Server Running')
bot.run(os.getenv('token'))

Я хочу написать «$ add_twitch turb4ik», и бот сохраняет стример turb4ik в streamers.json и проверяет, активен ли стример или нет. Если True, отправьте уведомление в определенный канал. Но вроде не работает.

И я получаю эту синтаксическую ошибку:

Unhandled exception in internal background task 'live_notifs_loop'.
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "main.py", line 62, in live_notifs_loop
    streamers = json.loads(file.read())
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "add_twitch" is not found

Я также пробовал этот фрагмент кода, он дает мне информацию о канале, но не дает мне статуса стримера:

client_id = os.getenv('client_id')
oauth_token = os.getenv('Dweller_token')

twitch = Twitch(client_id, oauth_token)
twitch.authenticate_app([])

user_info = twitch.get_users(logins=['turb4ik'])
user_id = user_info['data'][0]['id']

print(user_info)

И есть еще одна проблема: каждый раз, когда я запускаю своего бота, он говорит, что twitchAPI не установлен, и мне нужно устанавливать его каждый раз, когда я запускаю своего бота. Иногда кажется, что мой бот забывает о twitchAPI, отключается и говорит, что мне снова нужно установить twitchAPI.

Я знаю, что это сложно, но, пожалуйста, помогите мне. Может быть, мне стоит сделать это с помощью SQL (библиотека sqlite3) или около того. Большое спасибо!

Изменить: еще одна синтаксическая ошибка:

    Unhandled exception in internal background task 'live_notifs_loop'.
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "main.py", line 78, in live_notifs_loop
    streamers = json.load(file)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Ignoring exception in command addtwitch:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 136, in add_twitch
    streamers = json.load(file)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

person Dweller    schedule 08.05.2021    source источник


Ответы (1)


Unhandled exception in internal background task 'live_notifs_loop'.
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "main.py", line 62, in live_notifs_loop
    streamers = json.loads(file.read())
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Вместо streamers = json.loads(file.read()) используйте streamers = json.load(file)

discord.ext.commands.errors.CommandNotFound: Command "add_twitch" is not found

Поскольку вы определяете свою команду с помощью name=addtwitch agrument, вы можете вызывать свою команду только с $addtwitch user. Чтобы этого избежать, добавьте аргумент aliases =['add_twitch'] в

@bot.command(name='addtwitch', help='Adds your Twitch to the live notifs.', pass_context=True, aliases =['add_twitch'])


person Chuaat    schedule 08.05.2021
comment
Спасибо, но у меня другая синтаксическая ошибка. Я отредактировал свой вопрос выше. - person Dweller; 08.05.2021
comment
Я отредактировал свой вопрос с новой синтаксической ошибкой ниже, вы можете взглянуть? - person Dweller; 08.05.2021
comment
Это потому, что вы не редактировали streamers = json.loads(file.read()) в streamers = json.load(file) @Dweller - person Chuaat; 08.05.2021
comment
Извините. Я сменил стримеры async def live_notifs_loop(): и в async def add_twitch(ctx, twitch_name):, но все равно получаю ошибки - person Dweller; 08.05.2021
comment
я понял спасибо - person Dweller; 08.05.2021