-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord_bot.py
145 lines (118 loc) · 4.92 KB
/
discord_bot.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
# -*- coding: utf-8 -*-
# twitter: https://twitter.com/0xyue_web3
import requests
import json
import random
import time
import data
def check_mod(channel_id):
print("check_mod")
guild_id = data.guild_id
mod_role_id_list = data.mod_role_id_list
headr = {
"Authorization": data.authorization,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
}
get_message_url = "https://discord.com/api/v9/channels/{}/messages?limit=50".format(
channel_id
)
message_res = requests.get(url=get_message_url, headers=headr)
message_content_list = json.loads(message_res.content)
# with open("messages.json", "w") as file:
# json.dump(message_content, file)
for message_content in message_content_list:
author_id = message_content["author"]["id"]
# author_name = message_content["author"]["username"]
get_profile_url = "https://discord.com/api/v9/users/{}/profile?with_mutual_guilds=false&guild_id={}".format(
author_id, guild_id
)
print(f"get_profile_url: {get_profile_url}")
profile_res = requests.get(url=get_profile_url, headers=headr)
profile_content = json.loads(profile_res.content)
content = message_content["content"]
username = profile_content["user"]["username"]
user_roles = profile_content["guild_member"]["roles"]
# with open("profile_{}.json".format(username), "w") as file:
# json.dump(profile_content, file)
print(f"username: {username}")
print(f"user_roles: {user_roles}")
print(f"content: {content}")
for role in user_roles:
if role in mod_role_id_list:
print(f"mod({username}) 在说话,暂时停止! 说话内容:{content}")
return True
return False
def get_content():
if data.script_mode_flg:
return random.choice(data.script_text_list)
else:
return get_content_from_channel()
def get_content_from_channel():
channel_id = data.text_channel_id
headr = {
"Authorization": data.authorization,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
}
url = "https://discord.com/api/v9/channels/{}/messages?limit=100".format(channel_id)
res = requests.get(url=url, headers=headr)
result = json.loads(res.content)
result_list = []
for context in result:
if ("<") not in context["content"]:
if ("@") not in context["content"]:
if ("http") not in context["content"]:
if ("?") not in context["content"]:
result_list.append(context["content"])
return random.choice(result_list)
def chat():
channel_list = data.channel_id_list
authorization_list = data.authorization_list
for authorization in authorization_list:
header = {
"Authorization": authorization,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
}
for channel_id in channel_list:
if data.check_mod_flg and check_mod(channel_id):
continue
msg = {
"content": get_content(),
"nonce": "82329451214{}33232234".format(random.randrange(0, 1000)),
"tts": False,
}
url = "https://discord.com/api/v9/channels/{}/messages".format(channel_id)
try:
res = requests.post(url=url, headers=header, data=json.dumps(msg))
print(res.content)
except Exception:
print(Exception)
continue
if len(channel_list) > 1:
channel_sleep_time = random.randrange(
data.channel_sleep_time_min, data.channel_sleep_time_max
)
print(f"channel_sleep_time : {channel_sleep_time}")
time.sleep(channel_sleep_time)
# 多账号一起刷的时候,开启间隔时间
if len(authorization_list) > 1:
ac_sleep_time = random.randrange(
data.ac_sleep_time_min, data.ac_sleep_time_max
)
print(f"ac_sleep_time : {ac_sleep_time}")
time.sleep(ac_sleep_time)
if __name__ == "__main__":
while True:
try:
print("start")
chat()
# 机器人发送消息的间隔时间。
chat_sleeptime = random.randrange(
data.chat_sleep_time_min, data.chat_sleep_time_max
)
print(f"chat sleeptime:{chat_sleeptime}")
time.sleep(chat_sleeptime)
except:
pass