
本文将指导你如何使用discord.py库创建一个回声机器人。该机器人会在收到特定命令后开始重复用户发送的消息,直到用户再次发送命令停止。我们将使用全局变量控制机器人的开启和关闭状态,并加入超时处理机制,以防止机器人长时间无响应。
要实现一个回声机器人,我们需要监听Discord服务器上的消息,并在特定条件下将消息内容发送回相同的频道。以下是具体步骤和代码示例:
初始化机器人和全局变量
首先,我们需要初始化Discord机器人,并定义一个全局变量boolean来控制回声功能的开启和关闭。
import discord from discord.ext import commands import asyncio # 替换为你的机器人令牌 TOKEN = 'YOUR_BOT_TOKEN' intents = discord.Intents.default() intents.message_content = True # 启用消息内容意图 bot = commands.Bot(command_prefix='k!', intents=intents) boolean = False # 全局变量,控制回声功能
请确保替换YOUR_BOT_TOKEN为你的实际机器人令牌,并且启用了message_content intent,否则机器人将无法读取消息内容。
on_message 事件处理
接下来,我们定义on_message事件处理函数。此函数会在机器人接收到任何消息时被调用。我们检查全局变量boolean的状态,如果为True,则将消息内容发送回消息所在的频道。
@bot.event
async def on_message(message: discord.Message):
global boolean
if boolean:
if message.author.bot: # 忽略机器人自身发送的消息
return
if message.content == "k!echo": # 停止回声的命令
boolean = False
return
if isinstance(message.channel, discord.TextChannel): # 确保消息来自文本频道
await message.channel.send(message.content)
await bot.process_commands(message) # 确保其他命令也能被处理这段代码首先检查boolean是否为True,如果是,则忽略机器人自身发送的消息,并检查消息内容是否为停止回声的命令k!echo。如果不是停止命令,则将消息内容发送回相同的频道。await bot.process_commands(message)这行代码确保了即使在on_message事件中,其他命令也能正常被处理。
echo 命令
现在,我们定义一个命令echo来启动回声功能。
@bot.command(name="echo")
async def echo(ctx):
global boolean
boolean = True
channel = ctx.channel
await ctx.send('Bot will start echoing. Type "k!echo" to stop.')
def check_stop(msg):
return msg.content == "k!echo" and msg.author.id == ctx.author.id and msg.channel == channel
try:
response = await bot.wait_for("message", check=check_stop, timeout=60.0)
await channel.send(response.content) #发送停止命令
boolean = False #停止回声
except asyncio.TimeoutError:
boolean = False
await ctx.send('Echoing stopped due to inactivity.')当用户输入k!echo命令时,boolean被设置为True,机器人开始回声。bot.wait_for函数等待用户输入k!echo命令来停止回声。如果60秒内没有收到停止命令,则会触发asyncio.TimeoutError,机器人自动停止回声。
运行机器人
最后,使用以下代码运行机器人:
bot.run(TOKEN)
import discord
from discord.ext import commands
import asyncio
# 替换为你的机器人令牌
TOKEN = 'YOUR_BOT_TOKEN'
intents = discord.Intents.default()
intents.message_content = True # 启用消息内容意图
bot = commands.Bot(command_prefix='k!', intents=intents)
boolean = False # 全局变量,控制回声功能
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(message: discord.Message):
global boolean
if boolean:
if message.author.bot: # 忽略机器人自身发送的消息
return
if message.content == "k!echo": # 停止回声的命令
boolean = False
return
if isinstance(message.channel, discord.TextChannel): # 确保消息来自文本频道
await message.channel.send(message.content)
await bot.process_commands(message) # 确保其他命令也能被处理
@bot.command(name="echo")
async def echo(ctx):
global boolean
boolean = True
channel = ctx.channel
await ctx.send('Bot will start echoing. Type "k!echo" to stop.')
def check_stop(msg):
return msg.content == "k!echo" and msg.author.id == ctx.author.id and msg.channel == channel
try:
response = await bot.wait_for("message", check=check_stop, timeout=60.0)
await channel.send(response.content) #发送停止命令
boolean = False #停止回声
except asyncio.TimeoutError:
boolean = False
await ctx.send('Echoing stopped due to inactivity.')
bot.run(TOKEN)通过以上步骤,你已经成功创建了一个简单的Discord回声机器人。这个机器人可以根据用户的命令开启和关闭回声功能,并在一段时间没有活动后自动停止。你可以根据自己的需求,进一步扩展和改进这个机器人,例如添加更多的命令、自定义回声消息的格式等。
以上就是创建一个Discord.py回声机器人:命令开启与停止的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号