【问题标题】:Bot doesnt respond to commands, but he react to events [closed]Bot不响应命令,但他对事件做出反应[关闭]
【发布时间】:2021-06-23 03:58:29
【问题描述】:

我找了 2 天,找不到任何东西。我看过stackoverflow,但没有奏效。当我在 Visual Studio Code 中编码时,它起作用了。然后我切换到 PyCharm,后来它坏了。我收到了很多警告并使用了快速修复。在 Visual Studio Code 上它运行了,但在 PyCharm 上我遇到了很多错误并且不想立即运行。

我得到的唯一错误是:

get_prefix() takes 1 positional argument but 2 were given.

有人可以帮忙吗?

import datetime
import os
import random
from datetime import datetime
from itertools import cycle
from pathlib import Path

import discord
from discord.ext import commands, tasks

import json

intents = discord.Intents.default()

cwd = Path(__file__).parents[0]
cwd = str(cwd)


def get_prefix(message):
    with open(cwd + '/json/prefixes.json', "r") as f:
        prefixes = json.load(f)

    return prefixes[str(message.guild.id)]


secret_file = json.load(open(cwd + '/json/secrets.json'))
description = '''Nameless Bot - Made by Cris'''
bot = commands.Bot(command_prefix=get_prefix, case_insensitive=True, intents=intents, owner_id=368817489502535702)
bot.config_token = secret_file['token']

for filename in os.listdir(cwd + '/cogs/'):
    if filename.endswith('.py') and not filename.startswith("_"):
        bot.load_extension('cogs.{}'.format(filename[:-3]))
        print("Bestand: {} gevonden! Toppp".format(filename))

bot.colors = {
    "WHITE": 0xFFFFFF,
    "AQUA": 0x1ABC9C,
    "GREEN": 0x2ECC71,
    "BLUE": 0x3498DB,
    "PURPLE": 0x9B59B6,
    "LUMINOUS_VIVID_PINK": 0xE91E63,
    "GOLD": 0xF1C40F,
    "ORANGE": 0xE67E22,
    "RED": 0xE74C3C,
    "NAVY": 0x34495E,
    "DARK_AQUA": 0x11806A,
    "DARK_GREEN": 0x1F8B4C,
    "DARK_BLUE": 0x206694,
    "DARK_PURPLE": 0x71368A,
    "DARK_VIVID_PINK": 0xAD1457,
    "DARK_GOLD": 0xC27C0E,
    "DARK_ORANGE": 0xA84300,
    "DARK_RED": 0x992D22,
    "DARK_NAVY": 0x2C3E50,
}
bot.color_list = [c for c in bot.colors.values()]

@bot.event
async def on_ready():
    bot.ready = True
    bot.guild = bot.get_guild(656957602139209781)
    change_status.start()
    print('\nIngelod als:')
    print(bot.user.name)
    print(bot.user.id)
    print('------ \n')

@tasks.loop(seconds=10)
async def change_status():
    status = cycle([f'on {len(bot.guilds)} Servers'])
    await bot.change_presence(activity=discord.Game(next(status)))


@bot.command(brief="Change the prefix")
@commands.has_permissions(administrator=True)
async def changeprefix(ctx, prefix, guild):
    with open(cwd + '/json/prefixes.json', "r") as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = prefix

    with open(cwd + '/json/prefixes.json', "w") as f:
        json.dump(prefixes, f, indent=4)

    await ctx.send(f"The prefix was changed to {prefix}")


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


@bot.event
async def on_guild_join(guild):
    with open(cwd + '/json/prefixes.json', "r") as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = ">"

    with open(cwd + '/json/prefixes.json', "w") as f:
        json.dump(prefixes, f, indent=4)

@bot.event
async def on_message(msg):
    if f"<@!{bot.user.id}>" in msg.content:
        with open(cwd + '/json/prefixes.json', "r") as f:
            prefixes = json.load(f)

        pre = prefixes[str(msg.guild.id)]

        await msg.channel.send(f"__**My prefix for this server is:**__ `` {pre} ``")
        await bot.process_commands(msg)

bot.run(bot.config_token)

【问题讨论】:

  • IDE(几乎)与代码是否有效无关。我建议创建一个简化版本的代码,准确显示哪些中断和哪些错误;然后更新您的问题。
  • 欢迎来到 Stack Overflow。可能是当前工作目录已更改。但是如果没有任何错误消息或其他信息,代码本身就没有什么可诊断的。您是否创建了一个 venv,您是如何在与 PyCharm 不同的 VSCode 中运行代码的?请编辑问题以包含您遇到的错误。
  • 代码在 command_prefix=get_prefix 上中断。它说 get_prefix() 接受 1 个位置参数,但给出了 2 个

标签: python pycharm discord.py


【解决方案1】:

on_message 事件中的逻辑存在问题 - process_commands 应该不在 if 语句中:

@bot.event
async def on_message(msg):
    if f"<@!{bot.user.id}>" in msg.content:
        with open(cwd + '/json/prefixes.json', "r") as f:
            prefixes = json.load(f)

        pre = prefixes[str(msg.guild.id)]

        await msg.channel.send(f"__**My prefix for this server is:**__ `` {pre} ``")
    await bot.process_commands(msg)

get_prefix 也应该有 2 个参数,bot 和 message:

def get_prefix(bot, message):
   ...

【讨论】:

  • 哦,谢谢你们
  • 如果有帮助记得采纳答案
  • 问题出在 get_prefix 上,我正在尝试解决它。​​
  • 是的,get_prefix 应该有 2 个参数,bot 和 message - 让我编辑我的答案
  • 我妈太笨了,非常感谢!!
猜你喜欢
  • 2021-10-23
  • 2020-12-15
  • 2021-08-28
  • 1970-01-01
  • 2021-11-17
  • 2017-06-11
  • 2021-05-12
相关资源
最近更新 更多