你可以将你的Gradio应用作为Discord机器人提供,让你Discord服务器中的用户直接与之互动。
Discord 机器人将监听频道中提及它的消息。当它收到消息(可以包括文本和文件)时,它将通过 Gradio 的内置 API 将其发送到您的 Gradio 应用程序。您的机器人将使用从 API 收到的响应进行回复。
因为Gradio的API非常灵活,你可以非常容易地创建支持文本、图像、音频、流媒体、聊天历史记录以及各种其他功能的Discord机器人。

gradio和discord.py库:pip install --upgrade gradio discord.py~=2.0现在,我们准备开始了!
首先,前往Discord 应用仪表板。寻找“新建应用”按钮并点击它。为您的应用命名,然后点击“创建”。

在结果屏幕上,您将看到有关应用程序的基本信息。在设置部分下,点击“Bot”选项。如果您愿意,可以更新您的机器人的用户名。
然后点击“重置令牌”按钮。将生成一个新的令牌。复制它,因为我们在下一步中需要它。
向下滚动到“特权网关意图”部分。您的机器人需要某些权限才能正常工作。在本教程中,我们只会使用“消息内容意图”,因此点击切换按钮以启用此意图。保存更改。

让我们从一个非常简单的 Discord 机器人开始编写,以确保一切正常。将以下 Python 代码写入名为 bot.py 的文件中,并粘贴上一步中的 Discord 机器人令牌:
# bot.py
import discord
TOKEN = #PASTE YOUR DISCORD BOT TOKEN HERE
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)现在,运行这个文件:python bot.py,它应该会运行并打印出类似的消息:
We have logged in as GradioPlaygroundBot#1451如果那部分工作正常,我们准备添加Gradio特定的代码。我们将使用Gradio Python Client来查询上面提到的Gradio Playground Space。以下是更新后的bot.py文件:
import discord
from gradio_client import Client, handle_file
import httpx
import os
TOKEN = #PASTE YOUR DISCORD BOT TOKEN HERE
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
gradio_client = Client("abidlabs/gradio-playground-bot")
def download_image(attachment):
response = httpx.get(attachment.url)
image_path = f"./images/{attachment.filename}"
os.makedirs("./images", exist_ok=True)
with open(image_path, "wb") as f:
f.write(response.content)
return image_path
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
# Ignore messages from the bot itself
if message.author == client.user:
return
# Check if the bot is mentioned in the message and reply
if client.user in message.mentions:
# Extract the message content without the bot mention
clean_message = message.content.replace(f"<@{client.user.id}>", "").strip()
# Handle images (only the first image is used)
files = []
if message.attachments:
for attachment in message.attachments:
if any(attachment.filename.lower().endswith(ext) for ext in ['png', 'jpg', 'jpeg', 'gif', 'webp']):
image_path = download_image(attachment)
files.append(handle_file(image_path))
break
# Stream the responses to the channel
for response in gradio_client.submit(
message={"text": clean_message, "files": files},
):
await message.channel.send(response[-1])
client.run(TOKEN)现在我们准备在服务器上安装机器人。返回Discord 应用仪表板。在设置部分,点击“OAuth2”选项。向下滚动到“OAuth2 URL 生成器”框,并勾选“bot”复选框:

然后在弹出的“Bot Permissions”框中,启用以下权限:

复制下面显示的生成的URL,它应该看起来像这样:
https://discord.com/oauth2/authorize?client_id=1319011745452265575&permissions=377957238784&integration_type=0&scope=bot将其粘贴到您的浏览器中,这将允许您将Discord机器人添加到您管理的任何Discord服务器中。
现在你可以从你的Discord服务器中的任何频道提到你的机器人,可以选择附加一张图片,它会用生成的Gradio应用程序代码来回应!
机器人将:
监听提及
处理任何附加的图像
将文本和图像发送到您的Gradio应用程序
将响应流式传输回 Discord 频道
这只是一个基本示例 - 您可以扩展它以处理更多类型的文件,添加错误处理,或与不同的Gradio应用程序集成。

如果你从Gradio应用程序构建了一个Discord机器人,请随时在X上分享并标记Gradio账户,我们很乐意帮助你扩大影响力!