python - 遍历成员列表以识别同时创建的所有帐户(Python,discord.py)
问题描述
+我正在尝试制作一个 Discord 机器人,它获取垃圾邮件发送者之一的 ID,获取其创建日期,然后将结果与所有服务器成员列表进行比较,以制作垃圾邮件发送者的 ID 列表以 *massban 他们(因为他们经常同时创建)。
问题是它找不到任何人并打印“没有找到具有此类注册日期的其他用户”。(见第 30 行)在控制台中很多次。也许我错过了 break 但它不会在找到所有 ID 之前破坏搜索循环吗?
理想情况下,该脚本将允许一些时间公差(例如在第 23 行中 - 不仅是加号,还有减号。我发现最接近的是不确定性 Python 包,但它需要将日期时间对象转换为字符串才能操作,反之亦然。也许还有更方便的方法吗?)
Python 3.8.5 版
不和谐版本 1.0.1
discord.py 版本 1.5.1
#!/usr/bin/env python3
import asyncio
import discord
from discord.ext import commands
from datetime import datetime, timedelta
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('The bot is online')
@client.command()
@commands.has_any_role("Moderators", "Staff")
async def listids(ctx,member: discord.Member = None):
if member == None:
await ctx.send('No ID was provided. Please provide an ID.') #asking user to provide an ID
try:
timedeltaA = timedelta(0, 6000) #testing purposes. change to 60
timedeltaB = timedelta(0, 6000) #testing purposes. change to 60
creationtimeA = member.created_at + timedeltaA
creationtimeB = member.created_at - timedeltaB
print('Searching for accounts created between ' + str(creationtimeB) + ' and ' + str(creationtimeA))
#print(ctx.guild.members)
for x in ctx.guild.members: #parsing all members of the given server and comparing their creation dates against the aforementioned criteria
if x.created_at > creationtimeA and x.created_at < creationtimeB: #MAKE AND CHECK AND COMPARE AGAINST TWO CONDITIONS
accounts = ', '.join(x.id) #converting results to a string
await ctx.send('The following accounts were created at the same time: ' + accounts)
break
else:
await ctx.send('No additional users were found with such register date.')
print('No additional users were found with such register date.')
return
except Exception as e:
print(e)
return
client.run('token')
感谢您的宝贵时间,祝您有美好愉快的一天!;3
解决方案
与其使用 timedelta 和搞乱时间,不如使用内置的 datetime 支持。
creation_day = member.created_at.date
for x in ctx.guild.members:
if x.created_ad.date == creation_day:
print(x, member, 'are created in same day')
这将显示在同一天创建的所有帐户,如果您想要几小时或几分钟,您可能必须将日期时间转换为更可取的格式化字符串并进行检查。我不建议使用 timedelta 和 seconds。
推荐阅读
- javascript - 打字稿:如何在此文档中引用另一个文档的字段?
- php - 有没有更好的方法来使用 PHP Parser 删除除 Function_ 节点之外的所有其他代码?
- python - 解析文本文件并转换为 csv
- intellij-idea - intellij:数据库工具工具访问问题
- django - 静态文件 DJANGO
- python - 使用 Python 和 Boto3 从 S3 读取多个 CSV 文件
- powershell - 如何在计划任务中执行 Get-CrmConnection
- r - 返回顶点列表
- javascript - 如何通过 YouTube Data API 获取特定帐户(频道)发布的评论?
- javascript - 如何使用jquery触发dataTable搜索窗格的onclick事件?