English 中文(简体)
我怎么能够核对信息,就不提及或不使用不和。 y
原标题:How can I check a message does it contain mention or not using discord.py

我正在用不和的手法制造一个新的不和谐的机器人,我希望与使用我的机器人的人交往。 例如,我将打上“/mention @myfirend”的字句,然后由我的机器人回答“Lexspe提到@myLG”。 但是,如果没有提及的话,机器人将回答“你应当提及”的问题。

我没有这样的想法,因此我要求翻案,并说,如果你想要用刀子来做,你应该使用不同的图书馆,但我对其他图书馆没有想法,因此我再次问,复印机开始提供 st的回答,其中有许多错误,我想在这里问你。

此外,我还搜索了许多论坛,但所有论坛都不是关于闪电的论坛,因此我仍然不知道如何核对信息。

我的守则如下:

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")

我做了一些这样的事情,但正如我所说的那样,当有人使用“/选择”时,我怎么能够核对信息?

问题回答

You could just typehint kimi as a discord.Member object and if the argument is not provided the bot answers with "You should mention someone", otherwise the bot will mention the provided 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}")

我无法对此进行测试,因此我可以肯定会这样做,但我想到的是:

@bot.tree.command(name = "ping")
@app_commands.describe(kimi = "say something")
async def say(interaction: discord.Interaction):
    if interaction.message.mentions: # checks that the list of mentions is not empty
        toSend = f" mentionned {interaction.message.mentions[0]}"
    else:
        toSend = " did not manage to mention anyone"
    await interaction.response.send_message(f"{interaction.user.mention} {toSend}")

为了检查引发激烈指挥的信息的内容是否含有提及的内容,你需要检索互动的归属信息。 然后,你可以使用该电文的属性,看看该电文是否有任何有效的提及。

Interaction Object Documentation

不要忘记该成员的物体,如@boez的话,你检查了即将到来的参数,看看它是否有有效的用户支持方。 例如:

<@user-id>

以上是适当的@(有些人)应该看。 如果我们把用户带在单版上,我们就可以这样做。 如果你想把某些用户排除在外,这样做也是有益的。

当我们用以下法典印刷上述文字时,我们就可以看到这一点:

@tree.command(name="say", guilds=[discord.Object(id=no)])
@app_commands.describe(user="Input valid user @")
async def say(interaction: discord.Interaction, user: str):
    print(user)  # print the user
    await interaction.response.send_message(f"{interaction.user.mention} mentioned {user}")

这一回报:

<@497930397985013781>

如果我们编辑该守则以检查该身份证是否有效:

@tree.command(name="say", guilds=[discord.Object(id=no)])
@app_commands.describe(user="Input valid user @")
async def say(interaction: discord.Interaction, user: str):
    user_id = re.sub("[^0-9]", "", user)  # remove all non characters
    if user_id.isdigit():  # check if it s a digit
        if interaction.guild.get_member(int(user_id)) is not None:  # String splice the mention to get the ID and pass it to get_member
            return await interaction.response.send_message(f"{interaction.user.mention} mentioned {user}")

    await interaction.response.send_message(f"Not a mention")

以上使用<条码>re,但仅凭<条码>进口<>。 如果你不希望利用,你就只会使扼杀。

产出:

“Outputs”/

我对我说真话时,我发出最崇高的信息。 底线是我发出非@讯息。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签