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
它可以通过使用^{} 事件来完成。
您只需跟踪他们的活动何时更改为某个游戏,记录时间,并记录他们停止玩或离线的时间
首先,我想你应该收集公会所有成员当前的活动,只是为了初步了解哪些游戏玩得最多
例如,您可以执行一个bot命令,该命令一旦在公会频道中运行,就会创建一个字典,将不同游戏的名称作为键,并将当前正在玩游戏的成员数量作为值。例如:
然后,您可能希望根据成员活动中的更改保持词典的更新。这可以通过bot事件来完成:
在这里,我使用了一个字典来收集数据,但是建议使用一个外部文件来永久写入数据,或者更确切地说是一个数据库,否则一旦脚本中断,所有信息都将丢失
编辑:
正如Łukasz Kwieciński善意建议的那样,必须指定存在和成员的意图,因为它们不包括在默认意图中,并且分别用于管理活动和启用涉及公会成员的活动
需要注意的是,要使用这些特权意图,必须在开发者门户中激活应用程序的bot选项卡中的特权网关意图选项
相关问题 更多 >
编程相关推荐