discord.py: система уровней - винтики - PullRequest
0 голосов
/ 20 июня 2020

Каждый раз, когда я пытался запустить приведенный ниже код, он просто не работал. Я новичок во всем этом.

import discord
from discord.ext import commands
import json
client = commands.Bot(command_prefix='')

class lvls(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener
    async def on_member_join(self, member):
        with open(r"C:\Users\Frank\Desktop\BOTZS\test.py", 'r') as f:
            users = json.load(f)

        await update_data(users, member)

        with open('LvlUP.json', 'w') as f:
            json.dump(users, f)

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.bot == False:
            with open('LvlUP.json', 'r') as f:
                users = json.load(f)

            await update_data(users, message.author)
            await add_experience(users, message.author, 5)
            await level_up(users, message.author, message)

            with open('LvlUP.json', 'w') as f:
                json.dump(users, f)

    async def update_data(self, users, user):
        if not f'{user.id}' in users:
            users[f'{user.id}'] = {}
            users[f'{user.id}']['experience'] = 0
            users[f'{user.id}']['level'] = 1

    async def add_experience(self, users, user, exp):
        users[f'{user.id}']['experience'] += exp

    async def level_up(self, users, user, message):
        experience = users[f'{user.id}']['experience']
        lvl_start = users[f'{user.id}']['level']
        lvl_end = int(experience ** (1 / 4))
        if lvl_start < lvl_end:
            embed = discord.Embed(title="**LEVEL UP!**",
                                  description=f'{user.mention} has leveled up to level {lvl_end}! :fire: 'f'\n Soundwave Superior,{user.mention} Inferior ',
                                  color=discord.Color.dark_red())
            embed.set_thumbnail(url=user.avatar_url)
            users[f'{user.id}']['level'] = lvl_end
            await message.channel.send(embed=embed)


def setup(client):
    client.add_cog(lvls(client))

Образ

1 Ответ

1 голос
/ 20 июня 2020

Вам нужно добавить () для commands.Cog.listener:

# Your code
@commands.Cog.listener()
async def on_member_join(self, member):
# Your code

Вам нужно настроить клиент в конце файла:

# Your code 
setup(client)

И вам нужно запустить клиент после настройки:

# Your code 
setup(client)
client.run('YOUR_TOKEN') # Set your token for the bot

Полный код если вы не понимаете мои комментарии:

import discord
from discord.ext import commands
import json
client = commands.Bot(command_prefix='')

class lvls(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_member_join(self, member):
        with open(r"C:\Users\Frank\Desktop\BOTZS\test.py", 'r') as f:
            users = json.load(f)

        await update_data(users, member)

        with open('LvlUP.json', 'w') as f:
            json.dump(users, f)

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.bot == False:
            with open('LvlUP.json', 'r') as f:
                users = json.load(f)

            await update_data(users, message.author)
            await add_experience(users, message.author, 5)
            await level_up(users, message.author, message)

            with open('LvlUP.json', 'w') as f:
                json.dump(users, f)

    async def update_data(self, users, user):
        if not f'{user.id}' in users:
            users[f'{user.id}'] = {}
            users[f'{user.id}']['experience'] = 0
            users[f'{user.id}']['level'] = 1

    async def add_experience(self, users, user, exp):
        users[f'{user.id}']['experience'] += exp

    async def level_up(self, users, user, message):
        experience = users[f'{user.id}']['experience']
        lvl_start = users[f'{user.id}']['level']
        lvl_end = int(experience ** (1 / 4))
        if lvl_start < lvl_end:
            embed = discord.Embed(title="**LEVEL UP!**",
                                  description=f'{user.mention} has leveled up to level {lvl_end}! :fire: 'f'\n Soundwave Superior,{user.mention} Inferior ',
                                  color=discord.Color.dark_red())
            embed.set_thumbnail(url=user.avatar_url)
            users[f'{user.id}']['level'] = lvl_end
            await message.channel.send(embed=embed)


def setup(client):
    client.add_cog(lvls(client))

setup(client)
client.run('YOUR_TOKEN')

Надеюсь, это вам поможет.

UPD: Попробуйте добавить self. для своих функций:

# on_member_join
await self.update_data(users, member)

# on_message
await self.update_data(users, message.author)
await self.add_experience(users, message.author, 5)
await self.level_up(users, message.author, message)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...