-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
bot.py
279 lines (216 loc) · 8.97 KB
/
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import discord
from discord import Intents
import random
import time
import os
from scrapper import advscrape
from scrapper import scrape
from discord.ext import commands
# pylint: disable=unused-wildcard-import
from cogs.usefullTools.dbIntegration import *
intents = Intents.default()
# pylint: disable=assigning-non-slot
intents.members = True
bot = commands.Bot(command_prefix="cyb!",
case_insensitive=True, intents=intents)
bot.remove_command('help')
working_directory = os.getcwd()
try:
for filename in os.listdir('./cogs'):
if filename.endswith('.py') and filename != '__init__.py':
bot.load_extension(f"cogs.{filename[:-3]}")
except Exception as e:
print("Cogs error: Cannot load cogs")
print("\033[5;37;40m\033[1;33;40mWARNING\033[1;33;40m\033[0;37;40m", end=' ')
print("Functionality limited!\n")
print(f"exception thrown:\n{e}")
# Basic stuff
@bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.online, activity=discord.Game('cyb!help'))
print("+[ONLINE] Cyberon is online")
@bot.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
await channel.send("```Hello!I'm Cyberon, Thank you for inviting me!\nCommand Prefix: cyb!\nFor help in commands type: cyb! help```")
await channel.send("To start configuration of your server use `cyb!help config` command.")
break
@bot.event
async def on_member_join(member):
try:
print(
f'+[NEW_MEMBER] {member} has joined the server: {member.guild.name}')
channel = None
if fetch_join_log_channel(int(member.guild.id)) is not None:
channel = bot.get_channel(fetch_join_log_channel(
int(member.guild.id))["channel_id"])
if channel is not None:
embed = discord.Embed(
title='Member joined the server',
description=f'Member **{member.name}** joined the server!\nWelcome! My name is cyberon I will be here to help, just say `cyb!help` whenever needed ',
colour=0x008000
)
members = await member.guild.fetch_members().flatten()
bot_count = 0
for people in members:
if people.bot is True:
bot_count += 1
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name='Number of members',
value=len(members) - bot_count)
embed.add_field(name='Number of bots', value=bot_count)
embed.set_footer(text=f'id: {member.id}')
await channel.send(embed=embed)
else:
pass
except Exception as e:
raise Exception
@bot.event
async def on_member_remove(member):
try:
print(
f'+[REMOVE_MEMBER] {member} has left the server: {member.guild.name}')
delete_warns(member.guild.id, member.id)
channel = None
if fetch_leave_log_channel(int(member.guild.id)):
channel = bot.get_channel(fetch_leave_log_channel(
int(member.guild.id))["channel_id"])
if channel is not None:
embed = discord.Embed(
title='Member left the server',
description=f'Member **{member.name}** has left the server!\n:cry: I will miss you',
colour=0xFF0000
)
try:
members = await member.guild.fetch_members().flatten()
bot_count = 0
for people in members:
if people.bot is True:
bot_count += 1
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name='Number of members',
value=len(members) - bot_count)
embed.add_field(name='Number of bots', value=bot_count)
embed.set_footer(text=f'id: {member.id}')
await channel.send(embed=embed)
except:
pass
else:
pass
except Exception as e:
raise Exception
@bot.event
async def on_guild_channel_delete(channel):
join_channel = None
if fetch_join_log_channel(int(channel.guild.id)) is not None:
join_channel = fetch_join_log_channel(
int(channel.guild.id))["channel_id"]
if channel.id == join_channel:
delete_join_log_channel(int(channel.guild.id))
leave_channel = None
if fetch_leave_log_channel(int(channel.guild.id)) is not None:
leave_channel = fetch_leave_log_channel(
int(channel.guild.id))["channel_id"]
if channel.id == leave_channel:
delete_leave_log_channel(int(channel.guild.id))
log_channel = None
if fetch_mod_log_channel(int(channel.guild.id)) is not None:
mod_channel = fetch_mod_log_channel(
int(channel.guild.id))["channel_id"]
if channel.id == mod_channel:
delete_mod_log_channel(int(channel.guild.id))
@bot.event
async def on_guild_remove(guild):
clear_server_data(guild.id)
@bot.event
async def on_bulk_message_delete(messages):
message_channel = fetch_message_edit_log_channel(int(messages[0].guild.id))
if message_channel is not None:
message_channel = fetch_message_edit_log_channel(
int(messages[0].guild.id))["channel_id"]
message_channel = bot.get_channel(message_channel)
embed = discord.Embed(
title='Bulk message delete',
description=f'{len(messages)} messages deleted in {messages[0].channel.mention}',
color=0xff0000
)
if message_channel.id != messages[0].channel.id:
await message_channel.send(embed=embed)
@bot.event
async def on_message_delete(message):
message_channel = fetch_message_edit_log_channel(int(message.guild.id))
if message_channel is not None:
message_channel = fetch_message_edit_log_channel(
int(message.guild.id))["channel_id"]
message_channel = bot.get_channel(message_channel)
embed = discord.Embed(
title='Message deleted',
description=f'Message deleted in {message.channel.mention}\nContents:\n```\n{message.content}\n```\n'
f'Author of the message:\n{message.author.mention}',
color=0xff0000
)
if message_channel.id != message.channel.id:
await message_channel.send(embed=embed)
@bot.event
async def on_message_edit(before, after):
if not after.author.bot:
if before.content != after.content:
message_channel = fetch_message_edit_log_channel(
int(before.guild.id))
if message_channel is not None:
message_channel = fetch_message_edit_log_channel(int(before.guild.id))[
"channel_id"]
message_channel = bot.get_channel(message_channel)
embed = discord.Embed(
title='Message edited',
description=f'Message edited in {before.channel.mention}\nbefore:\n```\n{before.content}\n```\n\nAfter:\n```\n{after.content}\n```\n'
f'Author of the message:\n{after.author.mention}\n'
f'[jump](https://discordapp.com/channels/{after.guild.id}/{after.channel.id}/{after.id}) to the message',
color=0xff0000
)
if message_channel.id != before.channel.id:
await message_channel.send(embed=embed)
@bot.command()
async def search(ctx, *message):
query = (" ").join(message)
print(message)
URL = "https://www.google.com/search?q=" + query
item, link = scrape(URL)
await ctx.send(item.text)
await ctx.send(link)
@bot.command()
async def advsearch(ctx, *message):
query = (" ").join(message)
print(message)
URL = "https://www.google.com/search?q=" + query
item = advscrape(URL)
await ctx.send(item.text)
# Ping
@bot.command()
async def ping(ctx):
await ctx.send(f'Ping: {round(bot.latency * 1000)}ms')
# If the user enters something bonkers
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send(f"command not found\nPlease use `cyb!help` to see all commands")
TOKEN = os.getenv("BOT_TOKEN")
try:
if TOKEN is None:
try:
with open('./token.0', 'r', encoding='utf-8') as file_handle:
TOKEN = file_handle.read()
if TOKEN is not None:
print('Using token found in token file..')
bot.run(TOKEN)
else:
print("Token error: Token not found")
except FileNotFoundError:
print("No token file or environment variable\nQuitting")
else:
print('Using token found in Environment variable....')
bot.run(TOKEN)
except discord.errors.LoginFailure:
print(
"\033[1;31;40mFATAL ERROR\033[0m 1;31;40m\nToken is malformed; invalid token")