首页 > 解决方案 > Discord bot 停止发送消息,但不会引发错误或崩溃

问题描述

我一直在使用 pycharm 在 python 中开发一个不和谐的机器人,它创建一个 webhook,然后使用该 webhook 发送一条消息。它工作得很好,然后停止发送消息。我创建了一个日志来查看它是否甚至没有进入 on_message 事件,但它仍在进入它。我注意到如果我停止机器人更长时间,或者重新启动我的计算机,它通常会再次工作,但我无法找出它发生的原因或合法的解决方案。我将把代码放在下面,任何帮助将不胜感激。

import discord

TOKEN = 'Was not sure if I should put this on stack'

client = discord.Client()


@client.event
async def on_ready():
    print('{0.user} is locked and loaded'.format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')

    hook = await message.channel.create_webhook(name="HookTest7033")

    if message.author == client.user:
        return

    if user_message.lower() == '!hooktest':
        await hook.send("Hello")
        await hook.send("Bye")
        hooks = await message.channel.webhooks()
        for webhook in hooks:
            await webhook.delete()
        return

    if message.author == await client.fetch_user(User ID goes here):
        await hook.send("Test response")
        hooks = await message.channel.webhooks()
        for webhook in hooks:
            await webhook.delete()
        return

    if message.author == await client.fetch_user(User ID goes here):
        await hook.send("Test response")
        hooks = await message.channel.webhooks()
        for webhook in hooks:
            await webhook.delete()
        return


client.run(TOKEN)

标签: pythondiscord.py

解决方案


推荐阅读