-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
184 lines (129 loc) · 5.05 KB
/
Copy pathbot.py
File metadata and controls
184 lines (129 loc) · 5.05 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
from http import client
import discord
from discord.ext import commands, tasks
import youtube_dl
import asyncio
from random import choice
import json
import os
if os.path.exists(os.getcwd() + "/config.json"):
with open("./config.json") as f:
configData = json.load(f)
else:
configTemplate = {"Token": "", "Prefix": "."}
with open(os.getcwd() + "/config.json", "w+") as f:
json.dump(configTemplate, f)
token = configData["Token"]
prefix = configData["Prefix"]
activity = discord.Activity(type=discord.ActivityType.listening, name="You")
bot = commands.Bot(command_prefix=".", activity=activity, status=discord.Status.idle)
bot.remove_command("help")
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'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 = data.get('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['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
@bot.event
async def on_ready():
print("Bot is Ready")
@bot.command()
async def info(ctx):
await ctx.send("ID: {}".format(ctx.guild.id))
@bot.command()
async def ping(ctx):
latency = round(bot.latency * 1000, 1)
await ctx.send(f"Pong! {latency}ms")
@bot.command()
async def hi(ctx):
await ctx.send(f"Hello There!")
@bot.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, * , reason = None):
await member.ban(reason=reason)
await ctx.send(f"{member} was banned!")
@bot.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, * , reason = None):
await member.kick(reason=reason)
await ctx.send(f"{member} was kicked!")
@bot.command()
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
bannedUsers = await ctx.guild.bans()
name, discriminator = member.split('#')
for ban in bannedUsers:
user = ban.user
if(user.name, user.discriminator) == (name, discriminator):
await ctx.guild.unban(user)
await ctx.send(f"{user.mention} was unbanned!")
return
@bot.command()
async def userinfo(ctx):
user = ctx.author
embed = discord.Embed(title = "USER INFO", description = f"The info I retrived about {user}", color = user.color)
embed.set_thumbnail(url=user.avatar_url)
embed.add_field(name="NAME", value=user.name, inline=True)
embed.add_field(name="NICKNAME", value=user.nick, inline=True)
embed.add_field(name="ID", value=user.id, inline=True)
embed.add_field(name="STATUS", value=user.status, inline=True)
embed.add_field(name="TOP ROLE", value=user.top_role.name, inline=True)
await ctx.send(embed=embed)
@bot.command()
async def help(ctx):
embed = discord.Embed(title="HELP")
embed.add_field(name="Ping", value="Gets the bot latency", inline=True)
embed.add_field(name="Hi", value="Greets the user" ,inline=True)
embed.add_field(name="Userinfo", value="Retreives the info of the user", inline=True)
embed.add_field(name="Kick", value="Kicks the user", inline=True)
embed.add_field(name="Ban", value="Bans the user", inline=True)
embed.add_field(name="Unban", value="Unbans the user", inline=True)
await ctx.message.delete()
await ctx.author.send(embed=embed)
@bot.command()
async def play(ctx, url):
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel!")
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
await ctx.guild.change_voice_state(channel=channel, self_mute=False, self_deaf=True)
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=bot.loop)
voice_channel.play(player, after=lambda e:print('Player Error: %s' %e) if e else None)
await ctx.send(f'**Now Playing:** {player.title}')
@bot.command()
async def stop(ctx):
voice_client = ctx.message.guild.voice_client
await voice_client.disconnect()
bot.run(token)