discord.py中能否通过一次请求发送两条消息?
一次请求无法发送两条Discord消息的原因及解决办法
Discord的/channels/{channel.id}/messages接口本身不支持一次请求发送多条消息。你代码里把content设为列表,接口只会读取列表的第一个元素,所以只发送了"first message"。
下面是两种可行的解决方式:
发起两次独立的POST请求,分别发送每条消息
@bot.command() async def request(ctx): messages = ['first message', 'second message'] channel_id = ctx.channel.id for msg in messages: requests.post(f'https://discord.com/api/v10/channels/{channel_id}/messages', headers=headers, data={'content': msg})将两条消息用换行符合并成一条内容发送,这样在Discord里会显示为两行
@bot.command() async def request(ctx): content = 'first message\nsecond message' requests.post(f'https://discord.com/api/v10/channels/{ctx.channel.id}/messages', headers=headers, data={'content': content})
需要注意的是,这是Discord API的设计限制——消息创建接口一次只能生成一条消息,和requests库本身没有关系,requests只是如实传递你设置的参数而已。
内容的提问来源于stack exchange,提问作者toxxicity
相关产品推荐
相关产品推荐

