【问题标题】:Module object is not callable. i dont know why模块对象不可调用。我不知道为什么
【发布时间】:2021-09-13 05:09:20
【问题描述】:
import os
import discord
import json
from discord.ext import commands, check

def check_if_user_has_premium(ctx):
    with open("premium_users.json") as f:
        premium_users_list = json.load(f)
        if ctx.author.id not in premium_users_list:
            return False

    return True

@bot.command()
@check(check_if_user_has_premium)
async def apremiumcommand(ctx):
    await ctx.send("Hello premium user!")

@apremiumcommand.error
async def apremiumcommand_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
            await ctx.send("Sorry, but you are not a premium user!")
    else:
        raise error


bot.run(os.getenv("TOKEN"))

我收到此错误:

Traceback(最近一次调用最后一次): 文件“main.py”,第 269 行,在 @check(check_if_user_has_premium) TypeError: 'module' 对象不可调用

【问题讨论】:

  • 您的意思可能是@commands.check(...),而不是@check(...)
  • @AbhishekPratapSingh 不,json.load 用于文件,json.loads 用于字符串。等同于json.dump/json.dumps

标签: python discord discord.py


【解决方案1】:

正如您在documentation 中看到的那样,您应该已经完成​​了

@commands.check(f) 而不是@check(f)

您还需要从导入行中删除 , check,因为该函数仅存在于模块 discord.ext.commands

【讨论】:

  • 如果对您有帮助,请将其标记为已解决:D
猜你喜欢
  • 2012-11-18
  • 2013-08-06
  • 2014-04-11
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多