Discord.py中Command类rest_is_raw属性的放置位置咨询
解决discord.py中rest_is_raw属性的放置问题
要让带*参数的test_2命令仅接收第一个参数,你需要将rest_is_raw=True作为参数传递给@bot.command()装饰器,这是该属性的正确放置位置。设置这个属性后,即便命令定义里用了*,也只会捕获第一个空格分隔的参数,和test_1的行为完全一致。
修改后的test_2命令代码:
# Example command 2 @bot.command(rest_is_raw=True) async def test_2(ctx, *, arg): await ctx.send(arg)
完整的修改后代码:
import discord from discord.ext import commands bot = commands.Bot(command_prefix= '!', intents=discord.Intents.all()) # Example command 1 @bot.command() async def test_1(ctx, arg): await ctx.send(arg) # Example command 2 - 添加rest_is_raw=True到装饰器 @bot.command(rest_is_raw=True) async def test_2(ctx, *, arg): await ctx.send(arg) #Ping-pong @bot.command() async def ping(ctx): await ctx.send('pong') @bot.event async def on_ready(): await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="!help")) print('My bot is ready') bot.run('mytokenissecret')
此时发送!test_2 hello world,机器人会回复hello,和!test_1 hello world的输出完全一致。
内容的提问来源于stack exchange,提问作者galileo
相关产品推荐
相关产品推荐

