This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
306 lines (258 loc) · 10.4 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import discord
import datetime
import time
import random
import json
import os
# Import the extension for discord.py - discord.ext command
from discord.ext import commands
# import discord member, used in warning -- import Embed to polish messages
from discord import Member, Embed
from math import sqrt, cos, sin # import more math functions
bot_warnings_file = "./.botmem/warnings.json"
about_embed = "./embeds/about.json"
help_embed = "./embeds/help.json"
prefix = "m/" # Set the prefix. e.g "!sb "
bot = commands.Bot(command_prefix=prefix) # Define what bot is
# Remove the default help command from the Discord.py commands lib.
bot.remove_command('help')
botver = "1.3.1 [beta]" # Set the bot version number.
functions = ['+', '-', '*', '/', 'sqrt', 'cos', 'sin'] # math functions
start_time = time.time() # Starts the timer for the uptime of the bot.
# create bot memory structure
if not os.path.isdir("./.botmem"):
os.mkdir("./.botmem")
if not os.path.exists(bot_warnings_file):
open(bot_warnings_file, "w+")
if open(bot_warnings_file, "r").read() == "":
warnings = {}
else:
warnings = json.loads(open(bot_warnings_file).read())
##############################
@bot.event # When the bot first loads up
async def on_ready():
bot.add_cog(Music(bot))
print(""" _______
|| _ ||
|| | | || version {} """.format(botver))
print('The bot has logged in as {0.user}'.format(
bot)) # Say that [bot name] is logged on in the terminal
# Change the bot activity
await bot.change_presence(activity=discord.Game(name=f"m/help - {botver}"))
@bot.command() # Help command. This will give you all of the commands
async def help(ctx):
e = Embed(title="help", description="help command", name="helpCommands")
for field in json.loads(open(help_embed).read()):
e.add_field(name=field[name], value=field[value])
await ctx.send(embed=e)
# About command. This includes; Bot latency, Bot guild number, Bot uptime, Bot version.
@bot.command()
async def about(ctx):
# Get the latency of the bot
# round rounds a decimal number into the nearest whole number. bot.latency is given as a decimal. (bot.latency x 100) = the time in ms.
latency = round(bot.latency * 100)
# Get the number of guilds the bot is in
guilds = len(bot.guilds) # Where len is length of the array for bot.guilds
# Get the bot uptime
current_time = time.time() # Sets current time to the time.
# Takes away the current time from the start time (rounded)
difference = int(round(current_time - start_time))
# Calculates the bot uptime and displays the difference.
botuptime = str(datetime.timedelta(seconds=difference))
# Send all the about statistics to the user
e = Embed(title="About",
description="About and statistics\n About Minimal:", name="aboutCommand")
for field in json.loads(open(about_embed).read()):
e.add_field(name=eval(field[name]), value=eval(field[value]))
e.add_field(name="Ping", value=f"Ping: {latency}ms ")
e.add_field(name="Uptime", value=f"Uptime: {botuptime}")
e.add_field(name="Version", value=f"Version: {botver}")
e.add_field(name="Serving", value=f"Minimal is serving {guilds} servers")
e.add_field(name="Credits",
value="Made with discord.py Created by Cob:web Development: \n https://cob-web.xyz/discord/'")
await ctx.send(embed=e) # Shows all the output for the about command
@bot.command() # calculate command
async def calculate(ctx, *args):
command = ""
cont = False
for i in args:
for j in functions:
if j in i:
command += i
cont = True
if cont:
cont = False
continue
try:
float(i)
command += i
except Exception as e:
e = Embed(title="Error", name="error")
e.add_field(name="Calculation Error:", value=e)
ctx.send(embed=e)
break
e = Embed(title="Calculation", name="calculateCommand")
e.add_field(name="Answer", value=str(eval(command)))
await ctx.send(embed=e)
@bot.command(pass_context=True)
@commands.has_permissions(ban_members=True)
async def warn(ctx, user: Member): # warning a member
warn_mem = ctx.message.mentions[0]
server_id = ctx.message.guild.id
try:
warnings[str(server_id)]
except KeyError:
warnings[str(server_id)] = {}
try:
warnings[str(server_id)][str(warn_mem.id)] += 1
except KeyError:
warnings[str(server_id)][str(warn_mem.id)] = 1
open(bot_warnings_file, "w+").write(json.dumps(warnings))
e = Embed(title="Warning", name="warnCommand")
e.add_field(
name="Warn", value=f"User {warn_mem} has {warnings[str(server_id)][str(warn_mem.id)]} warning(s)")
await ctx.send(embed=e)
@warn.error
async def warn_error(ctx, user: Member):
await ctx.send("missing permissions")
@bot.command(pass_context=True)
@commands.has_permissions(ban_members=True)
async def unwarn(ctx, user: Member): # unwarning a member
warn_mem = ctx.message.mentions[0]
server_id = ctx.message.guild.id
try:
warnings[str(server_id)]
except KeyError:
warnings[str(server_id)] = {}
try:
warnings[str(server_id)][str(warn_mem.id)] -= 1
except KeyError:
e = Embed(title="Unwarning", name="unwarnCommand")
e.add_field(name="Unwarn",
value=f"User {warn_mem} has 0 warnings")
await ctx.send(embed=e)
return
open(bot_warnings_file, "w+").write(json.dumps(warnings))
e = Embed(title="Unwarning", name="unwarnCommand")
e.add_field(
name="Unwarn", value=f"User {warn_mem} has {warnings[str(server_id)][str(warn_mem.id)]} warning(s)")
await ctx.send(embed=e)
@unwarn.error
async def unwarn_error(ctx, user: Member):
await ctx.send("missing permissions")
@bot.command(pass_context=True)
async def status(ctx, user: Member): # warning status of member
warn_mem = ctx.message.mentions[0]
server_id = ctx.message.guild.id
try:
warnings[str(server_id)]
e = Embed(title="Status of user",
description="Status", name="statusCommand")
e.add_field(
name="Status", value=f"User {warn_mem} has {warnings[str(server_id)][str(warn_mem.id)]} warning(s)")
await ctx.send(embed=e)
except KeyError as e:
e = Embed(title="Status of user",
description="Status", name="statusCommand")
e.add_field(name="Status",
value=f"User {warn_mem} has 0 warnings")
await ctx.send(embed=e)
class Music(commands.Cog):
"""
MusicPlayer
- contains queue and ways to interact with it
"""
def __init__(self, bot):
self.queue = []
self.stop = False
self.skip = False
self.bot = bot
# private
async def _waitForSong(self, expected_duration):
# wait for song to finish and
# check if skip was called.
import asyncio
st = time.time()
while time.time() - st < expected_duration:
if self.skip:
return
await asyncio.sleep(1)
# public
@commands.command(name="join")
async def join(self, ctx):
channel = ctx.author.voice.channel
await channel.connect()
@commands.command(pass_context=True)
async def leave(self, ctx):
# leave from voice channel
try:
server = ctx.message.guild.voice_client
await server.disconnect()
except RuntimeError:
pass
@commands.command(pass_context=True)
async def enqueue(self, ctx, url):
# enqueue a song to play
self.queue.append(url)
@commands.command()
async def shuffle(self, ctx):
# shuffle queue
random.shuffle(self.queue)
@commands.command()
async def stop(self, ctx):
# stop queue from playing
self.stop = True
ctx.voice_client.stop()
@commands.command()
async def skip(self, ctx):
# skip a song
self.skip = True
ctx.voice_client.stop()
@commands.command()
async def show(self, ctx):
await ctx.send(str(self.queue))
@commands.command(pass_context=True)
async def play(self, ctx):
if not ctx.voice_client:
# if we are not connected to a
# voice channel, connect to
# the author's channel.
channel = ctx.author.voice.channel
await channel.connect()
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL
YDL_OPTIONS = {'format': 'bestaudio',
'noplaylist': 'True', 'quiet': 'True'}
FFMPEG_OPTIONS = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = ctx.voice_client
if len(self.queue) == 0: # queue is empty
await ctx.send("you need to enqueue a video to start playing")
else:
while len(self.queue) > 0: # iterate over all queue
# until it has nothing in it
if self.skip:
# make sure we skip
self.skip = False
ctx.voice_client.stop()
continue
if self.stop:
# make sure we stop
self.stop = False
ctx.voice_client.stop()
break
url = self.queue.pop(0)
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
URL = info['formats'][0]['url']
# info embed
e = Embed(title="Video Playing",
description="", name="playCommand")
e.add_field(name="title", value=info["title"])
await ctx.send(embed=e)
voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
await self._waitForSong(info['duration'])
@bot.event # When there is a message sent
async def on_message(message):
await bot.process_commands(message) # Process the message into a command
bot.run('') # The bot "password", this is needed to connect to the account.