-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
183 lines (168 loc) · 8.74 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
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
#Necessary libraries to import
import discord
from discord.ext import commands
import os
import qbittorrentapi
import plexapi
from plexapi import myplex
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
from plexapi.settings import Settings
import shutil
import asyncio
#Place your bot's token here, or put it in a .env file to be referenced.
TOKEN = '<YOUR TOKEN HERE>'
#initialize the bot's prefix to be '!', can be changed if desired.
bot = commands.Bot(command_prefix='!')
#removes the default help command
bot.remove_command('help')
#declaration of custom help command
@bot.group(invoke_without_command=True)
async def help(ctx):
em = discord.Embed(title="Help", description='Use !help <command> for extended information on a specific command.')
em.add_field(name='Torrents Commands', value='`addtor`, `viewtorrents`, `checktor`')
em.add_field(name='Plex Commands', value='`updateplex`')
em.add_field(name='PC Commands', value='`restartpc`, `checkstorage`')
await ctx.channel.send(embed=em)
#!addtor
#DOCUMENTATION:
#Use - Adds a torrent to begin downloading in QBitTorrent.
#Syntax - !addtor <TORRENT INFO HASH> <DIRECTORY>
#Notes - Remember to fill out the parameters for <QBITTORRENT USERNAME> and <QBITTORRENT PASSWORD>, else the command will not work properly.
@bot.command(aliases=['toradd', 'addtorrent', 'torrentadd'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def addtor(ctx, torHash, filePath):
print('Adding torrent with hash', torHash, 'to qBitTorrent downloads...')
cli = qbittorrentapi.Client(host='localhost:8080', username = '<QBITTORRENT USERNAME>', password = '<QBITTORRENT PASSWORD>')
try:
cli.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
print(f'qBittorrent: {cli.app.version}')
cli.torrents_add(urls=torHash, save_path=filePath)
print('Added successfully.')
await ctx.channel.send('Download begun. Use `!viewtorrents` to check the status.')
@help.command()
async def addtor(ctx):
em = discord.Embed(title='Add Torrent', description='Takes a torrent hash and save path from the user and starts downloading the contents to the host PC. <torHash> should be a valid torrent info hash, and <directory> should be a valid directory on the host PC.', color=ctx.author.color)
em.add_field(name='**Syntax**', value='!addtor <torrent hash> <directory>')
em.add_field(name='**Example directories**', value='''C:\Users\<username>\Downloads
D:\TorrentDownloads\MyCollection''')
em.add_field(name='**Aliases**', value='`!addtor`, `!addtorrent`, `!toradd`, `!torrentadd`')
await ctx.channel.send(embed=em)
#!checktor
#DOCUMENTATION:
#Use - Checks the status of a specified torrent. Status taken directly from the dictonary of the torrent info, so it may be very hard to read.
#Syntax - !checktor <TORRENT INFO HASH>
#Notes - Remember to fill out the parameters for <QBITTORRENT USERNAME> and <QBITTORRENT PASSWORD>, else the command will not work properly.
@bot.command(aliases=['checktorrent'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def checktor(ctx, torHash):
cli = qbittorrentapi.Client(host='localhost:8080', username = '<QBITTORRENT USERNAME>', password = '<QBITTORRENT PASSWORD>')
try:
cli.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
await ctx.channel.send(str(cli.torrents_info(torrent_hashes=torHash)))
@help.command()
async def checktor(ctx):
em = discord.Embed(title='Check Torrent', description='Displays an enormous text wall of the torrent info dictionary, telling everything about a specified torrent. Hard to read, only to be used by those who know what they\'re looking at.')
em.add_field(name='**Syntax**', value='!checktor <torrent hash>')
em.add_field(name='**Aliases**', value='`!checktor`, `!checktorrent`')
await ctx.channel.send(embed=em)
#!updateplex
#DOCUMENTATION:
#Use - Updates a plex account to refresh all libraries on a given server.
#Notes - Make sure the parameters <PLEX USERENAME>, <PLEX PASSWORD>, and <PLEX SERVER NAME> are filled out correctly, else the command will not work.
@bot.command(aliases=['up','plexupdate'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def updateplex(ctx):
account = MyPlexAccount('<PLEX USERNAME>', '<PLEX PASSWORD>')
plex = account.resource('<PLEX SERVER NAME>').connect()
plex.library.update()
await ctx.channel.send('Updated plex successfully. If new files do not show up, use !restartpc.')
@help.command()
async def updateplex(ctx):
em = discord.Embed(title='Update Plex', description='Updates the Plex media listings in the case that newly torrented files are not showing up. May require a PC reboot.', color=ctx.author.color)
em.add_field(name='**Aliases**', value='`!updateplex`, `!up`, `!plexupdate`')
await ctx.channel.send(embed=em)
#!restartpc
#DOCUMENTATION:
#Use - Restarts the host PC after waiting three seconds.
@bot.command(aliases=['rpc', 'resetcomputer'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def restartpc(ctx):
await ctx.channel.send("Restarting computer... Be back shortly!")
os.system("shutdown /r /t 3")
@help.command()
async def restartpc(ctx):
em = discord.Embed(title='Restart PC', description='Restarts the host PC in which the bot is running from.', color=ctx.author.color)
em.add_field(name='**Aliases**', value='`!restartpc`, `!rpc`, `!restartcomputer`')
await ctx.channel.send(embed=em)
#!viewtorrents
#DOCUMENTATION:
#Use - Neatly displays the file location and name of all completed torrents, as well as the percent complete of all in progress torrents.
#Notes - Remember to fill out the parameters for <QBITTORRENT USERNAME> and <QBITTORRENT PASSWORD>, else the command will not work properly.
@bot.command(aliases=['vt','torrentstatus','downloadstatus'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def viewtorrents(ctx):
cli = qbittorrentapi.Client(host='localhost:8080', username = '<QBITTORRENT USERNAME>', password = '<QBITTORRENT PASSWORD>')
try:
cli.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
cStr = ''
dStr = ''
completeStr = cli.torrents_info(status_filter='completed')
downloadStr = cli.torrents_info(status_filter='downloading')
print(downloadStr)
cStr += '```COMPLETED:\n'
if (not completeStr):
sendStr += 'None.'
for i in range(len(completeStr)):
cStr += 'Location and Name:\t'
cStr += (completeStr[i]['content_path']) + '\n'
cStr +='```'
dStr += '```DOWNLOADING:\n'
if (not downloadStr):
dStr += 'None.'
for j in range(len(downloadStr)):
dStr += 'Location and Name:\t'
dStr += (downloadStr[j]['content_path']) + '\n'
dStr += 'Progress Decimal Percent:\t'
dStr += str((downloadStr[j]['progress'])) + '\n'
dStr += '```'
await ctx.channel.send(cStr)
await ctx.channel.send(dStr)
@help.command()
async def viewtorrents(ctx):
em = discord.Embed(title='View Torrents', description='View status of all current torrents.', color=ctx.author.color)
em.add_field(name='**Aliases**', value='`!viewtorrents`, `!vt`, `!torrentstatus`, `!downloadstatus`')
await ctx.channel.send(embed=em)
#!checkstorage
#DOCUMENTATION:
#Use - Checks the total, used, and free space of a given drive.
#Syntax - !checkstorage <DRIVE NAME>
@bot.command(aliases=['storage','checkspace','space','checkdirectory','dirspace'])
#@commands.has_role('<ROLE NAME>') : Add if you'd like to restrict this command to a specific role.
async def checkstorage(ctx, drivename):
sendStr = '```Space remaining on disk ' + drivename + '\n'
total, used, free = shutil.disk_usage(drivename)
sendStr += ("Total %d GiB" % (total // (2**30))) + '\n'
sendStr += ("Used %d GiB" % (used // (2**30))) + '\n'
sendStr += ("Free %d GiB" % (free // (2**30))) + '```'
await ctx.channel.send(sendStr)
@help.command()
async def checkstorage(ctx):
em = discord.Embed(title='Check Storage', description='Returns the total storage, used storage, and free storage of a given drive.', color=ctx.author.color)
em.add_field(name='**Aliases**', value='`!checkstorage`, `!storage`, `!checkspace`, `!space`, `!checkdirectory`, `!dirspace`')
em.add_field(name='**Syntax**', value='!checkstorage <drivename>'
await ctx.channel.send(embed=em)
#login
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('-------------')
bot.run(TOKEN)