若要创建一个只回应发起人的 Discord 机器人,你可以使用 Discord.py 库来实现。下面是一个示例代码,它只会回应发起人的消息:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.messages = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(message):
# 只回应发起人的消息
if message.author == bot.user:
return
# 发起人的 ID
owner_id = 1234567890
if message.author.id == owner_id:
await message.channel.send('你是我的主人!')
else:
await message.channel.send('你不是我的主人!')
bot.run('YOUR_BOT_TOKEN')
在这个示例中,我们创建了一个 bot
对象并设置了命令前缀为 !
。on_message
事件处理函数会在接收到新消息时触发。我们检查消息的发起人是否是机器人自身,如果是,则忽略该消息。接下来,我们定义了一个 owner_id
变量,它是发起人的 Discord 用户 ID。你需要将 owner_id
设置为你自己的 Discord 用户 ID。如果发起人的 ID 与 owner_id
相同,机器人将回应“你是我的主人!”;否则,机器人将回应“你不是我的主人!”。
最后,你需要将 'YOUR_BOT_TOKEN'
替换为你自己的 Discord 机器人的令牌。你可以在 Discord 开发者门户中创建一个新的机器人,并获得一个令牌。
确保在运行代码之前,你已经安装了 discord.py
库,并将示例代码中的相关部分替换为你自己的信息。