如何在特定游戏中跟踪用户游玩时间 | discord.py

huangapple go评论69阅读模式
英文:

How to track user playtime in a specific game | discord.py

问题

我正在尝试设计一个机器人,它将跟踪服务器成员在本周内玩Rocket League的时长,并将在周末结束或在调用“!排行榜”命令时显示排行榜。我不确定如何开始,有没有人可以帮我找到正确的方法?我特别困惑如何检测用户何时开始玩Rocket League。

为了更详细地说明,机器人需要:

  1. 检测用户何时开始玩火箭联盟
  2. 在一周的时间段内跟踪用户的游戏时间
  3. 在命令下生成服务器成员的游戏时间排行榜
英文:

I'm trying to design a bot that would track how long server members have played Rocket League in the current week, and will display a leaderboard either at the end of the week or whenever a "!leaderboard" command is called. I'm not sure how to go about this, would anyone mind helping me get on the right track? I'm particularly struggling with how to detect when a user has started playing Rocket League.

For a bit more explanation, the bot needs to:

  1. detect when a user is playing rocket league
  2. track the users playtime during a period of one week
  3. generate a playtime leaderboard of server members on command

答案1

得分: 2

Discord的官方API似乎没有提供跟踪用户游戏活动时长的方法,但有一种方法可以绕过它。

Discord允许机器人通过更改用户状态来查看用户何时开始玩游戏。您可以检测状态的变化并启动一个计时器。当用户停止玩游戏(您可以定期轮询他们的状态来检查),您可以停止计时器并计算游戏时间。

这是您可以这样做的方式:

import discord
from discord.ext import commands
from datetime import datetime

intents = discord.Intents.default()
intents.presences = True 
bot = commands.Bot(command_prefix='!', intents=intents)

tracked_game = 'Rocket League'
timers = {}

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.event
async def on_member_update(before, after):
    # Check if the member started playing a game
    if (not before.activity or before.activity.name != tracked_game) and (after.activity and after.activity.name == tracked_game):
        timers[after.id] = datetime.now()
        print(f"{after} started playing {after.activity.name}")
    # Check if the member stopped playing a game
    elif before.activity and before.activity.name == tracked_game and (not after.activity or after.activity.name != tracked_game):
        start_time = timers.pop(after.id, None)
        if start_time:
            playtime = datetime.now() - start_time
            print(f"{after} played {tracked_game} for {playtime}")

@bot.command()
async def leaderboard(ctx):
    # TODO: Implement leaderboard functionality
    pass

bot.run('your bot token')

我将排行榜功能留给您,因为您需要一个数据库或某种存储方式来跟踪所有信息。您可以使用诸如SQLite或甚至普通的JSON / 文本文件之类的东西。

英文:

I believe Discord's official API does not provide a method to track a user's game activity duration, but there is a way around it.

Discord allows a bot to see when a user starts playing a game by changing their status. You can detect the change and start a timer. When the user stops playing the game (which you can check by polling their status at regular intervals), you can stop the timer and calculate the playtime.

This is how you could do this:

import discord
from discord.ext import commands
from datetime import datetime

intents = discord.Intents.default()
intents.presences = True 
bot = commands.Bot(command_prefix='!', intents=intents)

tracked_game = 'Rocket League'
timers = {}

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.event
async def on_member_update(before, after):
    # Check if the member started playing a game
    if (not before.activity or before.activity.name != tracked_game) and (after.activity and after.activity.name == tracked_game):
        timers[after.id] = datetime.now()
        print(f"{after} started playing {after.activity.name}")
    # Check if the member stopped playing a game
    elif before.activity and before.activity.name == tracked_game and (not after.activity or after.activity.name != tracked_game):
        start_time = timers.pop(after.id, None)
        if start_time:
            playtime = datetime.now() - start_time
            print(f"{after} played {tracked_game} for {playtime}")

@bot.command()
async def leaderboard(ctx):
    # TODO: Implement leaderboard functionality
    pass

bot.run('your bot token')

I have left the leaderboard functionality as you'll need a database or some sort of storage to keep track of everything. You could use something like SQLite or even just plain JSON / a txt file.

huangapple
  • 本文由 发表于 2023年6月1日 03:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定