Discord.on_message()缺少1个必需的位置参数:'ctx'

时间:2019-12-09 13:17:58

标签: python json discord discord.py

我正在使用用户级系统,现在我在启动Discord升级代码时遇到了大问题,我不知道也没有任何解决办法。

这是我的代码:

import discord
import asyncio
import json
from discord.ext import commands

сlass Levels:

    def __init__(self, client):
        self.bot = client

        self.bot.loop.create_task(self.save_users())

        with open(r'C:\Users\conex_000\PycharmProjects\Shirochka\users.json', 'r') as f:
           self.users = json.load(f)

    async def save_users(self):
        await self.bot.wait_until_ready()
        while not self.bot.is_closed():
            with open(r'C:\Users\conex_000\PycharmProjects\Shirochka\users.json', 'w') as f:
                json.dump(self.users, f, indent=4)

            await asyncio.sleep(5)

    def lvl_up(self, author_id):
        cur_xp = self.users[author_id]['exp']
        cur_lvl = self.users[author_id]['level']

        if cur_xp >= round((4 * (cur_lvl ** 3)) / 5):
            self.users[author_id]['level'] += 1
            return True
        else:
            return False

    async def on_message(self, message):
        if message.author == self.bot.user:
            return

        author_id = str(message.author.id)

        if not author_id in self.users:
            self.users[author_id] = {}
            self.users[author_id]['level'] = 1
            self.users[author_id]['exp'] = 0

        self.users[author_id]['exp'] += 1

        if self.lvl_up(author_id):
            await message.channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}")

    @client.command()
    async def level(self, ctx, member: discord.Member = None):
        member = ctx.author if not member else member
        member_id = str(member.id)

        if not member.id in self.users:
            await ctx.send("Can't identify a member")
        else:
            embed = discord.Embed(color=member.color, timestamp=ctx.message.created_at)

            embed.set_author(name=f'Level - {member}', icon_url=client.user.avatar_url)

            embed.add_field(name='Level', value=self.users[member_id]['level'])
            embed.add_field(name='Level', value=self.users[member_id]['exp'])

            await ctx.send(embed=embed)

此代码正在启动,但是当我在Discord聊天程序上键入任何消息时,返回以下错误:

  

TypeError:on_message()缺少1个必需的位置参数:“ ctx”

2 个答案:

答案 0 :(得分:1)

添加了子类化命令。“水平齿轮”的“齿轮”,用装饰器标记的命令和事件现在都可以使用。

class Levels(commands.Cog):

def __init__(self, client):
    self.bot = client

    self.bot.loop.create_task(self.save_users())

    with open(r'C:\Users\conex_000\PycharmProjects\Shirochka\users.json', 'r') as f:
       self.users = json.load(f)

async def save_users(self):
    await self.bot.wait_until_ready()
    while not self.bot.is_closed():
        with open(r'C:\Users\conex_000\PycharmProjects\Shirochka\users.json', 'w') as f:
            json.dump(self.users, f, indent=4)

        await asyncio.sleep(5)

def lvl_up(self, author_id):
    cur_xp = self.users[author_id]['exp']
    cur_lvl = self.users[author_id]['level']

    if cur_xp >= round((10 * (cur_lvl ** 3)) / 5):
        self.users[author_id]['level'] += 1
        return True
    else:
        return False
@commands.Cog.listener()
async def on_message(self, message):
    if message.author == self.bot.user:
        return

    author_id = str(message.author.id)

    if not author_id in self.users:
        self.users[author_id] = {}
        self.users[author_id]['level'] = 1
        self.users[author_id]['exp'] = 0

    self.users[author_id]['exp'] += 1

    if self.lvl_up(author_id):
        await message.channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}")

@commands.command()
async def level(self, ctx, member: discord.Member = None):
    member = ctx.author if not member else member
    member_id = str(member.id)

    if not member_id in self.users:
        await ctx.send("Can't identify a member")
    else:
        embed = discord.Embed(color=member.color, timestamp=ctx.message.created_at)

        embed.set_author(name=f'Level - {member}', icon_url=self.bot.user.avatar_url)

        embed.add_field(name='Level', value=self.users[member_id]['level'])
        embed.add_field(name='XP', value=self.users[member_id]['exp'])

        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(Levels(bot))

答案 1 :(得分:0)

在嵌齿轮内部,您必须@commands.Cog.listener()装饰器添加到与事件相关的功能中,否则会出现错误。

编辑:如果您的班级不是齿轮,请尝试@client.event