我的不和谐机器人的命令不起作用,除了 1

时间:2021-04-20 19:39:34

标签: python discord.py

import discord
import os
import requests
import json
import random
from random import randint
from replit import db
from discord.ext import commands


client = discord.Client()

bot = commands.Bot(command_prefix='.') 

@client.event
async def on_ready():
  print(f'{client.user.name} működik!')   
  await client.change_presence(activity=discord.Game(name='egy bot vagyok'))

@client.event
async def on_member_join(member):
    await member.create_dm()   
    await member.dm_channel.send(
        f' {member.name}, itt van!'
    )


@bot.command()
async def test(ctx, arg):
    await ctx.send(arg)
    print(f'haha')

@client.event
async def on_message(message):

    if message.author == client.user:
        return

    if message.content.startswith('ping'):
        await message.channel.send("pong")

    if message.content.startswith("shrug"):
        await message.channel.send('¯\_(ツ)_/¯')

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

  if message.content.startswith('.pic'):
    await message.channel.send(file=discord.File('combo-0.png'))


token = os.environ.get("secret")
client.run(token)

我是不和谐机器人编程的初学者 这是我的不和谐机器人的全部代码,除了一个命令外,所有命令都不起作用

te .pic 命令

直到现在所有命令都有效,我不知道为什么它们不起作用

如果我能得到一些帮助,我会很高兴 :D (抱歉我的英语不好,英语不是我的第一语言)

2 个答案:

答案 0 :(得分:1)

您不能有多个 on_message 事件,只会注册最后一个。你必须把它们合二为一

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

    if message.content.startswith('ping'):
        await message.channel.send("pong")

    if message.content.startswith("shrug"):
        await message.channel.send('¯\_(ツ)_/¯')

    if message.content.startswith('.pic'):
        await message.channel.send(file=discord.File('combo-0.png'))

答案 1 :(得分:0)

您不能像这样混淆 discord.Client()commands.Bot 客户端。

commands.Botdiscord.Client 的子类,因此您应该能够使用 discord.Client()commands.Bot()

执行您可以执行的所有操作
bot = commands.Bot('!')
@bot.event
async def on_ready():
    print('ready')

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

有关重写 discord.ext.commands 的帮助,互联网上有大量教程。