python - discord.py 为什么 @client.command 不起作用?

标签 python python-3.x discord.py bots

当我运行注册命令时没有任何反应。我已经尝试了一切,但没有任何帮助( 导入不和谐 导入环境 从discord.ext导入命令

intents = discord.Intents.all()
client = commands.Bot(command_prefix='!', intents=discord.Intents.all())

TOKEN = env.discord_token("DISCORD_TOKEN")


@client.event
async def on_ready():
    print("Im ready to go : {0.user}".format(client))

@client.event
async def on_message(message):

    if message.author == client.user:
        return
    if message.content.startswith("!instagram"):
        await message.channel.send('https://www.instagram.com')
    if message.content.startswith("!youtube"):
        await message.channel.send("https://www.youtube.com/channel/fsaf!212faf")

#Dont work
@client.command
async def register(ctx, arg1, arg2):
    await ctx.send(arg2, arg1)



@client.event
async def on_member_join(member):
    role_1 = member.guild.get_role(807733964214321878)
    await member.add_roles(role_1)

client.run(TOKEN)

最佳答案

其实不然

@client.command

正确的使用方法是..

@client.command(name="Command name", description="Command Description")

“名称”和“描述”参数是可选的。

还有一件事... 由于您使用了“on_message”事件.. 这样做...

async def on_message(message):

    if message.author == client.user:
    return
    if message.content.startswith("!instagram"):
        await message.channel.send('https://www.instagram.com')
    if message.content.startswith("!youtube"):
        await message.channel.send("https://www.youtube.com/channel/fsaf!212faf")

    else:
        await client.process_commands(message)

根据我的说法,你的整个代码:

import discord 
import env 
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix='!', intents=discord.Intents.all())

TOKEN = env.discord_token("DISCORD_TOKEN")


@client.event
async def on_ready():
    print("Im ready to go : {0.user}".format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
       return
    if message.content.startswith("!instagram"):
        await message.channel.send('https://www.instagram.com')
    elif message.content.startswith("!youtube"):
        await message.channel.send("https://www.youtube.com/channel/fsaf!212faf")
    else:
        await client.process_commands(message)


@client.command(name='reg')
async def register(ctx, arg1, arg2):
    await ctx.send(arg2, arg1)



@client.event
async def on_member_join(member):
    role_1 = member.guild.get_role(807733964214321878)
    await member.add_roles(role_1)

client.run(TOKEN)

关于python - discord.py 为什么 @client.command 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66839150/

相关文章:

python - 当句号介于两个字母之间时,使用正则表达式插入一行

python - 有没有更简单的方法通过命令进行嵌入? |重写discord.py

python - 如何在每个线程的基础上实现日志记录?

python - 为什么 Python 不抛出切片越界的错误?

python - 在Python中将儒略日期转换为公历日期

不和谐.ext.commands.errors.CommandNotFound : Command "balance" is not found error even though its there

python-3.x - Discord 音乐机器人 - 队列 (discord.py)

python - 仅加载站点首页的 Django 静态文件

python - 如何在 Python 中将字符串转换为复数?

python - 当参数默认为 None 时使用 __repr__,但可能是一个 str