我正在使用discord.py制作一个新的discord机器人,我想用我的机器人与其他人互动.例如,我将输入"/提及@myfiend",然后我的机器人将回复"Lexanspe提及@myFriend!".但如果没有提到,BOT会回复"你应该提到某人!"

我没有任何 idea ,所以我问了Copilot,它说如果你想用斜杠命令,你应该使用不同的库,但我不知道其他库,所以我又问了一遍,Copilot开始给出愚蠢的答案,其中包含很多错误,所以我想在这里问你.

我也搜索了很多,但所有的论坛都不是关于斜杠命令的,所以我仍然不知道如何查看消息.

下面是我的代码:

import discord
from discord import app_commands
from discord.ext import commands

bot = commands.Bot(command_prefix="!", intents=discord.Intents.default(), activity=discord.Game(name='with arda'))

@bot.event
async def on_ready():
    print("started")
    try:
        synced = await bot.tree.sync()
        print(f"Synced {len(synced)} commands(s)")
    except Exception as e:
        print(e)

@bot.tree.command(name = "gizli")
async def sa(interaction: discord.Interaction):
    await interaction.response.send_message(f"as {interaction.user.mention}, slash komudu calisti ezzzzz", ephemeral=True)

@bot.tree.command(name = "ping")
@app_commands.describe(kimi = "say something")
async def say(interaction: discord.Interaction, kimi: str):
    await interaction.response.send_message(f"{interaction.user.mention} mentioned {kimi}")

@bot.tree.command(name = "say")
@app_commands.describe(message = "say something")
async def say(interaction: discord.Interaction, message: str):
    await interaction.response.send_message(f"{interaction.user.mention} said {message}")

bot.run("token")

我做了一些类似的东西,但正如我所说的,当有人使用"/ping"时,我如何判断消息?

推荐答案

您只需将提示kimi作为discord.Member对象,如果没有提供参数,机器人就会回答"你应该提到某人",否则机器人就会提到所提供的成员.

@bot.tree.command(name = "ping")
@app_commands.describe(kimi = "say something")
async def say(interaction: discord.Interaction, kimi: discord.Member=None):
    if kimi is None:
       await interaction.response.send_message("You should mention someone!")
    await interaction.response.send_message(f"{interaction.user.mention} mentioned {kimi.mention}")

Python相关问答推荐

Polars:使用列值引用when / then表达中的其他列

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

具有多个选项的计数_匹配

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

使用新的类型语法正确注释ParamSecdecorator (3.12)

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

如何使用html从excel中提取条件格式规则列表?

用Python解密Java加密文件

Polars:用氨纶的其他部分替换氨纶的部分

数据抓取失败:寻求帮助

Telethon加入私有频道

如何在给定的条件下使numpy数组的计算速度最快?

所有列的滚动标准差,忽略NaN

提取相关行的最快方法—pandas

Numpyro AR(1)均值切换模型抽样不一致性

将一个双框爆炸到另一个双框的范围内

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

如何使用matplotlib查看并列直方图

如何写一个polars birame到DuckDB

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接