我正在使用 Discords API 制作一个 Discord 机器人。我需要在文本中打印某些部分。例如,如果输入命令是!play songname
,它应该打印songname
;如果输入是 !play "query"
,它应该打印 "query"
。
到目前为止,我有以下代码:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
TOKEN = # redacted
client = commands.Bot(command_prefix = '!')
@client.event
async def on_ready():
print("Qaki.us-MB-J&L")
@client.command(pass_content=True)
async def play (ctx):
如何让它打印命令的适当部分?
编辑:
import discord
import time
import spotipy
from discord.ext import commands
from discord.ext.commands import Bot
from spotipy.oauth2 import SpotifyClientCredentials
Word = 'word'
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="ID", client_secret="ID"))
TOKEN = ("ID")
bot = commands.Bot(command_prefix = '!')
Songname = 'word'
@bot.event
async def on_ready():
print("Qaki.us-MB-J&L")
print(Songname)
@bot.command()
async def test(ctx, arg):
Songname = (arg)
if Word == arg:
await ctx.send('Fail')
return
await ctx.send('Now playing {}'.format (arg))
results = sp.search(q= arg, limit=20)
for idx, track in enumerate(results['tracks']['items']):
print(idx, track['name'])
bot.run(TOKEN)
找出args是什么后开始工作
答案 0 :(得分:1)
您想要做的是使用*, arg
。这显示在 discord.py documentation 中。使用此命令将为您提供发起命令的人提供的完整“查询”。
@client.command()
async def test(ctx, *, arg):
await ctx.send(arg) # sending in channel that the command was initiated in
print(arg) # printing to console
要查看此内容的其他部分,例如特定参数、单字参数等,请从 the top of the linked page 开始。