【问题标题】:How to quit a discord bot without using 'except' python如何在不使用“除外”python的情况下退出不和谐机器人
【发布时间】:2021-11-08 08:57:54
【问题描述】:

需要快速帮助 这是仅发布到不和谐频道的代码',我已经使用了 while 循环和所有这些,但没有任何效果。

def post_discord():
    print('POSTING NEW OFFER TO DISCORD')
    token = 'token here'
    while True:
        bot = commands.Bot(command_prefix='!')
        @bot.command()
        async def on_ready():

            await bot.wait_until_ready()
            channel = bot.get_channel(12345678) # replace with channel ID that you want to send to
                # await channel.send()
            await channel.send(text,file =discord.File('promo.png'))

            await asyncio.sleep(2)

            # exit()
            # await bot.close()





        bot.loop.create_task(on_ready())

        bot.run(token)
        time.sleep(3)

【问题讨论】:

  • 我建议改用 Discord webhook。详细答案见here

标签: python discord.py


【解决方案1】:

如果您真的只想在机器人完成您希望它做的任何事情后停止运行,那么您正在寻找的是discord.Client.logout()。在您的场景中,用法为bot.logout()

尽管正如回复您帖子的人所说,webhook 可能更适合您的用例。使用 requests 发送消息的简单 webhook 如下所示:

import requests


data = {"content": "Message Content"}
requests.post("Webhook URL", data=data)

Discord 的相关文档可以在 here 找到,这条路线的显着限制是您无法附加文件,但如果您只需要发送图像和一些文本,则可以使用 .png 链接足够了,因为 Discord 将隐藏消息中的链接。

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2021-03-05
    • 2021-06-09
    • 2021-05-24
    • 2018-12-04
    • 2018-08-22
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    相关资源
    最近更新 更多