简体   繁体   English

Discord 机器人没有响应命令...(使用 Pycord)

[英]Discord Bot not responding to commands... (Using Pycord)

Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong," when someone types ".ping" but when I try to run the command.我试图制作一个 discord 机器人,我希望它的第一个命令非常简单,我只希望它在有人键入“.ping”但我尝试运行命令时响应“pong”。 nothing happens.. I've tried using other commands too but they all result in errors,, I'm a beginner to using Python, so please?什么也没有发生..我也尝试过使用其他命令,但它们都会导致错误,我是使用 Python 的初学者,所以好吗? can someone tell me what's wrong with my code, and how I can fix it?有人可以告诉我我的代码有什么问题,以及如何修复它吗?

In short, my bot doesn't respond back when I type the command I made for it to do so.简而言之,当我键入我为其执行的命令时,我的机器人没有响应。

import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot

case_insensitive=True

client = discord.Client()
bot = commands.Bot(
  command_prefix="!",
  intents=discord.Intents(members=True, messages=True, guilds=True)
)

@client.event
async def on_ready():
    print('logged in as {0.user}!'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
      
@bot.command(name="ping", description= "Find out!")
async def ping(ctx):
  await ctx.send("Pong!")

client.run(os.getenv('TOKEN'))

Instead of @bot.command you should use @client.command .而不是@bot.command你应该使用@client.command

Also no need for the from discord.ext.commands import Bot import也不需要from discord.ext.commands import Bot import

So your new code would be所以你的新代码是

import discord
import os
from discord.ext import commands

case_insensitive=True

client = discord.Client()
bot = commands.Bot(
  command_prefix="!",
  intents=discord.Intents(members=True, messages=True, guilds=True)
)

@client.event
async def on_ready():
    print('logged in as {0.user}!'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
      
@client.command(name="ping", description= "Find out!")
async def ping(ctx):
  await ctx.send("Pong!")

client.run(os.getenv('TOKEN'))

You could just do inside on_message code block你可以在on_message代码块里面做

import discord

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!ping'):
        await message.channel.send('Pong!')

client.run('your token here')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM