(Python)如何使用discord.py从不和谐的消息中获取变量?

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

我正在写一个简单的discord bot,很有趣,我希望它从特定短语后面的消息中获取(例如)一个数字,以后再使用例如,某个用户键入“ $ test 6”,我希望我的机器人将这个数字(6)用作其他示例,然后将其发送到同一频道。

我在创建此问题以拆分message.content后出现能行吗(不算用户可以写数字以外的内容)

import discord

client = discord.Client()
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith("$test"):
        stringOne = str(message.content)
        stringTwo = "$test "
        split = stringOne.partition(stringTwo)[2]
        await message.channel.send("number/s from your message: " + split)

python discord.py
1个回答
0
投票

这可以通过命令参数来完成:

@bot.command()
async def test(ctx, number = None):
    if not number:
        await ctx.send("Please enter a number, for example: `$test 5`")
    else:
        await ctx.send(f"Here's the number you gave me: {number}")

以及使用不和谐对象的示例:

@bot.command()
async def kick(ctx, member: discord.Member = None, *, reason = "Default reason"):
    if not member:
        await ctx.send("Please provide a member for me to kick!")
    else:
        await member.kick(reason=reason)
        await ctx.send(f"Successfully kicked {member} for `{reason}`")

参考:

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