简体   繁体   中英

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. nothing happens.. I've tried using other commands too but they all result in errors,, I'm a beginner to using Python, so please? 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 .

Also no need for the 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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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