如何在不使用 python 的情况下退出不和谐机器人'except'



需要快速帮助这是代码,只是发布到一个不和谐的渠道,我使用它作为我的项目的一部分,我想从这个帖子和关闭(不杀死程序),所以我不能使用'退出',我已经使用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)

如果你真的想在bot完成你想让它做的事情后停止它的运行,你要找的是discord.Client.logout()。在您的场景中,使用bot.logout()

尽管有人回复你的帖子说,webhook可能更适合你的用例。使用requests发送消息的简单webhook看起来像这样:

import requests

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

Discord的相关文档可以在这里找到,这个路由的显著限制是你不能附加文件,但如果你只需要发送一张图片和一些文本,一个。png链接可能就足够了,因为Discord会将链接隐藏在消息中。

最新更新