Discord.py - 机器人不响应命令

Posted

技术标签:

【中文标题】Discord.py - 机器人不响应命令【英文标题】:Discord.py - Bot does not respond to commands 【发布时间】:2021-10-06 21:42:28 【问题描述】:

当我写命令“!LVL”时,机器人没有任何反应 大大地。这些团队曾经工作过。我改变了一些东西,现在他没有 对他们做出反应。另外,之前如果我写错了一个命令, 然后在控制台中,机器人给出了这样一个命令没有的错误 存在。现在他只对一个命令作出反应,甚至对 内置一个 !help

但所有事件都正常工作。

import discord
from discord.ext import commands
from discord.utils import get
from pymongo import MongoClient
from datetime import datetime
import asyncio
import time
import os

intents = discord.Intents.default()
intents.members = True
intents.messages = True
intents.voice_states = True
intents.integrations = True
intents.invites = True
intents.presences = True
intents.webhooks = True
client = commands.Bot(command_prefix = "!", intents=intents)
cluster = MongoClient("mongodb://127.0.0.1:27017")
colluser = cluster.chill.user
collclan = cluster.chill.clans
collshop = cluster.chill.shop


@client.event
async def on_ready():
    print("Bot connected to the server")

    for guild in client.guilds:
        for member in guild.members:
            post = 
                "_id": member.id,
                "name": member.mention,
                "user": member.name,
                "inventory": [],
                "warms": 0,
                "mute": 0,
                "voice_activ": 0,
                "localban": 0,
                "register": member.created_at,
                "onservfrom": member.joined_at,
                "clan": 0,
                "marry": 0,
                "love": 0,
                "childs": [],
                "balance": 0,
                "bank": 0,
                "rep": 0,
                "cdxp": 0,
                "cnxp": 0,
                "dxp": 0,
                "nxp": 0,
                "cdbal": 0,
                "cdlvl": 1,
                "cnlvl": 1,
                "dlvl": 1,
                "nlvl": 1
            

            if colluser.count_documents("_id": member.id) == 0:
                colluser.insert_one(post)

@client.event
async def on_guild_role_create(role):
    print("new role created")
    post = 
        "_id": role.id,
        "titel": "none",
        "cp": [],
        "exp": 0,
        "lvl": 0,
        "maxcp": 7,
        "bank": 0
    

    if collclan.count_documents("role_id": role.id) == 0:
        collclan.insert_one(post)

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

    f4 = colluser
    user = message.author
    data = colluser.find_one("_id": user.id)
    dtn = datetime.today().strftime("%I:%M %p")
    await asyncio.sleep(1)
    day = data["dxp"]
    dark = data["nxp"]
    dl = data["dlvl"]
    nl = data["nlvl"]
    balanc = data["balance"]
    dexp = 500 + 100 * dl
    nexp = 500 + 100 * nl
    if not data["cdbal"] == "0":
        return
    if len(message.clean_content) >= 15:
        print("Сообщение длинее 15 символов")
        bal = 6
        exp = 6
    if len(message.clean_content) <= 14:
        print("Сообщение короче 15 символов")
        bal = 3
        exp = 3
    if any(("AM" in dtn) for AM in dtn):
        print("time is AM")
        colluser.update_one("_id": user.id,
            "$set": "dxp": + exp)
    if any(("PM" in dtn) for PM in dtn):
        print("time is PM")
        colluser.update_one("_id": user.id,
        "$set": "nxp": + exp)
    if data["dxp"] >= 500 + 100 * dl:
        print(f"Новый уровень светлой стороны у пользователя user")
        colluser.update_one("_id": user.id,
            "$set": "dlvl": dl + 1)
        colluser.update_one("_id": user.id,
            "$set": "dxp": day - dexp)
    if data["nxp"] >= 500 + 100 * nl:
        print(f"Новый уровень темной стороны у пользователя user")
        colluser.update_one("_id": user.id,
            "$set": "nlvl": nl + 1)
        colluser.update_one("_id": user.id,
            "$set": "nxp": dark - nexp)
    print(f"Пользователь user написал сообшение")
    f1.update_one("_id": user.id,
        "$set": "balance": balanc + bal)
    print("F1")
    f2.update_one("_id": user.id,
        "$set": "cdbal": "1")
    print("F2")
    await asyncio.sleep(40)
    print("F3")
    f4.update_one("_id": user.id,
        "$set": "cdbal": "0")
    print("F4")

@client.event
async def on_voice_state_update(member, before, after):
    data = colluser.find_one("_id": member.id)
    day = data["dxp"]
    dark = data["nxp"]
    dl = data["dlvl"]
    nl = data["nlvl"]
    dexp = 500 + 100 * dl
    nexp = 500 + 100 * nl
    guild = client.guilds[0]
    guild_id = 830565202276909056
    if before.channel is None and after.channel is not None:
        print("1")
        t1 = time.time()
        colluser.update_one("_id": member.id,
            "$set": "voice_tim1": t1)
    elif before.channel is not None and after.channel is None:
        dtn = datetime.today().strftime("%I:%M %p")
        t1 = data["voice_tim1"]
        voice_activ1 = data["voice_activ"]
        balance = data["balance"]
        t2 = time.time()
        tim = t2-t1
        print("0")
        print(dtn)
        colluser.update_one("_id": member.id,
            "$set": "voice_activ": voice_activ1 + tim)
        colluser.update_one("_id": member.id,
            "$set": "balance": balance + tim / 12)
        if any(("AM" in dtn) for AM in dtn):
            print("time is AM")
            colluser.update_one("_id": member.id,
                "$set": "dxp": day + tim / 4)
        if any(("PM" in dtn) for PM in dtn):
            print("time is PM")
            colluser.update_one("_id": member.id,
                "$set": "nxp": dark + tim / 4)
        if data["dxp"] >= 500 + 100 * dl:
            colluser.update_one("_id": member.id,
                "$set": "dlvl": dl + 1)
            colluser.update_one("_id": member.id,
                "$set": "dxp": day - dexp)
        if data["nxp"] >= 500 + 100 * nl:
            colluser.update_one("_id": member.id,
                "$set": "nlvl": nl + 1)
            colluser.update_one("_id": member.id,
                "$set": "nxp": dark - nexp)
        if dl >> nl:
            role_id1 = 869267468047220856
            role_id2 = 869267490851676200
            role1 = guild.get_role(role_id1)
            role2 = guild.get_role(role_id2)
            await member.add_roles(role1, reason="Роль выдана за превоблодания уровня света")
            await member.remove_roles(role2, reason="Роль снята за превоблодания уровня света")
        if nl >> dl:
            role_id1 = 869267468047220856
            role_id2 = 869267490851676200
            role1 = guild.get_role(role_id1)
            role2 = guild.get_role(role_id2)
            await member.add_roles(role2, reason="Роль выдана за превоблодания уровня тьмы")
            await member.remove_roles(role1, reason="Роль снята за превоблодания уровня тьмы")

@client.event
async def on_member_join(member):
    post = 
        "_id": member.id,
        "name": member.mention,
        "user": member.name,
        "inventory": [],
        "warms": 0,
        "mute": 0,
        "voice_activ": 0,
        "localban": 0,
        "register": member.created_at,
        "onservfrom": member.joined_at,
        "clan": 0,
        "marry": 0,
        "love": 0,
        "childs": [],
        "balance": 0,
        "bank": 0,
        "xp": 0,
        "dxp": 0,
        "nxp": 0,
        "cdbal": 0,
        "lvl": 1,
        "dlvl": 1,
        "nlvl": 1
    

    if colluser.count_documents("_id": member.id) == 0:
        colluser.insert_one(post)

@client.command()
async def lvl(ctx, member: discord.Member = None):
    print("test")
    if member is None:
        member = message.author
    else:
        data = colluser.find_one("_id": member.id)
        nnexp = 500 + 100 * data["nlvl"]
        ndexp = 500 + 100 * data["dlvl"]
        dnnexp = nnexp - data["nxp"]
        dndexp = dndexp -data["dxp"]
        if data["voice_activ"] >= 60:
            vcm = data["voice_activ"] / 60
            if vcm >= 60:
                vch = vcm / 60
                if vch >= 24:
                    vcd = vch /24
                    vca = vch
                else:
                    vca = vch
                    t = "часов"
            else:
                vca = vcm
                t = "минут"
        else:
            t = "Секунд"
            vca = data["voice_activ"]
        lvleb = discord.Embed(colour=role.color)
        lvleb.set_author(name=f"Профиль пользователя: member", icon_url=member.avatar_url)
        lvleb.add_field(name="Уровень света", value=data["nlvl"], inline=True)
        lvleb.add_field(name="Уровень тьмы", value=data["dlvl"], inline=True)
        lvleb.add_field(name="Опыт света", value=f"data['nlvl'] / dnnexp", inline=True)
        lvleb.add_field(name="Опыт тьмы", value=f"data['dlvl'] / dndexp", inline=True)
        lvleb.add_field(name="Время в войсе", value=f"vcat", inline=False)
        await ctx.send(embed=lvleb)

@client.event
async def on_command_error(ctx, error):
    print(error)

    if isinstance(error, commands.UserInputError):
        await ctx.send(embed = discord.Embed(
            description = f"Правильное использование команды: `ctx.prefixctx.command.name` (ctx.command.brief): `ctx.prefixctx.command.usage`"
        ))


client.run("TOKEN")

【问题讨论】:

你也可以discord.Intents.all() 【参考方案1】:

on_message 事件的末尾添加一个await client.process_commands(message)

https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

【讨论】:

以上是关于Discord.py - 机器人不响应命令的主要内容,如果未能解决你的问题,请参考以下文章

Discord.py 机器人:如何让我的不和谐机器人向我发送对用户在 DM 中使用的命令的响应,例如进行调查?

Discord.py Intents 使我的机器人不响应任何内容

Discord.py 机器人不响应自定义表情符号

discord.py bot 不会响应命令

为啥我的 discord.py 机器人不响应 on_message 事件?

是否可以在 discord.py 中为不同的前缀使用不同的命令?