-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·216 lines (180 loc) · 6.61 KB
/
bot.py
File metadata and controls
executable file
·216 lines (180 loc) · 6.61 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
# Work with Python 3.7+
from discord.ext import tasks
import discord
import logging
import json
from pycoingecko import CoinGeckoAPI
logging.basicConfig(format="%(asctime)s | %(levelname)s:%(name)s:%(message)s",
filename='cicerone.log', level=logging.INFO)
logging.info('----- Started -----')
with open("auth.json") as data_file:
auth = json.load(data_file)
with open("links.json") as data_file:
data = json.load(data_file)
TOKEN = auth["token"]
BOT_PREFIX = "!"
TICKER = "SIO"
intents = discord.Intents(messages=True, guilds=True)
intents.reactions = True
intents.members = True
intents.emojis = True
intents.presences = True
intents.typing = False
client = discord.Client(intents=intents)
cg = CoinGeckoAPI()
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def save_diary_file(message):
with open("dev-diary.json") as data_file:
listed = json.load(data_file)
listed.append(message)
file = open("dev-diary.json", "w")
file.write(json.dumps(listed, indent=2, sort_keys=True, default=str))
file.close()
@tasks.loop(minutes=15)
# task runs every 15 minutes
async def update_info_channels():
await client.wait_until_ready()
guild = client.get_guild(859581142159065128)
total_channel = client.get_channel(887310034969710623)
online_channel = client.get_channel(887310070088605766)
price_channel = client.get_channel(890480831653625917)
widget = await guild.widget()
online_members = len(widget.members)
total_members = guild.member_count
sol_price = cg.get_price(ids='solana', vs_currencies='usd')[
"solana"]["usd"]
await total_channel.edit(name=f"Total Members: {total_members}")
await online_channel.edit(name=f"Online Members: {online_members}")
await price_channel.edit(name=f"SOL Price: {sol_price}$")
@client.event
async def on_message(msg):
# Bot will save all the messages in #💻-dev-diary channel into a text file
if msg.channel.id == 882693892254859274:
dictionar = {}
dictionar["author"] = msg.author.name
dictionar["created_at"] = msg.created_at
dictionar["content"] = msg.content
for i in range(len(msg.embeds)):
key = "embed_" + str(i)
dictionar[key] = msg.embeds[i].to_dict()
message = dictionar
save_diary_file(message)
return
# We do not want the bot to respond to Bots or Webhooks
if msg.author.bot:
return
# We want the bot to not answer to messages that have no content
# (example only attachment messages)
# Bot checks BOT_PREFIX
if not msg.content or msg.content[0] != BOT_PREFIX:
return
# Bot ignore all system messages
if msg.type is not discord.MessageType.default:
return
args = msg.content[1:].split()
cmd = args[0].lower()
# Bot runs in #🤖-bot-commands channel and private channels for everyone
# Bot runs in all channels for specific roles
if not (
isinstance(msg.channel, discord.DMChannel)
or msg.channel.name == "🤖-bot-commands"
or "Core Team" in [role.name for role in msg.author.roles]
):
message = f"{data['default']}"
await msg.channel.send(message)
return
# -------- <help> --------
elif cmd == "help":
message = "\n".join(data["help"])
# -------- <about> --------
elif cmd == "about":
message = "\n".join(data["about"])
# -------- <ban(Amitabha only)> --------
elif (
cmd == "ban"
and isinstance(msg.channel, discord.TextChannel)
and msg.author.id == 359782573066551320
):
if len(args) < 2:
message = (
f"Input a substring of users to ban, like `!ban SiO` will ban all users containing"
+ f" `SiO` in their names (_fuction is case-sensitive_)."
)
await msg.channel.send(message)
return
cmd1 = args[1]
member = discord.utils.find(
lambda m: cmd1 in m.name, msg.channel.guild.members)
if member is None:
count = 0
else:
count = 1
while not (member is None):
await msg.channel.guild.ban(member)
member = discord.utils.find(
lambda m: cmd1 in m.name, msg.channel.guild.members)
if not (member is None):
count += 1
message = f"Banned {count} members! Nice!"
# -------- <del(Amitabha only)> --------
elif (
cmd == "del"
and isinstance(msg.channel, discord.TextChannel)
and msg.author.id == 359782573066551320
):
if len(args) < 2:
message = "Enter the number of messages to delete"
await msg.channel.send(message)
return
cmd1 = args[1]
deleted = await msg.channel.purge(limit=int(cmd1))
message = f"Deleted {len(deleted)} message(s)"
else:
message = f"{data['unknown']}"
await msg.channel.send(message)
@client.event
async def on_raw_reaction_add(payload):
guild = client.get_guild(859581142159065128)
role = guild.get_role(882347739357249576)
mbr = guild.get_member(payload.user_id)
if (payload.message_id == 882670328097169458 and payload.emoji.name == "Simplio"):
await mbr.add_roles(role)
@client.event
async def on_raw_reaction_remove(payload):
guild = client.get_guild(859581142159065128)
role = guild.get_role(882347739357249576)
mbr = guild.get_member(payload.user_id)
if (payload.message_id == 882670328097169458 and payload.emoji.name == "Simplio"):
await mbr.remove_roles(role)
@client.event
async def on_member_join(mbr):
# Bot ignore all members that have MEMBER_ID in ignored_ids list
if mbr.id in data["ignored_ids"]:
return
for ban_word in data["banned_words"]:
if mbr.guild.get_member(mbr.id) is not None and ban_word in mbr.name:
await mbr.ban()
logging.warning(f'Banned {mbr.name} with id: {mbr.id}')
return
@client.event
async def on_member_update(before, after):
# Bot ignore all members that have MEMBER_ID in ignored_ids list
if after.id in data["ignored_ids"]:
return
for ban_word in data["banned_words"]:
if after.guild.get_member(after.id) is not None and ban_word in after.name:
await after.ban()
logging.warning(f'Banned {after.name} with id: {after.id}')
return
@client.event
async def on_ready():
print(f"Logged in as: {client.user.name} {{{client.user.id}}}")
update_info_channels.start()
client.run(TOKEN)
logging.info('----- Finished -----')