使用 Discord.py 的带有参数的命令

问题描述 投票:0回答:2
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image

Client = discord.Client()
client = commands.Bot(command_prefix = "-")`
@client.command
async def spam(ctx, arg):
    count = 0 
    await Context.send(message.channel, "Wait for it....")
    time.sleep(3)
    while count < 20:
        await ctx.send(arg)
        time.sleep(3)
        count = count + 1

这段代码应该提到参数中指定的人。例如,如果有人输入

-spam @Bob
,机器人应该说

@Bob
@Bob 20 times
python discord.py
2个回答
1
投票

无需同时实例化

discord.Client
commands.Bot
Bot
Client
的子类。

Bot.command
是一个返回装饰器的函数,而不是它本身是装饰器。你需要调用它来使用它来装饰一个协程:

@client.command()

您可能应该使用转换器来获取您正在 ping 的用户。

Context
ctx
的实例的类。您应该通过
ctx
访问所有方法。

不要使用

time.sleep
,因为它会阻塞事件循环。而是等待
asyncio.sleep

from discord.ext.commands import Bot
from asyncio import sleep
from discord import User

client = Bot(command_prefix = "-")

@client.command()
async def spam(ctx, user: User):
    await ctx.send("Wait for it....")
    await sleep(3)
    for _ in range(20):
        await ctx.send(user.mention)
        await sleep(3)

client.run("token")

0
投票

首先,您不需要同时使用

discord.Client()
commands.Bot()

这是一个细分:

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image

client = commands.Bot(command_prefix = "-")

@client.command()
async def spam(ctx, user:discord.Member): #Replace arg with user:discord.Member, discord.Member has many attributes that allow you to do multiple actions
    count = 0 
    await ctx.send(message.channel, "Wait for it....") #replace Context with ctx, ctx is the Context
    time.sleep(3)
    while count <= 20: # Replaced count<20 with count<=20 otherwise it'll only spam 19 times 
        await ctx.send(user.mention) #changed args to user.mention, user.mention mentions the user
        time.sleep(3)
        count = count + 1
© www.soinside.com 2019 - 2024. All rights reserved.