如何使用discord.py检查消息是否包含提及或未提及

问题描述 投票:0回答:1

我正在使用discord.py 制作一个新的discord 机器人,我想使用我的机器人与其他人进行交互。例如,我将输入“/提及@myfirend”,然后我的机器人将回复“Lexanspe提到@myfriend!”。但如果没有提及,机器人会回复“你应该提及某人!”。

我没有任何想法,所以我问了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”时,我如何检查消息?

python discord discord.py
1个回答
0
投票

首先,“/”命令是一种特殊类型的命令,属于交互类型。它们与您发送的代码中以您定义的令牌开头的特定于您的机器人的“!”不同。 要检查是否触发 / 命令的消息内容,您需要检索交互的消息属性。然后您可以使用消息的属性 Mentions 来查看其中是否有任何有效的提及。 请参阅交互对象的文档消息对象的文档提及对象的文档

© www.soinside.com 2019 - 2024. All rights reserved.