如何实现Discord Bot在用户输入'verify'时自动发送消息?
基础功能实现示例(discord.py v2.x)
1. 基础机器人框架
先确保安装最新版discord.py:
pip install discord.py
最基础的机器人启动代码:
import discord from discord.ext import commands # 配置机器人权限,必须开启消息内容权限才能读取消息 intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print(f'已登录为 {bot.user}') # 替换成你的机器人TOKEN bot.run('YOUR_BOT_TOKEN')
2. 自定义命令实现
创建一个触发后回复用户的命令:
@bot.command() async def hello(ctx): await ctx.send(f'你好,{ctx.author.mention}!')
在频道发送!hello即可触发该命令。
3. 监听消息自动回复
监听包含特定关键词的消息并自动响应:
@bot.event async def on_message(message): # 避免机器人回复自己的消息 if message.author == bot.user: return if '打招呼' in message.content: await message.channel.send(f'嗨,{message.author.name}!') # 必须保留这一行,否则自定义命令会失效 await bot.process_commands(message)
如果以上示例不是你需要的功能,请补充说明具体要实现的功能细节(比如管理类命令、语音频道操作、嵌入消息生成等),我会给出针对性的实现方案。
内容的提问来源于stack exchange,提问作者Slaughter
相关产品推荐
相关产品推荐

