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

时间:2021-03-09 20:14:20

标签: python discord discord.py

我在学生组织工作,那里有一个非常受欢迎的 Discord 服务器。我们想组织一个局域网派对,但我们不知道我们的成员最喜欢什么游戏 - 以最大限度地提高出席率。

我想我可以编写一个机器人来抓取服务器中人们玩的游戏的名称,以及他们的频率。这可以让我们了解在 LAN 派对上运行哪些游戏。

所以我的问题是,有没有办法记录人们玩什么以及玩了多少?这甚至可以使用 Discord API 吗?

关于这个噱头的法律方面,不管是否,让我们假装没事。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

首先,我想您想收集公会所有成员的当前活动,以便初步了解哪些游戏玩得最多。

例如,您可以实现一个 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")

那么您可能希望根据成员活动的变化来更新字典。这可以通过机器人事件来完成:

# 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 亲切建议的那样,必须指定状态和成员的 意图,因为它们不包含在默认意图中,并且分别需要管理活动和启用涉及公会成员的事件.

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