-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
157 lines (130 loc) · 5.56 KB
/
main.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
import subprocess
subprocess.call(['pip', 'install', 'dnspython'])
import discord
from discord.ext import commands
from Resources import server as sr
from Resources import meme_db as me
from Resources import config
from discord.ext.commands import when_mentioned_or
import random
from cogs import levelsys, fact_cog, joke_cog, meme_cog, motivation_cog, help_cog, misc_cog
'''
this discord library revolves around the concept of event.
event is something you listen to and the respond to event.
for example : when a message happen in discord you will receive an
event about it than you respond to it.
discord.py is asynchronous library so things are done with callbacks
callback is function that is called when something else happen
'''
cogs = [levelsys, fact_cog, joke_cog, meme_cog, motivation_cog, help_cog, misc_cog]
roles_meme = ['NoobMemer', 'MemeRular', 'MemeStar', 'AlphaMemer']
client_obj = config.Database_oauth()
TOKEN , OWNER_IDS = client_obj.discord_TOKEN()
db_client = client_obj.database_info()
def get_prefix(bot, message):
db = db_client['meme']
collection = db["server_info"]
data = collection.find_one({'server_id': message.guild.id})
if data is None:
print('Server Not found')
else:
prefix = data['command_prefix']
return when_mentioned_or(prefix)(bot, message)
client = commands.Bot(command_prefix=get_prefix, aliases=['PLS ', 'Pls ', 'pLs ', 'plS '],
owner_ids=OWNER_IDS, help_command=None) # create client to interact with bot
def full_run():
for i in range(len(cogs)):
cogs[i].setup(client)
@client.command(name='setup')
@commands.has_permissions(manage_roles=True)
async def setup(message): # when member type --setup command
guild = message.guild
channel_name = "joke-and-fact"
text_channel_list, roles_list = sr.get_server_info(guild.name) # used to get server information
if channel_name in text_channel_list:
embed = discord.Embed(
title='Failed',
description="joke & fact channel already exist",
colour=0xff0000)
await message.send(embed=embed)
else:
await guild.create_text_channel(name=channel_name) # create text channel in server
sr.update_channel_info(channel_name, guild.name)
embed = discord.Embed(
title='Success',
description="joke & fact channel has been successfully created.",
colour=0xff0000)
await message.send(embed=embed)
# to create role for meme
for role in roles_meme:
if role in roles_list:
continue
else:
await guild.create_role(name=role, colour=discord.Colour.random())
sr.update_server_roles_info(roles_meme, guild.name)
@client.event
async def on_guild_channel_delete(channel):
channel_name = channel.name
guild = channel.guild
sr.update_server_info(guild.name, channel_name)
@client.event
async def on_guild_channel_create(channel):
channel_name = channel.name
guild = channel.guild
sr.update_server_info(guild.name, channel_name)
@client.event
async def on_connect():
print(" bot connected")
@client.event
async def on_disconnect():
print("bot disconnected")
@client.event
async def on_member_join(member): # when member join the server
for channel in member.server.channels:
if str(channel) == 'general':
await member.server.channel.send(f"""Welcome to the server {member}""")
@client.event # to register an event
async def on_ready(): # this will call when bot is ready to use
print('we have logged in as {0.user}'.format(client))
for guild in client.guilds:
server_id = guild.id
server_name = guild.name
channel_guild = guild.text_channels
role_guild = guild.roles
text_channels_list = []
roles_list = []
for role in role_guild:
roles_list.append(role.name)
for channel in channel_guild:
text_channels_list.append(channel.name)
sr.add_server_info(server_id, server_name, text_channels_list, roles_list)
def colour_generator():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return discord.Colour.from_rgb(r, g, b)
@client.event
async def on_message(message):
channels = ['joke-and-fact']
if message.author == client.user:
return None
if message.channel.name in channels:
if message.content.startswith('meme'):
msg = message.content.split(' ')
try:
page, title, url = me.single_meme(msg[1])
if page is None:
embed = discord.Embed(title=title, colour=colour_generator())
embed.set_image(url=url)
await message.channel.send(embed=embed)
else:
embed = discord.Embed(title=title, url=url, colour=colour_generator())
embed.set_image(url=url)
embed.set_footer(text="r/" + page)
await message.channel.send(embed=embed)
except:
print("Not meme page name")
await client.process_commands(message) # code to execute commands
client.run(TOKEN)
if __name__ == "__main__":
full_run()