You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importloggingimportdiscordfromdiscord.extimporttasks, commandsfromdatetimeimportdatetimeimportjsonBotToken='xxxxxxxxxxxxxxxxxxxx111111111111111111111111111'intents=discord.Intents.all()
client=commands.Bot(command_prefix='/', intents=intents)
GUILD_ID=1234567890FORUM_ID_TO_MONITOR=1234567890UPPER_LIMIT=100# This function takes the global constant, GUILD_ID and FORUM_ID_TO_MONITOR, and returns a temp_forum object( by discord.py# standard) as the target to be monitored here.defalign_target_forum():
temp_guild=Nonetemp_forum=NoneforaGuildinclient.guilds:
ifaGuild.id==GUILD_ID:
temp_guild=aGuildforaForumintemp_guild.forums:
ifaForum.id==FORUM_ID_TO_MONITOR:
temp_forum=aForumiftemp_forumisNone:
print('Forum not found!')
else:
returntemp_forum# A class to represent a valid member-thread interaction, notice that any interactions by the same member will be considered# as one valid interaction, as a member could only agree/disagree once toward the same topic/proposal. Before this proposal# system is applied, both message and reaction(emoji) replies would be count once as well.classValidInteractionInThread:
def__init__(self, thread_id, member_id):
self.thread_id=thread_idself.member_id=member_iddef__eq__(self, other):
ifisinstance(other, ValidInteractionInThread):
returnself.thread_id==other.thread_idandself.member_id==other.member_idreturnFalseasyncdefget_current_valid_interactions_list(target_forum):
temp_valid_interactions_list= []
temp_all_threads=target_forum.threadsforaThreadintemp_all_threads:
# Append members that have interacted by sending a messagetemp_all_message_member_in_thread=awaitaThread.fetch_members()
foraMemberintemp_all_message_member_in_thread:
temp_valid_interaction=ValidInteractionInThread(aThread.id, aMember.id)
iftemp_valid_interactionnotintemp_valid_interactions_list:
temp_valid_interactions_list.append(temp_valid_interaction)
# Append members that have interacted by sending a reaction(emoji)temp_all_messages_within_thread= [messageasyncformessageinaThread.history()]
temp_all_reactions_within_thread= []
foraMessageintemp_all_messages_within_thread:
temp_all_reactions_within_thread.append(aMessage.reactions)
foraReactionsintemp_all_reactions_within_thread:
foraReactioninaReactions:
users= [userasyncforuserinaReaction.users()]
foraUserinusers:
temp_valid_interaction=ValidInteractionInThread(aThread.id, aUser.id)
iftemp_valid_interactionnotintemp_valid_interactions_list:
temp_valid_interactions_list.append(temp_valid_interaction)
returntemp_valid_interactions_list# Below is a list storing all valid interactions found since bot start running.past_valid_interactions_list= []
# A list storing the numbers of interactions newly found during EACH loop period, in int format, since bot start running.interactions_count_history= []
# A list storing all the recording datetime information, in str format, of EACH update.datetime_strings= []
# hours=24# A task which loops every day, updating newly found interactions.@tasks.loop(seconds=10)asyncdefupdate_valid_interaction_daily(target_forum):
current_time=datetime.now().strftime('%Y/%m/%d %H:%M:%S')
datetime_strings.append(current_time)
globalpast_valid_interactions_listiflen(past_valid_interactions_list) ==0:
past_valid_interactions_list=awaitget_current_valid_interactions_list(target_forum)
interactions_count_history.append(len(past_valid_interactions_list))
else:
current_valid_interactions_list=awaitget_current_valid_interactions_list(target_forum)
temp_valid_interactions_count=0foraInteractionincurrent_valid_interactions_list:
ifaInteractionnotinpast_valid_interactions_list:
past_valid_interactions_list.append(aInteraction)
temp_valid_interactions_count+=1interactions_count_history.append(temp_valid_interactions_count)
whilelen(interactions_count_history) <len(datetime_strings):
interactions_count_history.append(0)
whilelen(interactions_count_history) >UPPER_LIMIT:
interactions_count_history.pop(0)
datetime_strings.pop(0)
interaction_wrt_time= {'datetime_strings': datetime_strings, 'interactions_count_history': interactions_count_history}
withopen('Data/ForumActivitiesHistory/Interaction_WRT_Time.json', 'w') asf:
json.dump(interaction_wrt_time, f)
print(interactions_count_history, datetime_strings)
@client.command(name='forum_activities', description='get the last few days\' activities w.r.t time')asyncdefforum_activities(ctx):
withopen('Data/ForumActivitiesHistory/Interaction_WRT_Time.json', 'r') asf:
interaction_wrt_time=json.load(f)
temp_datetime_strings=interaction_wrt_time['datetime_strings']
temp_interactions_count_history=interaction_wrt_time['interactions_count_history']
output_str=''iflen(temp_datetime_strings) >10:
foriinrange(10, 0,-1):
output_str+='Activities found at '+temp_datetime_strings[-i] +' is:'+str(temp_interactions_count_history[-i])
else:
foriinrange(0,len(temp_datetime_strings)):
output_str+='Activities found at '+temp_datetime_strings[i] +' is:'+str(temp_interactions_count_history[i])
awaitctx.send(output_str)
@client.eventasyncdefon_ready():
print(f'We have logged in as {client.user}')
target_forum=align_target_forum()
update_valid_interaction_daily.start(target_forum)
client.run(BotToken)
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: