-
Notifications
You must be signed in to change notification settings - Fork 9
/
crawlandreact.py
160 lines (122 loc) · 5.13 KB
/
crawlandreact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# By Albert (Technobird22) for non profit. The model code is not mine
'''Respond to given model prompts'''
import os
import time
import random
import requests
import discord
from dotenv import load_dotenv
import presets
import interfaces
import utils
async def fetch(url, extension):
temp_id = 0
temp_filename = "temp/image_" + str(temp_id) + extension
while os.path.isfile(temp_filename):
print("Trying name:", temp_filename)
temp_id += 1
temp_filename = "temp/image_" + str(temp_id) + extension
print("Downloading image as '" + temp_filename + "'.")
try:
response = requests.get(url)
if response.status_code != 200:
raise()
file = open(temp_filename, "wb")
file.write(response.content)
file.close()
print("Successfully downloaded file.")
except:
print("Error downloading or saving file.")
raise
async def start_typing(message):
global client
await client.change_presence(activity=discord.Game(name='with AI | THINKING...'))
async with message.channel.typing():
time.sleep(0.1)
async def discord_announce(msg):
return
global client
await client.get_user(presets.OWNER_ID).send(msg)
for cur_channel in presets.announcement_channels:
if client.get_channel(cur_channel) == None:
print("ERROR! Channel '" + str(cur_channel) + "' not found!")
continue
await client.get_channel(cur_channel).send(msg)
def init_discord_bot():
global client, START_TIME
client.change_presence(activity=discord.Game(name='with AI | Connecting...'))
@client.event
async def on_ready():
joined_servers = "\n".join(("+ Connected to server: '" + guild.name + "' (ID: " + str(guild.id) + ").") for guild in client.guilds)
elapsed_time = str(round(time.time() - START_TIME, 1))
await client.get_user(presets.OWNER_ID).send("**Bot loaded in " + elapsed_time +" seconds! Time: " \
+ str(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())) + "**```diff\n" + joined_servers + "```")
print(joined_servers)
await client.change_presence(activity=discord.Game(name='with AI | READY'))
await discord_announce('**Bot is** `READY`!')
@client.event
async def on_message(message):
global history, TARGET_CHANNEL, rem_msgs
START_TIME = time.time()
if message.author == client.user:
return
# Crawl n' react ALL [messages]
if str(message.content) != ".cnra" or str(message.author) != presets.OWNER_TAG:
return
print("Crawlin...")
for chnl in message.guild.text_channels:
print('-'*30+"\nIn channel '" + chnl.name + "':")
try:
async for cur_message in chnl.history(limit=1, oldest_first=False):
pass
print("Perms sufficient!")
except:
print("Not allowed to access channel!\nSkipping...")
continue
async for cur_message in chnl.history(limit=100, oldest_first=False):
print('='*50)
print("Message: '", cur_message.content, "'; Current reactions:", cur_message.reactions)
if len(cur_message.reactions) != 0:
print("Skipping...")
continue
try:
if len(cur_message.attachments) != 0: # Contains attachment
print("Reacting...")
print("Received attachment(s):", cur_message.attachments)
IMAGE_EXTS = ['.png', '.jpg', '.jpeg', 'gif']
for attachment in cur_message.attachments:
if attachment.filename[-4:].lower() in IMAGE_EXTS:
print("Reacting to image:", str(attachment.url))
await utils.react_image(cur_message, attachment.url)
except discord.errors.HTTPException:
print("Error emote not found...")
pass
def start_all():
'''Start everything to run model'''
global client, TARGET_CHANNEL, START_TIME, SELF_TAG, history, rem_msgs
START_TIME = time.time()
rem_msgs = 0
history = "\n"
print("[INFO] Starting script...", flush=True)
print("[INFO] Initializing Discord stuff...", flush=True)
# Initialize discord stuff
load_dotenv()
client = discord.Client()
print("[OK] Initialized Discord stuff!", flush=True)
# Start Model
print("[INFO] Starting model...", flush=True)
interfaces.initialise()
print("[OK] Started model", flush=True)
# Run Discord bot
print("[INFO] Initializing Discord bot...", flush=True)
init_discord_bot()
print("[OK] Initialized Discord bot!", flush=True)
# Get discord tokens
print("[INFO] Getting Discord token...", flush=True)
token = os.getenv('DISCORD_TOKEN')
TARGET_CHANNEL = [103, 101, 110, 101, 114, 97, 108]
print("[OK] Got Discord token!", flush=True)
print("[OK] Running Discord bot...", flush=True)
client.run(token)
if __name__ == "__main__":
start_all()