[英]Discord bot with discord.py
上下文是我想用discord.py制作一个 discord 机器人,在阅读其文档时,它说我可以将属性放入类中,但我不知道将它们放在哪里。 class discord.ext.commands.Command(func, **kwargs)
@discord.ext.commands.command(name=..., cls=..., **attrs)
如果我输入!test_1 hello world它只返回hello ,但是如果我输入!test_1 "hello world"它返回hello world 。 !test_2 命令使得不需要使用引号,所以!test_2 this is an example返回this is an example 。
根据文档,使用 rest_is_raw 属性我可以使 test_2 表现得像 test_1 并且只接受第一个参数。
所以我的问题是我不知道将属性放在哪里。
我的代码:`
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
@bot.command()
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_1(ctx, arg)
和test_2(ctx, *, arg)
是完全不同的。 因为,在test_2
中,你传递了你键入的任何内容,并在 Discord 中发送。使用*
让你传递所有参数。 通常如果没有*
,1 个参数只能接受 1 个单词。 通过使用*
,它可以接受您发送的所有单词/句子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.