机器人不响应命令,但他对事件做出反应

时间:2021-03-26 10:17:22

标签: python pycharm discord.py

我找了 2 天,什么也没找到。我看过stackoverflow,但没有用。当我在 Visual Studio Code 中编码时它起作用了。然后我切换到 PyCharm,后来它坏了。我收到了很多警告并使用了 Quick Fix。在 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)

1 个答案:

答案 0 :(得分:0)

您在 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 个参数,botmessage :

def get_prefix(bot, message):
   ...