有没有办法收集我的朋友/人在Discord服务器上玩的游戏的数据?

2024-06-28 11:12:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我所在的学生组织有一个非常流行的Discord服务器。我们想组织一个局域网聚会,但是,我们不知道我们的会员最喜欢什么样的游戏——以最大限度地提高出席率

我在想我可以写一个机器人来抓取服务器上的玩家玩的游戏的名字,以及他们的频率。这可以让我们了解在局域网上运行什么游戏

所以我的问题是,有没有一种方法可以记录人们玩什么游戏以及玩多少游戏?这甚至可以用Discord API来记录吗

关于这一特技的法律方面,不管是否,让我们假装它很好


Tags: 方法服务器游戏记录玩家机器人名字学生
2条回答

它可以通过使用^{}事件来完成。 您只需跟踪他们的活动何时更改为某个游戏,记录时间,并记录他们停止玩或离线的时间

首先,我想你应该收集公会所有成员当前的活动,只是为了初步了解哪些游戏玩得最多

例如,您可以执行一个bot命令,该命令一旦在公会频道中运行,就会创建一个字典,将不同游戏的名称作为键,并将当前正在玩游戏的成员数量作为值。例如:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
intents.presences = True

bot = commands.Bot(command_prefix="!", intents=intents)

games = {}

# The following command iterates through the list of members in the guild, analyzes their activities
# and counts the number of members that play each game.
# The data is collected in the games dictionary and then is printed out in the console.

@bot.command()
async def activities(ctx):
    for member in ctx.guild.members:
        for activity in member.activities:
            if isinstance(activity, discord.Game):
                if activity not in games.keys():
                    games[activity] = 0
                games[activity] += 1

    for game, count in games.items():
        print(f"{game.name}: {count}")

bot.run("bot_token_here")

然后,您可能希望根据成员活动中的更改保持词典的更新。这可以通过bot事件来完成:

# The following event is called whenever a member changes their activities. 
# If there is a new activity and it's a game, the relevant counter is increased in games dict.
# If any activity was removed and it was a game, the counter is decreased.

@bot.event
async def on_member_update(before, after):
    old_activities = before.activities
    new_activities = after.activities
    for activity in new_activities:
        if activity not in old_activities and isinstance(activity, discord.Game):
            if activity not in games.keys():
                games[activity] = 0
            games[activity] += 1

    for activity in old_activities:
        if activity not in new_activities and isinstance(activity, discord.Game):
            games[activity] -= 1

在这里,我使用了一个字典来收集数据,但是建议使用一个外部文件来永久写入数据,或者更确切地说是一个数据库,否则一旦脚本中断,所有信息都将丢失

编辑:

正如Łukasz Kwieciński善意建议的那样,必须指定存在和成员的意图,因为它们不包括在默认意图中,并且分别用于管理活动和启用涉及公会成员的活动

需要注意的是,要使用这些特权意图,必须在开发者门户中激活应用程序的bot选项卡中的特权网关意图选项

相关问题 更多 >