微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何知道 Twitch Streamer 是否已上线并发送相关消息?不和谐.py

如何解决如何知道 Twitch Streamer 是否已上线并发送相关消息?不和谐.py

我现在正在编写 discord bot,所以我想知道如何将主播的名称保存在单独的文件中,以便当他上线时,bot 会在特定频道中发送有关该消息的消息。

这是我试过的:

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。

我知道这很难,但请帮助我。也许我应该做女巫 sqlsqlite3 库)左右。非常感谢!

编辑: 另一个语法错误

    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 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",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,line 136,in add_twitch
    streamers = json.load(file)
  File "/usr/lib/python3.8/json/__init__.py",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)

解决方法

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 参数定义您的命令,因此您只能使用 $addtwitch user 调用您的命令。为避免这种情况,请将 aliases =['add_twitch'] 参数添加到

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


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。