0

每当我尝试运行它下面的代码时,它似乎都不起作用。我对这一切真的很陌生。

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))

图片

4

1 回答 1

1

您需要添加()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)
于 2020-06-20T19:46:16.760 回答