簡體   English   中英

Discord 機器人沒有響應命令...(使用 Pycord)

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

我試圖制作一個 discord 機器人,我希望它的第一個命令非常簡單,我只希望它在有人鍵入“.ping”但我嘗試運行命令時響應“pong”。 什么也沒有發生..我也嘗試過使用其他命令,但它們都會導致錯誤,我是使用 Python 的初學者,所以好嗎? 有人可以告訴我我的代碼有什么問題,以及如何修復它嗎?

簡而言之,當我鍵入我為其執行的命令時,我的機器人沒有響應。

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

而不是@bot.command你應該使用@client.command

也不需要from discord.ext.commands import Bot import

所以你的新代碼是

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

你可以在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