小编sha*_*312的帖子

如何在discord.py中循环任务

我正在尝试制作我自己的小不和谐机器人,它可以从 Twitch 获取信息,但我对如何使机器人循环并检查条件感到困惑。

我希望机器人每隔几秒钟循环一段代码,以检查指定的 twitch 频道是否处于活动状态。


代码

import discord
from discord.ext import commands, tasks
from twitch import TwitchClient
from pprint import pformat


client = TwitchClient(client_id='<twitch token>')

bot = commands.Bot(command_prefix='$')

@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(bot))

@bot.command()
async def info(ctx, username):
    response = await ctx.send("Querying twitch database...")
    try:
        users = client.users.translate_usernames_to_ids(username)
        for user in users:
            print(user.id)
            userid = user.id
        twitchinfo = client.users.get_by_id(userid)
        status = client.streams.get_stream_by_user(userid)
        if status == None:
            print("Not live")
            livestat = twitchinfo.display_name + "is not …
Run Code Online (Sandbox Code Playgroud)

python-3.x twitch discord twitch-api

5
推荐指数
3
解决办法
2万
查看次数

标签 统计

discord ×1

python-3.x ×1

twitch ×1

twitch-api ×1