Discord bot不会将消息发送到频道

时间:2020-09-23 18:30:48

标签: python web-scraping discord

如果满足某些条件,我试图使我的机器人将消息发送到不和谐的频道,但是我似乎无法使代码正常工作。该代码每5秒检查一次列表中是否包含字符串“ .12”。然后应该转发邮件。

   - name: Validar Directorio
     stat:
       path: /tmp/Sabana
     register: sabana_directorio
   
   - debug:
       msg: "Existe"
     when: sabana_directorio.stat.isdir == sabana_directorio.stat.isdir

   - name: Crear el directorio si no existe.
     file:
       path: /tmp/Sabana
       state: directory
     when: sabana_directorio.stat.exists == false

1 个答案:

答案 0 :(得分:0)

这是一个似乎正在运行的机器人脚本。没有您要搜索的网址,我无法完全帮助您,但请尝试一下,看看它是否对您有用:

import discord
import requests
from bs4 import BeautifulSoup
import asyncio

client = discord.Client()

@client.event
async def on_ready():

    # Create a task and run check_html and feed it a parameter of 5 seconds
    client.loop.create_task(check_html(5))
    print("Bot is active")


async def check_html(time):
    while True:
        url = 'url here'
        res = requests.get(url)
        html = res.text
        soup = BeautifulSoup(html, 'html.parser')
        html_element = soup.find_all( 'td', { "class" : "eksam-ajad-aeg" } )

        ret = []

        for t in html_element:
            ret.append(t.text)
        print(ret)
        if '.12.' in ret:
            for guild in client.guilds:
                for channel in guild.channels:
                    if channel.id == 758088198852182037:
                        await channel.send('message')

        # Asyncronously sleep for 'time' seconds
        await asyncio.sleep(time)

client.run('token')