如何让Midjourney Bot识别requests.post发送的/imagine命令
解决Python发送Midjourney斜杠命令被识别为普通文本的问题
问题原因
Discord的斜杠命令(如/imagine)无法通过普通的发送消息接口(/channels/{channel_id}/messages)触发,这类命令必须使用Discord的交互API发送,且需要构造符合规范的请求Payload,不能单纯将命令字符串放在content字段中。
解决方案
1. 替换请求地址
将原请求URL替换为Discord交互API地址:https://discord.com/api/v9/interactions
2. 构造合规的交互Payload
斜杠命令的Payload需包含固定格式的交互信息,核心字段说明:
type: 固定为2(代表应用命令交互类型)application_id: Midjourney官方Bot的ID(当前为936929561302675456)guild_id: 目标服务器IDchannel_id: 目标频道IDsession_id: 可自定义的任意字符串(如abc123)data: 包含命令名imagine和参数prompt(即提取后的提示词内容)
3. 修改后的完整代码
import time import requests # 配置参数 DISCORD_INTERACTION_URL = "https://discord.com/api/v9/interactions" GUILD_ID = "你的服务器ID" CHANNEL_ID = "目标频道ID" MIDJOURNEY_BOT_ID = "936929561302675456" SESSION_ID = "abc123" # 可自定义任意字符串 header = { "Authorization": "你的Discord用户Token", # 若使用Bot账号则改为"Bot 你的Bot Token" "Content-Type": "application/json" } # 读取提示词文件 time.sleep(0.25) print("Copying Prompts...") file_path = "...\Output.txt" with open(file_path, 'r') as file: prompts = [line.strip() for line in file if line.strip()] # 发送提示词 time.sleep(0.25) print("Sending Prompts...") for prompt in prompts: # 提取/imagine后的提示词内容 if not prompt.startswith('/imagine '): print(f"跳过无效格式提示词:{prompt}") continue prompt_content = prompt.replace('/imagine ', '', 1) # 构造交互请求Payload payload = { "type": 2, "application_id": MIDJOURNEY_BOT_ID, "guild_id": GUILD_ID, "channel_id": CHANNEL_ID, "session_id": SESSION_ID, "data": { "name": "imagine", "options": [ { "type": 3, "name": "prompt", "value": prompt_content } ] } } response = requests.post(DISCORD_INTERACTION_URL, json=payload, headers=header) # 输出请求结果 if response.status_code in (200, 204): print(f"提示词发送成功:{prompt_content[:50]}...") else: print(f"发送失败,状态码:{response.status_code},响应:{response.text}") time.sleep(1.5) print("Prompts Sent")
注意事项
- 权限验证:确保你的Discord Token(用户或Bot)在目标服务器拥有发送消息、使用Midjourney命令的权限
- Token安全:切勿泄露Token,否则他人可操控你的账号或Bot
- 频率限制:Midjourney有请求频率限制,请勿缩短
time.sleep时长,避免触发限制 - Bot ID更新:若Midjourney官方Bot ID变更,需同步修改
MIDJOURNEY_BOT_ID的值
内容的提问来源于stack exchange,提问作者Robby Dean Erdmann
相关产品推荐
相关产品推荐

