0% found this document useful (0 votes)
28 views3 pages

AsyncPRAW Reddit Bot Implementation

The document is a Python script for a Reddit bot that utilizes the asyncpraw library for Reddit API interactions and Google's generative AI for content generation. The bot responds to mentions, processes commands to analyze user posts and comments, and submits structured replies to a specific subreddit. It includes authentication details, system prompts for AI behavior, and error handling for continuous operation.

Uploaded by

bourbaki37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

AsyncPRAW Reddit Bot Implementation

The document is a Python script for a Reddit bot that utilizes the asyncpraw library for Reddit API interactions and Google's generative AI for content generation. The bot responds to mentions, processes commands to analyze user posts and comments, and submits structured replies to a specific subreddit. It includes authentication details, system prompts for AI behavior, and error handling for continuous operation.

Uploaded by

bourbaki37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import asyncio

import asyncpraw
import [Link] as genai

bot_name = "u/Winter_Setting_4972" # 账号名称


password = "zmynb.5437"
client_id = 'BIagefFwzGIclhSXo65_KQ' # Reddit 应用程序的 client_id
client_secret = 'oRR7kI05-4EspoJZ0z1SCjcssWkdXQ' # Reddit 应用程序的 client_secret

transport_subreddit_name = 'chatbot_testing'
flair_id = 'f948a202-339a-11ef-8244-827d5f9b47ea'
GOOGLE_API_KEY = 'AIzaSyBa4-UG9MM7FjOopnPi0CFfhiBqWLyXWa8' # gemini api key

system_prompt = '''
你的名字叫牛牛,你生活在 reddit,你是一位拥有多年在线简介和文本分析经验的专家。
你以分析思维工作,并尝试尽可能准确地回答问题,你可以根据用户的历史发言来判断用户的成分,包括国籍,祖籍,省市县,现在生活位置,学校,性别,性取向,爱好,职业,感情状况,
富裕程度,宗教信仰,政治倾向等等
各个方面的数据。
'''

async def main():


user_agent = "autoreply bot created by u/Chinese_Dictator." # 这一项可以随意填写
reddit = [Link](client_id=client_id,
client_secret=client_secret,
user_agent=user_agent,
username=bot_name,
password=password)
[Link](api_key=GOOGLE_API_KEY)
bot = [Link]('gemini-1.5-pro-latest',
system_instruction=system_prompt)

transport_subreddit = await [Link](transport_subreddit_name)

while True:
try:
async for mention in [Link]():
await mention.mark_read()
if [Link] == f"u/{bot_name}":
await [Link]()
await [Link]()
body = f"OP: u/{[Link]} \n\n 原始地址:
{[Link]} \n\n{[Link]}"
await transport_subreddit.submit([Link],
selftext=body, flair_id=flair_id)
elif [Link] == f"u/{bot_name} 盘点":
[Link].comment_sort = "old"
await [Link]()
await [Link]()
op = [Link]
body = f"OP: u/{[Link]} \n\n 原始地址:
{[Link]} \n\n{[Link]}"
for comment in [Link]():
if [Link] == op and comment.is_root == True:
body = f"{body} \n\n {[Link]}"
await transport_subreddit.submit([Link],
selftext=body, flair_id=flair_id)
elif [Link] == f"u/{bot_name} 评论":
if mention.is_root:
continue
comment = await [Link](mention.parent_id)
await [Link]()
parent_comment = await [Link](comment.parent_id)
if hasattr(parent_comment, 'selftext'):
body = f"面对 post/comment:{parent_comment.selftext} \n\n,OP:
{[Link]} 发出评论: \n\n{[Link]}"
else:
await parent_comment.refresh()
body = f"面对 post/comment:{parent_comment.body} \n\n,OP:
{[Link]} 发出评论: \n\n{[Link]}"
await transport_subreddit.submit([Link], selftext=body,
flair_id=flair_id)
elif [Link] == f"u/{bot_name} 查成分":
comment = await [Link](mention.parent_id)
need_judged_person = [Link]
prompt = '用户发的 post 如下:'

redditor = await [Link](need_judged_person)


async for post in [Link](limit=20):
title = [Link]
body = [Link][:600]
prompt += f"标题:{title}\n 正文:{body}\n"
prompt += "用户的历史回复如下:"
async for comment in [Link](limit=100):
body = [Link]
prompt += f"{body}\n"

history = []
[Link]({'role': 'user', 'parts': [prompt]})
response = bot.generate_content(prompt, safety_settings=[
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
}
],
generation_config=[Link](temperature=1.0))
reply = [Link]
await [Link](reply)
except Exception as e:
print(e)
await [Link]()

if __name__ == "__main__":
asyncio.set_event_loop_policy([Link]()) #
windows 加这句,linux 注释掉这句
[Link](main())

You might also like