繁体   English   中英

我如何跟踪在 discord 中输入了多少次命令

[英]How can i keep track of how many times has a command been typed in discord

所以我正在制作一个机器人,我希望在玩家加入或第一次在服务器上玩游戏时为玩家提供一些默认值的游戏。 所以我可以跟踪每个玩家的价值观。

游戏逻辑:玩家有一些价值,如硬币、力量、速度和大脑,您可以通过将硬币花在加电上来升级它们
代码:

# imports
import discord
import os
from discord import user
from discord import activity
from discord.ext import commands, tasks
from itertools import cycle
import random
# prefix
client = commands.Bot(command_prefix=",")
status = cycle(['spedrunning mincraft for the 100 time',
               'trololol', 'working on bugs', 'learning from dream'])
meme_list = ['https://i.pinimg.com/736x/62/bf/0d/62bf0d6b9ccd73f5e15aad8fa1d6163c.jpg',
'https://s3.india.com/wp-content/uploads/2019/02/valentines.jpg',
'https://img.delicious.com.au/WqbvXLhs/del/2016/06/more-the-merrier-31380-2.jpg',
'https://image.scoopwhoop.com/w960-h500-cfix/https://s4.scoopwhoop.com/anj/feat/941432013.jpg',
'https://perfectdaytoplay.com/wp-content/uploads/2020/05/Travel-funny-meme-covid19-pandemic-joke-humor-38.jpg',
'https://i.ytimg.com/vi/VrM-JkDnwiI/maxresdefault.jpg',
'https://i.ytimg.com/vi/zKxMTovmPN4/maxresdefault.jpg',
'https://i.imgur.com/k3pFRXd.jpg',
'https://starecat.com/content/wp-content/uploads/cats-and-dog-when-you-hold-your-hand-above-them-minecraft.jpg',
' https://i.pinimg.com/originals/1b/14/c3/1b14c30f94391830768d334f9234e412.jpg',
'https://i.pinimg.com/736x/a0/eb/34/a0eb3405be3afd0c622caf45b80fb4c7.jpg',
'https://static0.srcdn.com/wordpress/wp-content/uploads/2020/03/HarryPotterMemeHeader.jpg',
'https://i.pinimg.com/originals/6f/46/4f/6f464f022adadff6e831ecf54586a681.jpg',
'https://media.npr.org/assets/img/2015/03/03/overly_custom-39399d2cf8b6395770e3f10fd45b22ce39df70d4-s800-c85.jpg',
'https://cms.qz.com/wp-content/uploads/2018/07/meme-featured.jpg?qual75&stal41215',
'https://static.mommypoppins.com/styles/image620x420/s3/school_meme_3_0.jpg'
'https://s.yimg.com/ny/api/res/1.2/_twsNG4z4ENpWE7ZXoCJ2Q--/YXBwaWQ9aGlnaGxhbmRlcjt3PTk2MDtoPTk1OC4zNjE3NzQ3NDQwMjczO2NmPXdlYnA-/https://s.yimg.com/os/creatr-uploaded-images/2021-05/5d4f3960-acc5-11eb-bbff-5305b55ded14',
'https://i.imgflip.com/5c74jb.jpg',
'https://imgflip.com/i/5c74rc',
'https://imgflip.com/i/5c7539',
'https://imgflip.com/i/5c75bl',
'https://imgflip.com/i/5cx3vk',
'https://imgflip.com/i/5cx4ky',
'https://imgflip.com/i/5cx5io',

]

@client.event
async def on_ready():
    change_status.start()
    print("bot running ")


@tasks.loop(seconds=3600)
async def change_status():
    await client.change_presence(activity=discord.Game(next(status)))


@client.command()
async def info(ctx):
    await ctx.send('commands prefix is Blaeam  ,clear <amount> clears the amount that you puted ,kick,ban')
@client.command()
async def meme(ctx):
    await ctx.send(random.choice(meme_list))


@client.command()
async def clear(ctx, amount=5):
    await ctx.channel.purge(limit=amount)
    await ctx.send(f'Cleared {amount} message(s)')

def default_stats(coins,power,speed,brain):
 coins=100
 power=10
 speed=10
 brain=5



@client.command()
async def start (ctx):
  await ctx.send(' the game started ')
  await ctx.send('This game is called power apps.'+'In this game you have coins, power, speed, brain.'+'You can level up your skills by spending coins on the skils'+'To do that you can type -->play<amount of coins>_power, speed, brain '+'How to get coins.'+'You can get coins by battling other players by typing the command battle_<member name >')
  


@client.command()
async def play_power(ctx, member: discord.Member):
  await ctx.send('{member.name}put ten of his coins to power')
  #TODO:add game here 


@client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f'Kicked {member.name} from the server!')


@client.command()
async def ban(ctx, member: discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f'Banned {member.name} from the server!')


client.run(Token)```
If you have any ideas tell me.
You can also find this project on replit [https://replit.com/@YoLoGRAlex/Bleam#main.py][1]


  [1]: https://replit.com/@YoLoGRAlex/Bleam#main.py

所以我假设你是P3ter#6969

话虽如此,您可以使用on_command来临时跟踪用户每个 session 的使用命令(注意:如果您重新启动机器人,所有这些都将丢失)

您可以声明一个空字典,例如:

some_dictionary = {}

然后做一个on_command监听器

@client.event
async def on_command(ctx):
        x = some_dictionary
        try:
            x[ctx.author.id] #check if the userID is already in the dictionary
        except KeyError:
            x[ctx.author.id] = {} #if not, create it
        try:
            x[ctx.author.id][ctx.command.name] = x[ctx.author.id][ctx.command.name] + 1 #check if the command name already exists, if it does add + 1 usage to it
        except KeyError:
            x[ctx.author.id][ctx.command.name] = 1 # if it doesn't exist, set it to 1 for that command

但是,如果您想要永久使用它,则需要使用db并对其进行记录,将其插入命令本身或on_command

如果您希望仅在命令已成功执行(成功通过命令检查)[checks = has_permissions装饰器或@commands.check装饰器或类似装饰器] 时记录它,请使用on_command_completion

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM