Discord.py-从消息反应转储到json文件的用户ID



我正在寻找一种方法,将消息中的所有用户反应存储在json文件中。我希望那些用";✅"存储在一个json文件中;❌"存储在一个单独的json文件中。

这是我遇到问题的代码部分:

for reactions in message.reactions:
if str(reactions) == "✅":
user_list = [user async for user in message.reactions.users() if user != client.user]
for user in user_list:
users = user.id + "n"

with open ("reaction_tick.json", "w") as f:
json.dump(users,f)
return True
for reactions in message.reactions:
if str(reactions) == "❌":
user_list = [user async for user in message.reactions.users() if user != client.user]
for user in user_list:
users = user.id + "n"

with open ("reaction_cross.json", "w") as f:
json.dump(users,f)
return True

如何收集所有用户的id并将其放入字符串中,然后将其转储到json文件中?

到目前为止,当我运行所有代码时,没有发生任何事情,也没有错误消息。

如有任何帮助,我们将不胜感激。

================================================================这是我的其余代码:

@client.event
async def on_member_join(member):

channel = client.get_channel(1003203232824307745)

embed=discord.Embed(title=f'{member.name}#{member.discriminator} has joined the server!', description="Will they stay or will they leave? Place your bets!", color=0xf1c40f, inline=True)
embed.add_field(name='Will they stay longer than 1 hour?', value=f'React stay ✅')
embed.add_field(name='Will they leave before 1 hour is up?', value=f'React leave ❌')
embed.add_field(name='Betting will close in:', value=f'5 minutes', inline=False)
embed.add_field(name='Results will be posted in', value=f'1 hour!', inline=True)

message = await channel.send(embed=embed)
await message.add_reaction('✅')
await message.add_reaction('❌')
time = 3600 
id = member.id

while time >= 0:
if time <= 0:
embed.remove_field(index=3)
embed.insert_field_at(index=3, name='Betting complete! Results have been posted!', value=f'Ended at {datetime.datetime.now().strftime("%B %d, %I:%M %p")}', inline=False)
await message.edit(embed=embed)
await membercheck(id, channel)
await reactioncheck(message)
return
elif 1 <= time < 50:
embed.remove_field(index=3)
embed.insert_field_at(index=3, name='Results in:', value=f'{time:.1f} seconds', inline=False)
await message.edit(embed=embed)
time -= 10
await asyncio.sleep(10)
elif 60 == time:
embed.remove_field(index=3)
embed.insert_field_at(index=3, name='Results in:', value=f'{time/60:.1f} minute', inline=False)
await message.edit(embed=embed)
time -= 10
await asyncio.sleep(10)
elif 60 <= time < 3290:
embed.remove_field(index=3)
embed.insert_field_at(index=3, name='Results in:', value=f'{time/60:.1f} minutes', inline=False)
await message.edit(embed=embed)
time -= 10
await asyncio.sleep(10)  
elif 3300 == time:
await reactioncheck(message) #This is the function that I was having trouble with above. 
embed.remove_field(index=2)
embed.insert_field_at(index=2, name='Betting has closed:', value=f'Ended at {datetime.datetime.now().strftime("%B %d, %I:%M %p")}', inline=False)
await message.edit(embed=embed)
await message.clear_reactions()
time -= 10
await asyncio.sleep(10)  
elif 3300 <= time < 3600:
await reactioncheck(message)
embed.remove_field(index=2)
embed.insert_field_at(index=2, name='Betting will close in:', value=f'{time/60 - 55:.1f} minutes', inline=False)    
embed.remove_field(index=3)
embed.insert_field_at(index=3, name='Results in:', value=f'{time/60:.1f} minutes', inline=False)
await message.edit(embed=embed)
time -= 10
await asyncio.sleep(10)
async def reactioncheck(message):        
rmsg = message #the message with reactions
for reaction in rmsg.reactions:
users = "" #the string to dump
if str(reaction) == "✅":
user_list = [user async for user in reaction.users() if user != client.user]
#it should be reaction.users() and not <message>.reactions.users()
for user in user_list:
users = users + str(user.id) + "n" #converting ID to str and adding instead of overwriting

with open("reaction_tick.json", "w") as f:
json.dump(users, f)
return True
for reaction in rmsg.reactions:
if str(reaction) == "❌":
user_list = [user async for user in reaction.users() if user != client.user]
for user in user_list:
users = users + str(user.id) + "n"

with open("reaction_cross.json", "w") as f:
json.dump(users, f)
return True

我知道这有很多问题,我知道我应该创建其他帮助程序函数来压缩这个混乱。我试图实现的总体要点是,当一个新成员加入我的服务器时,我的机器人会将嵌入发送到指定的频道。嵌入有一个计时器,它会不断更新,直到计时器结束。嵌入式有两个定时器,第一个持续5分钟,第二个持续1小时。当第一个计时器结束时,我希望我的机器人存储对嵌入做出反应的人的名字,然后从嵌入中清除反应。第二个计时器结束后,嵌入程序将发送对✅和❌表情符号。基本上,人们都在打赌新会员是在给定的一小时内留下还是离开服务器。

您在用于创建user_list的行中犯了一个愚蠢的错误。是reaction.users(),而不是<message>.reactions.users()。您正在尝试调用不存在的list对象的函数users()(list显然没有.users()函数(。换句话说,您试图在错误的对象上使用users()message.reactions是一个列表(它返回一个List[Reaction](。您正在尝试运行无效的list.users()。我已经更正了代码:

for reaction in message.reactions:
if str(reaction) == "✅":
user_list = [user async for user in reaction.users() if user != client.user]
#it should be reaction.users() and not <message>.reactions.users()
for user in user_list:
users = users + str(user.id) + "n" #converting ID to str and adding instead of overwriting

with open ("reaction_tick.json", "w") as f:
json.dump(users,f)
return True
for reaction in message.reactions:
if str(reaction) == "❌":
user_list = [user async for user in reaction.users() if user != client.user]
for user in user_list:
users = users + str(user.id) + "n"

with open ("reaction_cross.json", "w") as f:
json.dump(users,f)
return True

注意,我已经在for循环中用reaction替换了reactions(这样我就不会出错(。如果你愿意,你可以纠正它们;只要确保你在其他地方也能纠正它们。

以下是如何使用此代码的示例:

from discord.ext import commands
import discord.utils
import json
intent = discord.Intents(messages=True, message_content=True, guilds=True)
bot = commands.Bot(command_prefix="", description="", intents=intent)
@bot.event
async def on_ready(): #When the bot comes online
print("It's online.")
@bot.command(name="gr")
async def gr(message, msg):
if msg == "message":

rmsg = None #the message with reactions

async for msg in message.channel.history(limit=200):
if msg.content == "message":
rmsg = msg

for reaction in rmsg.reactions:
users = "" #the string to dump
if str(reaction) == "✅":
user_list = [user async for user in reaction.users() if user != bot.user]
#it should be reaction.users() and not <message>.reactions.users()
for user in user_list:
users = users + str(user.id) + "n" #converting ID to str and adding instead of overwriting

with open("reaction_tick.json", "w") as f:
json.dump(users, f)
return True
for reaction in rmsg.reactions:
if str(reaction) == "❌":
user_list = [user async for user in reaction.users() if user != bot.user]
for user in user_list:
users = users + str(user.id) + "n"

with open("reaction_cross.json", "w") as f:
json.dump(users, f)
return True
bot.run(<token>)

你可以在Discord上(手动(发送消息,并用表情符号做出反应(用于测试(。将"message"s替换为手动发送的消息的contentrmsg是消息(您也手动对此做出了反应(。users是要作为JSON转储的string。另外,我已经用users = users + str(user.id) + "n"替换了users = user.id + "n"。在for循环中,您的代码每次都会覆盖users。此外,您不能将string(在我的情况下为users(与integer(user.id(连接起来。

您没有收到任何错误或异常吗?因为我有很多。

最新更新