
本文将指导你如何使用 Discord.py 库创建一个简单的回声机器人。该机器人可以通过特定命令启动,并开始重复用户发送的消息,直到用户发出停止命令或超时。我们将通过设置全局变量、使用 on_message 事件和 bot.wait_for 函数来实现此功能,并提供代码示例和注意事项。
设置全局变量:
首先,我们需要一个全局变量来控制机器人的回声状态。我们将使用一个布尔变量 boolean,其初始值为 False,表示回声功能关闭。当用户启动回声功能时,将其设置为 True。
boolean = False
监听消息事件 (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)
else:
pass代码解释:
创建回声命令 (echo command):
使用 bot.tree.command 创建一个命令,用于启动和停止回声功能。
@bot.tree.command(name="echo")
async def echo(interaction: discord.Interaction):
global boolean
boolean = True
channel = interaction.channel
await interaction.response.send_message('Bot will start echoing. Type "k!echo" to stop.')
async def check_stop(msg):
return msg.content == "k!echo" and msg.author.id == interaction.user.id
try:
while True:
response = await bot.wait_for("message", check=check_stop, timeout=60.0)
await channel.send(response.content)
break
except asyncio.TimeoutError:
await interaction.response.send_message('Echoing stopped due to inactivity.')代码解释:
import discord
from discord.ext import commands
import asyncio
# 替换为你的机器人 token
TOKEN = 'YOUR_BOT_TOKEN'
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
boolean = False
@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)
else:
pass
await bot.process_commands(message) # 确保其他命令也能正常工作
@bot.tree.command(name="echo")
async def echo(interaction: discord.Interaction):
global boolean
boolean = True
channel = interaction.channel
await interaction.response.send_message('Bot will start echoing. Type "k!echo" to stop.')
async def check_stop(msg):
return msg.content == "k!echo" and msg.author.id == interaction.user.id
try:
while True:
response = await bot.wait_for("message", check=check_stop, timeout=60.0)
await channel.send(response.content)
break
except asyncio.TimeoutError:
await interaction.response.send_message('Echoing stopped due to inactivity.')
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
except Exception as e:
print(e)
bot.run(TOKEN)注意事项:
通过使用全局变量、on_message 事件和 bot.wait_for 函数,我们成功创建了一个基于 Discord.py 的回声机器人。该机器人可以通过特定命令启动,并重复用户发送的消息,直到用户发出停止命令或超时。 这个教程提供了一个基础框架,你可以根据自己的需求进行扩展和修改,例如添加更多的命令、自定义回声行为等。
以上就是创建一个基于 Discord.py 的回声机器人的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号