试图在我的discord.py重写bot中创建!say命令

时间:2020-04-20 01:17:54

标签: python url-rewriting bots discord discord.py

我正在使用discord.py重写,并希望创建一个命令,以退回命令“ $ say”之后的文本。我查看了文档并在线查看,但是找不到任何最新代码。有谁知道我该怎么做?谢谢。

7 个答案:

答案 0 :(得分:1)

如果要使用命令,请确保导入命令并创建上下文参数。像这样:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='$')

@bot.command()
async def say(ctx, message=None)
    await ctx.send(message)

bot.run("TOKEN HERE")

该命令将由$say message执行,因为当我们定义bot时,我们设置了前缀,而当我们设置了命令时,我们做了async def name,显然,我们的命令名称就是。

因此,在您的漫游器运行时,您可以进行$say good morning!,而漫游器会在该通道中以good morning!进行回复。

ctx.send是一个协程,这就是我们在它前面等待的原因。 ctx定义频道,而message是我们的message参数。

一些有用的资源:

答案 1 :(得分:1)

您需要将 * 放在 ctx 和 message=None 之间。 message=None 将在命令后观看消息。

我正在使用这个:

@bot.command()
async def say(ctx,*,message=None)
  await ctx.send(message)

答案 2 :(得分:0)

@bot.command()
async def say(ctx, *, question):
    await ctx.message.delete()
    await ctx.send(f'{question}')

我之所以发送此邮件,仅是因为您可以使用它,并让它删除作者信息。然后它还会在您的命令后发送任何类型的消息。

答案 3 :(得分:0)

在这里,我有一个say命令,仅适用于指定的用户ID。

async def speak(ctx, *, text):
    if ctx.message.author.id == 621102107621326878:
        message = ctx.message
        await message.delete()

        await ctx.send(f"{text}")
    else:
        await ctx.send('lol mate what u doin that isn\'t a real command')

如果您想要一个普通的,请执行以下操作:

async def speak(ctx, *, text):
    message = ctx.message
    await message.delete()

    await ctx.send(f"{text}")

答案 4 :(得分:0)

试试这个,这是我用的

@client.command
async def say(ctx, *, text=''):
    if text == '':
        ctx.send("You need to say something")
    else:
        ctx.send(text)
        ctx.message.delete()

答案 5 :(得分:0)

我写过的最简单的代码是

@bot.command()
async def say(ctx, *, message):
    try:
        await ctx.send(message)
    except:
        await ctx.send("Please Give Some Message!")

答案 6 :(得分:0)

@client.command
async def say(ctx, *, text=''):
if text == '':
    ctx.send("Say Something First Noob!")
else:
await ctx.send(text)

现在注意不要做 ctx.send 在 ctx 之前有等待,如果你不这样做,那么 100% 它会说它从来没有等待过