如何用discord.py实现Discord服务器Boost消息推送功能?
实现Discord服务器Boost通知机器人(discord.py)
核心思路
当用户为服务器Boost时,其账号的premium_since属性会从None变为具体时间戳,我们可以通过监听on_member_update事件捕获这个变化,然后在指定频道发送通知。
步骤与代码实现
- 安装依赖
确保安装最新版discord.py:
pip install discord.py
- 基础机器人框架
搭建基础结构,替换为你的机器人Token:
import discord from discord.ext import commands intents = discord.Intents.default() # 必须开启成员意图才能监听成员更新事件 intents.members = True intents.guilds = True bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print(f"已登录为 {bot.user}") # 后续添加Boost监听逻辑 bot.run("你的机器人Token")
- 监听Boost事件并发送通知
添加事件处理函数,判断成员是否触发Boost行为:
@bot.event async def on_member_update(before, after): # 检测成员是否刚完成Boost:之前无Boost记录,现在有了 if before.premium_since is None and after.premium_since is not None: # 替换为你要发送通知的频道ID(整数类型) notify_channel_id = 123456789012345678 channel = bot.get_channel(notify_channel_id) if channel: # 自定义通知消息,可按需调整格式 embed = discord.Embed( title="🎉 感谢服务器Boost!", description=f"感谢 {after.mention} 为服务器Boost!服务器等级又迈进一步啦!", color=discord.Color.purple() ) embed.set_thumbnail(url=after.avatar.url if after.avatar else after.default_avatar.url) await channel.send(embed=embed)
关键注意事项
- 开启成员意图:在Discord开发者门户的机器人页面,必须开启"Privileged Gateway Intents"下的"Server Members Intent",否则机器人无法监听成员更新事件。
- 获取频道ID:开启Discord开发者模式(用户设置→高级→开发者模式),右键目标频道选择"复制ID",替换代码中的
notify_channel_id。 - 权限要求:机器人需要在目标频道拥有"发送消息"和"嵌入链接"权限,才能正常发送通知。
内容的提问来源于stack exchange,提问作者Kaze
相关产品推荐
相关产品推荐

