![](/img/trans.png)
[英]Receiving a runtime error at client.run('token') when running my discord.py bot
[英]client.run("TOKEN") blocking issue (python & discord.py)
我知道 client.run("TOKEN") 会阻止它后面的任何代码......我正在尝试让我的程序打印一条消息(在我的情况下,当耐克产品重新进货时),然后继续运行并打印另一条消息当它再次拿起东西时。 如何关闭/关闭我的 client.run("TOKEN") 命令,以便我的程序可以继续运行?
else:
print("NO RESTOCK FOUND...STILL MONITORING")
client = commands.Bot(command_prefix = "!")
channel = client.get_channel(ABCDEFG)
await channel.send("NO RESTOCK FOUND...STILL MONITORING")
client.run("TOKEN")
time.sleep(0.5)
client.close()
您可以导入线程并使用 class 线程,基本上制作一个 function 如下:
def runClient():
client.run("TOKEN")
在您希望运行 client.run("TOKEN") 的那部分中,只需执行以下操作: threading.Thread(target=runClient).start() 这将创建一个线程,该线程将运行 function 并将继续执行rest 之后是 function,希望这有帮助!
您可以使用await client.start()
client.run()
而不是使用 client.run() ,它不会阻塞代码。
import asyncio
async def client_start():
await client.start('token.goes.here')
然后,您可以在 function 中调用await client.start()
。
要发送消息,请使用message.channel.send('Message')
或ctx.send('Message')
,具体取决于您使用的是事件还是命令。
我有一个机器人需要随机发送信号,有时我需要在 client.run 行下方发送消息,所以我找不到任何解决方案,这是我发现在任何地方发送消息的最佳方式任何时候的代码,使用基本的 URL 请求方法:
您需要在此代码中更改的是:
1/第 2 行(您的频道 ID )
2/ 第 4 行(您的频道令牌)或(如果尝试作为机器人发送,机器人令牌)
3/ the_message = '你想发送的任何消息'
import requests
the_url = 'https://discord.com/api/v9/channels/your_channel_id/messages'
def send(the_message):
TOKEN = 'Your_Bot_Token'
header = {
'authorization': f'Bot {TOKEN}',
}
payload = {
'content': f'{the_message}',
'author': {
'pinned': True
}
}
requests.post(the_url, data=payload, headers=header)
######### Calling the Function to send a message in any place in the code ^_^ #########
send(the_message='Hola')
你可以在代码中的任何地方调用send function ^_^。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.