You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用discord.py编写selfbot启动时向指定guild频道发slash command?

解决discord.py selfbot指定服务器频道发送消息及Slash命令的问题

注意:Discord官方服务条款明确禁止使用selfbot,使用该工具可能导致你的账号被永久封禁,以下内容仅作技术学习参考,请勿用于违反Discord规则的场景。

一、指定Guild(服务器)和频道发送消息

你可以通过ID直接定位目标服务器和频道,具体实现如下:

获取频道的两种方式

  • 方式1:先获取服务器,再从服务器内筛选频道
guild = bot.get_guild(你的服务器ID)
if guild:
    channel = guild.get_channel(你的频道ID)
  • 方式2:直接通过频道ID获取(频道ID为全局唯一标识,无需先指定服务器)
channel = bot.get_channel(你的频道ID)

启动时发送消息的完整示例

import discord
from discord.ext import commands

# 启用selfbot模式
bot = commands.Bot(command_prefix="任意前缀", self_bot=True)

@bot.event
async def on_ready():
    print(f"已登录:{bot.user.name}")
    # 替换为你的目标频道ID
    target_channel = bot.get_channel(123456789012345678)
    
    if not target_channel:
        print("无法找到目标频道")
        return
    
    # 发送普通文本消息
    await target_channel.send("selfbot启动测试消息")

# 替换为你的用户Token(绝对不要公开分享Token)
bot.run("YOUR_USER_ACCOUNT_TOKEN")

二、发送Slash命令的可靠方法

由于selfbot属于非官方使用场景,没有官方支持的Slash命令调用接口,最直接且稳定的方式是模拟用户手动输入的格式:

直接发送Slash命令文本

获取到目标频道后,直接发送带/前缀的完整命令文本即可,比如要触发/ping命令:

await target_channel.send("/ping")

如果命令需要参数,直接按照Discord内的格式补充即可,例如/say content:hello world。

注意事项

  • 必须确保Slash命令的格式完全正确(包括参数名称、空格分隔等),否则无法触发命令
  • 需确认你的账号在目标频道拥有使用该Slash命令的权限

内容的提问来源于stack exchange,提问作者James Wyelengins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 04:18:23