我正在研发一个调平机器人.我发现了获得XP的一个主要问题,它很容易被滥用.我想让它在用户发送消息后有一个冷却时间,这样当用户在一个渠道向XP农场发送垃圾邮件时,他们对每条消息有0.5秒的冷却时间,以防止垃圾邮件.如果你发了很多垃圾短信,你很容易就能登上榜首,我不希望这种情况发生.每件事都与它一起工作,XPyield ,排行榜,以及升级消息.我在互联网上寻找了这个问题的答案,但没有一个起到帮助作用.有人知道吗?请多多关照!:)
代码:
@bot.event
async def on_message(message):
if not message.author.bot:
# specify the encoding as utf-8 when opening the file
with open('level.json','r', encoding='utf-8') as f:
users = json.load(f)
await update_data(users, message.author,message.guild)
await add_experience(users, message.author, 4, message.guild)
await level_up(users, message.author,message.channel, message.guild)
# specify the encoding as utf-8 when writing the file
with open('level.json','w', encoding='utf-8') as f:
json.dump(users, f)
await bot.process_commands(message)
async def update_data(users, user,server):
if not str(server.id) in users:
users[str(server.id)] = {}
if not str(user.id) in users[str(server.id)]:
users[str(server.id)][str(user.id)] = {}
users[str(server.id)][str(user.id)]['experience'] = 0
users[str(server.id)][str(user.id)]['level'] = 1
elif not str(user.id) in users[str(server.id)]:
users[str(server.id)][str(user.id)] = {}
users[str(server.id)][str(user.id)]['experience'] = 0
users[str(server.id)][str(user.id)]['level'] = 1
async def add_experience(users, user, exp, server):
users[str(user.guild.id)][str(user.id)]['experience'] += exp
async def level_up(users, user, channel, server):
experience = users[str(user.guild.id)][str(user.id)]['experience']
lvl_start = users[str(user.guild.id)][str(user.id)]['level']
lvl_end = int(experience ** (1/4))
if str(user.guild.id) != '757383943116030074':
if lvl_start < lvl_end:
await channel.send(':fire: {} has leveled up to **Level {}!**'.format(user.mention, lvl_end))
users[str(user.guild.id)][str(user.id)]['level'] = lvl_end
embed=discord.Embed(title=':fire: {} has leveled up to **Level {}!**'.format(user.mention, lvl_end), color=0x38ff6a)
await bot.get_channel(log_channel_id).send(embed=embed)
@bot.command(aliases = ['rank','lvl'])
async def level(ctx,member: discord.Member = None):
if not member:
user = ctx.message.author
# specify the encoding as utf-8 when opening the file
with open('level.json','r', encoding='utf-8') as f:
users = json.load(f)
lvl = users[str(ctx.guild.id)][str(user.id)]['level']
exp = users[str(ctx.guild.id)][str(user.id)]['experience']
embed = discord.Embed(title = 'Level {}'.format(lvl), description = f"{exp} XP " ,color = discord.Color.green())
embed.set_author(name = ctx.author, icon_url = ctx.author.avatar.url)
await ctx.send(embed = embed)
else:
# specify the encoding as utf-8 when opening the file
with open('level.json','r', encoding='utf-8') as f:
users = json.load(f)
lvl = users[str(ctx.guild.id)][str(member.id)]['level']
exp = users[str(ctx.guild.id)][str(member.id)]['experience']
embed = discord.Embed(title = 'Level {}'.format(lvl), description = f"{exp} XP" ,color = discord.Color.green())
embed.set_author(name = member, icon_url = member.avatar.url)
await ctx.send(embed = embed)
JSON文件示例:
{
"757383943116030074": {
"123456789012345678": {
"experience": 0,
"level": 1
}
}
}