-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
155 lines (131 loc) · 6.22 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
#bot.py
import os
import random
import discord
from discord.ext import commands
from dotenv import load_dotenv
# for saving the image
import requests
import uuid # for unique naming of the image
import shutil
# from local files
from dialogFiles.movieDialoguesEnglish import movieDialoguesEnglish
from dialogFiles.movieDialoguesTelugu import movieDialoguesTelugu
from dialogFiles.movieDialoguesHindi import movieDialoguesHindi
from dialogFiles.cussWords import cussWords
# dotenv library for parsing .env files
# loads the environment variables from .env file into shell environment variable
load_dotenv()
# Get the token from environment varible
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.members = True
# client - an object that represents the connection to discord
# ! client = discord.Client(intents = intents)
# convering all clients to bot
bot = commands.Bot(command_prefix='!',intents=intents)
# an event handler , which handles the event when connected to discord
@bot.event
async def on_ready():
print(f"{bot.user.name} has connected to discord!")
# ? The below code is for knowing the guilds the client is in and users in the guid
# print("Guild in which the client is there : ")
# for guild in client.guilds :
# print(f"\nname : {guild.name}, id : {guild.id}")
# GuildMembers = "\n - ".join([member.name for member in guild.members])
# print(f"Guild Members : \n - {GuildMembers}")
@bot.event
async def on_member_join(member):
await member.send(f"Welcome to Our Server {member.name.mention} 🤙🏻!!")
print(f"A new member {member.name} joined !")
# responding to messages from the channel in the guild
@bot.event
async def on_message(message):
if(message.author == bot.user):
return
if message.content.lower() in cussWords:
await message.channel.send(f'Well {message.author.mention}, you need to control your tongue .And you that {message.content} 🤣')
print(f"Warned {message.author.name} . ")
elif message.content.lower() == "raise-exception":
print("Exception raised !")
raise discord.DiscordException
# Warning the users using cussWords
else:
words = message.content.split()
for cuss in words:
if(cuss.lower() in cussWords):
await message.channel.send(f'Well {message.author.mention}, you need to control your tongue .And you are that {cuss} 👺')
print(f"Warned {message.author.name} . ")
break
# without the below line of code , bot commands dont work
await bot.process_commands(message)
# commands for the bot
@bot.command(name='get.dialog.telugu',help="gives you random telugu movie dialogues")
async def getDialoguesTelugu(ctx):
# ? @param ctx : context - contains the information of channel and guild from where the command came from
randomDialogue = f"{ctx.author.mention} be like : " + random.choice(movieDialoguesTelugu)
print(f"A Telugu dialogue replied to {ctx.author} ")
await ctx.send(randomDialogue)
@bot.command(name='get.dialog.english',help="gives you random english movie dialogues")
async def getDialoguesTelugu(ctx):
# ? @param ctx : context - contains the information of channel and guild from where the command came from
randomDialogue = f"{ctx.author.mention} be like : " + random.choice(movieDialoguesEnglish)
print(f"A Telugu dialogue replied to {ctx.author} ")
await ctx.send(randomDialogue)
@bot.command(name='get.dialog.hindi',help="gives you random hindi movie dialogues")
async def getDialoguesTelugu(ctx):
# ? @param ctx : context - contains the information of channel and guild from where the command came from
randomDialogue = f"{ctx.author.mention} be like : " + random.choice(movieDialoguesHindi)
print(f"A Telugu dialogue replied to {ctx.author} ")
await ctx.send(randomDialogue)
@bot.command()
async def test(ctx,*args):
# * here args variable is a tuple . variable no of arguments
print(f"Members in server : {ctx.guild} are {ctx.guild.members}")
await ctx.send(f"{len(args)} arguments : {','.join(args)} , server: {ctx.guild}")
# ! command for creating a new channel
# only with admin roles can create a channel
@bot.command(name="create_text_channel",help="Creates a new channel . Arg : nameOfTheChannel")
@commands.has_role('admin')
async def createTextChannel(ctx,channelName="None"):
if(channelName == "None"):
await ctx.send(f"{ctx.author.mention} , You need to specify the text-channel name ! !create_text_channel <Channel Name>")
guild = ctx.guild
# !checking whether the given channel exists
existing_channel = discord.utils.get(guild.channels,name=channelName)
if not existing_channel:
# create a new channel
print(f"Creating a text channel : {channelName}")
await guild.create_text_channel(channelName)
else :
await ctx.send(f"{ctx.author.mention} , A text channel with these name already exists!")
# ! test : Saving and Sending images
#bot commands for saving an uploaded image
@bot.command(name = "upload_image",help="Upload Your selfie to generate an amazing meme! ")
async def saveUploadedImage(ctx):
try:
# saving the image
imageName = str(uuid.uuid4()) + '.jpg'
# folderPath = 'uploadedPics/'
await ctx.message.attachments[0].save(imageName)
print(f"Saved uploaded Image successfuly : {imageName}")
await ctx.send(f"{ctx.author.mention},Image saved successfully !")
# sending an image back
await ctx.send("Here is your meme !",file = discord.File(imageName))
print(f"Sent a image : {imageName} to the channel !")
except IndexError :
print("No attachments of images")
await ctx.send(f"{ctx.author.mention}, there is no image uploaded !")
# handling exceptions
@bot.event
async def on_error(event,*args,**kwargs):
with open('err_log','a') as f:
print("error occured")
f.write(f"Unhandled Error : {args[0]} \n ")
# handling command errors
@bot.event
async def on_command_error(ctx,error):
if isinstance(error,commands.errors.CheckFailure):
await ctx.send(f"{ctx.author.mention} , You dont have the correct role for this !")
#run the client
bot.run(TOKEN)