【问题标题】:Message in a specific Discord channel特定 Discord 频道中的消息
【发布时间】:2021-07-06 18:37:57
【问题描述】:

大家好,我正在尝试让机器人在特定频道中自动发送消息。我获取了频道 ID 并将其传递给 if condition (a_string.find ('data: []')! = -1)。但是这段代码给了我这个错误。请参阅输出错误。

附:我正在使用 Replit,Live 是文件名 (Live.py)

from discord.ext import commands
from Naked.toolshed.shell import execute_js, muterun_js
import sys

class Live(commands.Cog):
    def __init__(self,client):
        self.client=client
    
    @commands.Cog.listener()
    async def on_ready(self):
        channel = self.get_channel(828711580434169858)
        response = muterun_js('serverJs.js')
        original_stdout = sys.stdout # Save a reference to the original standard output
        if response.exitcode == 0:
                a_string= str(response.stdout)#stampa in console
                if (a_string.find('data: []') != -1):
                    print("Streamer: Offline ")
                else:
                    print("Streamer: Online")
                    await channel.send('Live Link: https://...link....')
        else:
            sys.stderr.write(response.stderr)

    @commands.command()
    async def Live(self,ctx):
        await ctx.send('')

def setup(client):
    client.add_cog(Live(client))

OUTPUT ERROR:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/Solaris-IA/cogs/Live.py", line 12, in on_ready
    channel = self.get_channel(828711580434169858)
AttributeError: 'Live' object has no attribute 'get_channel'

【问题讨论】:

  • 你试过client.get_channel(ID)吗?我假设你必须使用它。
  • 是的,我试过了。它不起作用。语法错误

标签: python-3.x discord.py


【解决方案1】:

self.client.get_channel,不是self.get_channel,你还没有定义那个函数

channel = self.client.get_channel(828711580434169858)

【讨论】:

  • 你知道如何重复一些代码吗?检查应该每 Xmin 运行一次,否则它只在机器人启动时运行一次。我已经尝试在async def on_ready(self): 内部使用 tim.sleep(5) 进行测试,但它不会重复。或者也许我应该重复我的 serverJs.js 文件?
猜你喜欢
  • 2020-10-01
  • 2023-03-27
  • 2021-10-28
  • 2020-10-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2020-05-18
相关资源
最近更新 更多