簡體   English   中英

試圖在我的 discord.py 重寫機器人中創建一個 !say 命令

[英]Trying to create a !say command in my discord.py rewrite bot

我正在使用 discord.py 重寫並希望創建一個命令,該命令會返回命令之后的文本“$say”。 我查看了文檔和在線,但找不到任何最新的代碼。 有誰知道我怎么能做到這一點? 謝謝。

如果您想使用命令,請確保導入命令並創建上下文參數。 像這樣:

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。

因此,在 discord 中,當您的機器人正在運行時,您可以$say good morning! 機器人會在該頻道回復good morning! .

ctx.send 是一個協程,這就是我們在它前面等待的原因。 並且 ctx 定義了通道,而 message 是我們的消息參數。

一些有用的資源:

您需要將 * 放在 ctx 和 message=None 之間。 message=None 將在命令之后查看消息。

我正在使用這個:

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

我發送這個只是因為你可以使用它並讓它刪除作者的消息。 然后它還會在您發出命令后發送任何類型的消息。

在這里,我有一個 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}")

試試這個,這是我用的

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

我寫的最簡單的代碼是

@bot.command()
async def say(ctx, *, message):
    try:
        await ctx.send(message)
    except:
        await ctx.send("Please Give Some Message!")
@client.command
async def say(ctx, *, text=''):
if text == '':
    ctx.send("Say Something First Noob!")
else:
await ctx.send(text)

現在請注意不要執行 ctx.send 在 ctx 之前等待,如果你不這樣做,那么 100% 它會說它從未等待過

這里!

import discord
from discord.ext import commands

client = commands.Bot(command_prefix='!')

@client.command()
@commands.is_owner()
async def say(ctx, *, message):
  await ctx.message.delete()
  await ctx.send(message)

client.run("YOUR_TOKEN")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM