首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >{discord.py-rewrite}写一个包含用户名和数字的排行榜

{discord.py-rewrite}写一个包含用户名和数字的排行榜
EN

Stack Overflow用户
提问于 2020-09-07 07:27:09
回答 1查看 337关注 0票数 0

如果标题不太有意义,我很抱歉。所以我有一个不和谐的机器人,我想要它,所以每当有人说“蛋”时,它就会给他们一分。如果他们不说egg,它会将他们的点数设置为之前点数的一半,并进行四舍五入,这样就没有小数了。我尝试使用Pickle库来保存文件,但它看起来并不正确,好像它的组织方式与我的不同,它似乎只保存了它能保存的最少的内容。下面是我的代码当前的样子。

代码语言:javascript
复制
import discord
from discord.ext import commands
TOKEN = 'token'
bot = commands.Bot(command_prefix='!')


@bot.event
async def on_ready():
    print(f'Logged in as: {bot.user.name}')
    print(f'With ID: {bot.user.id}')

@bot.command()
async def ping(ctx):
    await ctx.send('Pong! Latency: {0}'.format(round(bot.latency, 1)))

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if message.channel.id == channel_id:
        if message.author == bot.user:
            return
        else:

            if ''.join(message.content.split()).lower() == "egg":
                return
            else:
                await message.channel.send(
                    "{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this server. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect on your self.".format(message.author.mention, message.author.mention))
    else:
        return
    
bot.run(TOKEN)

我知道这是非常随机的。我的最终目标是有一个所有说了鸡蛋的用户的文件,以及他们的当前分数。如果你不想说如何给他们积分,我也许能弄清楚。如果你有任何问题,我会试着回答。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-27 20:54:17

我能够弄明白这一点。

我使用的库是

代码语言:javascript
复制
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
import itertools
import json
from json import loads
import os

包含分数的json文件如下所示的https://hastebin.com/xicihepope.json {"<userID>": 46, "<userID>": 13, "<userID>": 35}

我能够获得单个用户的分数的方法是使用命令https://hastebin.com/eyoleduzar.py

代码语言:javascript
复制
@bot.command()
async def eggscore(ctx, user: discord.Member=None):
    os.chdir(r'Directory_Of_Json_File')
    user = user or ctx.author
    with open('EggData.json', 'r') as file:
        data = loads(file.read())
    score = data[user.mention]
    await ctx.send(score)

我是如何更新排行榜的

我能够用这个代码更新每一条消息的排行榜。https://hastebin.com/utakorecuc.py

代码语言:javascript
复制
@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if message.channel.id == 783032255802245170:
        if message.author == bot.user:
            return
        else:

            if ''.join(message.content.split()).lower() == "egg":
                os.chdir(r'Directory_Of_Json_File')
                with open('EggData.json', 'r') as file:
                    data = loads(file.read())
                try:
                    author = message.author.mention
                    usernameList = data
                    authorsAmount = usernameList[author]
                    print(authorsAmount)
                    usernameList[author] += 1
                    print(usernameList[author])
                    print(usernameList)
                    with open('EggData.json', 'w') as outfile:
                        json.dump(usernameList, outfile)
                    return
                except:
                    author = message.author.mention
                    usernameList = data
                    addUsersID = {f"{author}" : 1}
                    whatToAdd = usernameList
                    whatToAdd.update(addUsersID)
                    with open('EggData.json', 'w') as outfile:
                        json.dump(whatToAdd, outfile)
                    return
            else:
                await message.channel.send("{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this channel. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(message.author.mention, message.author.mention))
    else:
        return

代码的极端无效,但工作排序排行榜

我相信有更好的方法可以做到这一点,但这就是我所做的。https://hastebin.com/ikagajimev.py

代码语言:javascript
复制
@bot.command()
async def eggboard(ctx, user: discord.Member=None):
    os.chdir(r'Directory_Of_Json_File')
    user = user or ctx.author
    with open('EggData.json', 'r') as file:
        data = loads(file.read())
    boardData = data
    N = 3
    out = dict(itertools.islice(data.items(), N))
    SortedTop3 = sorted(out)
    print(SortedTop3) ## THIS IS A LIST ##
    print(out) ## THIS IS A DICTIONARY ##
    print(type(SortedTop3))
    print(type(out))
    FirstPlaceThing = SortedTop3[0]
    SecondPlaceThing = SortedTop3[1]
    ThirdPlaceThing = SortedTop3[2]
    FirstPlace = out[FirstPlaceThing]
    SecondPlace = out[SecondPlaceThing]
    ThirdPlace = out[ThirdPlaceThing]
    boardList = [SortedTop3[0],FirstPlace, SortedTop3[1],SecondPlace, SortedTop3[2], ThirdPlace]
    print(boardList)
    sorted_values = sorted(out.values())
    sorted_dict = {}

    for i in sorted_values:
        for k in out.keys():
            if out[k] == i:
                sorted_dict[k] = out[k]
                break
    sorted_dict_keys = []
    for key in sorted_dict.keys():
        sorted_dict_keys.append(key)
    sorted_dict_keys_1 = sorted_dict_keys[0]
    sorted_dict_keys_2 = sorted_dict_keys[1]
    sorted_dict_keys_3 = sorted_dict_keys[2]
    print(sorted_dict)
    embed=discord.Embed(title="Egg Leaderboard")
    embed.add_field(name="First Place", value=f"{sorted_dict_keys[2]} has an egg score of {sorted_dict[sorted_dict_keys_3]}", inline=False)
    embed.add_field(name="Second Place", value=f"{sorted_dict_keys[1]} has an egg score {sorted_dict[sorted_dict_keys_2]}", inline=False)
    embed.add_field(name="Third Place", value=f"{sorted_dict_keys[0]} has an egg score of {sorted_dict[sorted_dict_keys_1]}", inline=False)
    await ctx.send(embed=embed)

如果你有任何关于如何提高效率的想法,请告诉我,

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63769745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档