discord.py:水平系统-齿轮

时间:2020-06-20 18:05:01

标签: discord.py discogs-api

每当我尝试运行它下面的代码时,它似乎都无法正常工作。我真的很陌生。

#include <tuple>
#include <vector>
#include <functional>

template <typename T>
struct tree_item
{
    T param;
    std::function<void(T)> callback;
};

template <typename... Ts>
struct tuple_node
{
    std::tuple<Ts...> tupl;
};

// recursion base case
template <typename T>
void expand_nested(tree_item<T> ti)
{
    ti.callback(ti.param);
}

// recursive function
template <typename T>
void expand_nested(tree_item<T> ti, tree_item<T> rest...)
{
    ti.callback(ti.param);
    expand_nested(ti, rest...);
}

template <typename... Ts>
void expand_root(tuple_node<Ts...> nodes)
{
    auto current = std::get<1>(nodes.tupl);
    auto rest = std::get<...>(nodes.tupl); // Made up syntax that doesn't work
    // How can I fill the "rest" variable with the remaining elements of the "nodes.tupl" tuple?

    expand_nested(current, rest...);
}

int main()
{
    tuple_node<tree_item<int>, tree_item<float>> nodes;

    tree_item<int> tree_int;
    tree_item<float> tree_float;
    tree_item<double> tree_double;

    tuple_node<tree_item<int>, tree_item<float>, tree_item<double>> node;
    node.tupl = std::make_tuple(tree_int, tree_float, tree_double);

    expand_root(nodes);
}

Image

1 个答案:

答案 0 :(得分: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)