-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
191 lines (151 loc) · 6.14 KB
/
app.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
#WaLLE
import discord
from discord.ext import commands,tasks
import os
from dotenv import load_dotenv
import youtube_dl
load_dotenv()
# Get the API token from the .env file.
DISCORD_TOKEN = os.getenv("discord_token")
intents = discord.Intents().all()
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!',intents=intents)
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = ""
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['title'] if stream else ytdl.prepare_filename(data)
return filename
@bot.command(name='play_song', help='To play song')
async def play(ctx,url):
if not ctx.message.author.name=="Rohan Krishna" :
await ctx.send('NOT AUTHORISED!')
return
try :
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
filename = await YTDLSource.from_url(url, loop=bot.loop)
voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
await ctx.send('**Now playing:** {}'.format(filename))
except:
await ctx.send("The bot is not connected to a voice channel.")
@bot.command(name='join', help='Tells the bot to join the voice channel')
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
@bot.command(name='pause', help='This command pauses the song')
async def pause(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_playing():
await voice_client.pause()
else:
await ctx.send("The bot is not playing anything at the moment.")
@bot.command(name='resume', help='Resumes the song')
async def resume(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_paused():
await voice_client.resume()
else:
await ctx.send("The bot was not playing anything before this. Use play_song command")
@bot.command(name='leave', help='To make the bot leave the voice channel')
async def leave(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_connected():
await voice_client.disconnect()
else:
await ctx.send("The bot is not connected to a voice channel.")
@bot.command(name='stop', help='Stops the song')
async def stop(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_playing():
await voice_client.stop()
else:
await ctx.send("The bot is not playing anything at the moment.")
@bot.event
async def on_ready():
print('Running!')
for guild in bot.guilds:
for channel in guild.text_channels :
if str(channel) == "general" :
await channel.send('Bot Activated..')
await channel.send(file=discord.File('giphy.png'))
print('Active in {}\n Member Count : {}'.format(guild.name,guild.member_count))
@bot.command(help = "Prints details of Author")
async def whats_my_name(ctx) :
await ctx.send('Hello {}'.format(ctx.author.name))
@bot.command(help = "Prints details of Server")
async def where_am_i(ctx):
owner=str(ctx.guild.owner)
region = str(ctx.guild.region)
guild_id = str(ctx.guild.id)
memberCount = str(ctx.guild.member_count)
icon = str(ctx.guild.icon_url)
desc=ctx.guild.description
embed = discord.Embed(
title=ctx.guild.name + " Server Information",
description=desc,
color=discord.Color.blue()
)
embed.set_thumbnail(url=icon)
embed.add_field(name="Owner", value=owner, inline=True)
embed.add_field(name="Server ID", value=guild_id, inline=True)
embed.add_field(name="Region", value=region, inline=True)
embed.add_field(name="Member Count", value=memberCount, inline=True)
await ctx.send(embed=embed)
members=[]
async for member in ctx.guild.fetch_members(limit=150) :
await ctx.send('Name : {}\t Status : {}\n Joined at {}'.format(member.display_name,str(member.status),str(member.joined_at)))
@bot.event
async def on_member_join(member):
for channel in member.guild.text_channels :
if str(channel) == "general" :
on_mobile=False
if member.is_on_mobile() == True :
on_mobile = True
await channel.send("Welcome to the Server {}!!\n On Mobile : {}".format(member.name,on_mobile))
# TODO : Filter out swear words from messages
@bot.command()
async def tell_me_about_yourself(ctx):
text = "My name is WallE!\n I was built by Kakarot2000. At present I have limited features(find out more by typing !help)\n :)"
await ctx.send(text)
@bot.event
async def on_message(message) :
# bot.process_commands(msg) is a couroutine that must be called here since we are overriding the on_message event
await bot.process_commands(message)
if str(message.content).lower() == "hello":
await message.channel.send('Hi!')
if str(message.content).lower() in ['swear_word1','swear_word2']:
await message.channel.purge(limit=1)
if __name__ == "__main__" :
bot.run(DISCORD_TOKEN)