【问题标题】:Send Message Directly to Discord Client Inside Function直接向 Discord 客户端内部函数发送消息
【发布时间】:2021-05-15 20:35:21
【问题描述】:

我试图让我的代码检查两个字符串在列表中是否相等。如果它们不相等,则应直接向不和谐客户端发送消息。如果是,它应该继续使用该功能。我收到一条错误消息,提示“NoneType”对象没有属性发送。如果我复制这些行:

channel = bot.get_channel(123456789)
    await channel.send("Message")

只需删除 await 并将其粘贴到函数之外,我不会收到错误消息。我假设我的全局声明有问题,但我不确定。有人可以帮忙吗?

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

# instantiating discord client
token = "redacted"
client = discord.Client()


@tasks.loop(seconds=5.0)
async def scrape():
    global linksAndTitles
    global pbe_titles
    global currPatch
    global allTitles
    global recentPatches
    global currTemp
    temp = []

    URL = "redacted"
    page = urlopen(URL)
    soup = BeautifulSoup(page, 'html.parser')
    pbe_titles = soup.find_all('redacted', attrs={'redacted': 'redacted'})
    for tags in pbe_titles:
        temp.append(tags.text.strip())
    tempCurr = '\n'.join(str(line) for line in temp[:1])
    if tempCurr != currTemp:
        channel = bot.get_channel(123456789)
        await channel.send("Message")

正是最后两行给我抛出了错误。

【问题讨论】:

    标签: python function discord discord.py


    【解决方案1】:

    错误'NoneType' object has no attribute 'send' 的意思是您的channel 变量没有从bot.get_channel(123456789) 获取任何通道,这就是channel 的值变为None 的原因。我在代码方面没有看到任何错误,因此问题一定出在您传入的频道 ID 上。确保正确复制所需频道的 ID,然后将其粘贴到 channel = bot.get_channel(id) 行中。

    【讨论】:

    • 这不是频道 ID 的问题。我已经确定那部分是正确的。 channel.send 似乎直接存在问题,因为它无法识别频道是什么。这就是我收集到的。
    • 所以我的代码在循环开始时有效:if tempCurr == currTemp: 操作数是 == 但由于某种原因,当 != 是操作数时会中断
    【解决方案2】:

    您的问题似乎是botclient 之间的混合,一个表明这一点的示例是,您已经在代码的开头定义了两者,但是它们不能作为一对很好地工作。你应该坚持一个,这就是为什么它不能读取 id,因为 bot 只是 bot = commands.Bot(command_prefix='!') 它不连接到 discord.client 你的客户端被定义为 client = discord.Client()

    这是出现问题的行,

    如果您查看代码顶部,您会发现 bot 仅定义为 command_prefix='!'

    channel = bot.get_channel(123456789)
    

    所以改为使用一个,在本例中为 client,它们都应该像这样定义,

    client = commands.Bot(command_prefix = '!')
    

    这意味着您的代码的其余部分应该使用client,它包含在您的代码中,

    token = "redacted"
    client = commands.Bot(command_prefix = '!')
    
    
    @tasks.loop(seconds=5.0)
    async def scrape():
        global linksAndTitles
        global pbe_titles
        global currPatch
        global allTitles
        global recentPatches
        global currTemp
        temp = []
    
        URL = "redacted"
        page = urlopen(URL)
        soup = BeautifulSoup(page, 'html.parser')
        pbe_titles = soup.find_all('redacted', attrs={'redacted': 'redacted'})
        for tags in pbe_titles:
            temp.append(tags.text.strip())
        tempCurr = '\n'.join(str(line) for line in temp[:1])
        if tempCurr != currTemp:
            channel = client.get_channel(123456789)
            await channel.send("Message")
    

    但是,如果您仍想使用bot,请记住坚持使用它,而您的代码中不需要使用相反的方法。希望这对你有用。还要确保您在代码底部正确运行机器人令牌

    【讨论】:

    • 因此,出于某种原因,我的问题实际上与我的循环有关:如果我将循环更改为 if tempCurr == currTemp,则不会中断。代码继续并正常工作。但是,如果我保留if tempCurr != currTemp,代码就会中断。
    猜你喜欢
    • 2015-03-28
    • 2017-05-16
    • 2020-07-10
    • 2012-11-02
    • 2018-05-16
    • 1970-01-01
    • 2015-04-07
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多