您可以使用以下代码示例来按下按钮并使用discord.py库:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} ({bot.user.id})')
print('------')
@bot.command()
async def hello(ctx):
await ctx.send('Hello!')
bot.run('YOUR_DISCORD_BOT_TOKEN')
请注意,您需要在代码中替换YOUR_DISCORD_BOT_TOKEN
为您的Discord机器人的令牌。您可以在Discord开发者门户(https://discord.com/developers/applications)中创建和获取机器人令牌。
此代码示例创建了一个名为bot
的discord.py机器人,并将其命令前缀设置为!
。它还定义了一个简单的命令hello
,当用户发送!hello
命令时,机器人将回复Hello!
。
要运行此代码,您需要安装discord.py库。您可以使用以下命令来安装它:
pip install discord.py
确保您的Python版本是3.5.3或更高版本。
运行代码后,机器人将登录到您的Discord帐户,并准备好响应命令。您可以在Discord服务器上使用!hello
命令来测试机器人的响应。
希望这可以帮助到您!