searchbot.py
· 3.3 KiB · Python
Raw
#!/usr/bin/env python3
import discord
import asyncio
import aiohttp
from urllib.parse import quote_plus, urlencode
import os.path
from os import getcwd
from urllib.parse import unquote
from mutagen import File
client = discord.Client()
if not discord.opus.is_loaded():
discord.opus.load_opus("libopus.so.0")
def is_safe_path(basedir, path, follow_symlinks=True):
# resolves symbolic links
if follow_symlinks:
return os.path.realpath(path).startswith(basedir)
return os.path.abspath(path).startswith(basedir)
lastresults = []
player = None
@client.event
async def on_message(message):
def after():
coro = client.voice_client_in(message.server).disconnect()
fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
try:
fut.result()
except:
pass
global lastresults
global player
if message.content.startswith("!search "):
with aiohttp.ClientSession() as session:
async with session.get("http://tallyall.club/search.php", params={"q": message.content[8:], "json": ""}) as resp:
data = await resp.json()
if len(data["results"]) < 1:
await client.send_message(message.channel, ":sos: No results")
return
out = ["Search results:"]
lastresults = data["results"]
numbers = [":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:"]
for n,r in enumerate(data["results"][:8]):
out.append("{} {}".format(numbers[n], os.path.basename(r)))
out.append(":signal_strength: !play <number>")
if len(data["results"]) > 8:
out.append(":arrow_right: Full results at http://tallyall.club/search.php?" + urlencode({"q": message.content[8:]}))
await client.send_message(message.channel, "\n".join(out))
if message.content.startswith("!play "):
if player:
player.stop()
msg = message.content[6:]
if msg.isdigit():
if int(msg) <= len(lastresults) and int(msg) > 0:
msg = lastresults[int(msg)-1]
else:
await client.send_message(message.channel, "Invalid result number")
return
path = "/var/www/html/" + unquote(msg.strip().replace("http://tallyall.club/", ""))
if is_safe_path("/var/www/html", path) and os.path.isfile(path) and File(path):
voice = client.voice_client_in(message.server)
if voice:
await voice.disconnect()
voice = await client.join_voice_channel(client.get_channel("386339345914920980"))
player = voice.create_ffmpeg_player(path, after=after)
player.start()
else:
await client.send_message(message.channel, "Invalid path")
if message.content.startswith("!stop "):
if player:
player.stop()
voice = client.voice_client_in(message.server)
await voice.disconnect()
@client.event
async def on_ready():
print("Logged in as {} ({})".format(client.user.name, client.user.id))
print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(3072)))
with open(".bottoken") as f:
creds = f.read()
if " " in creds:
client.run(*creds.split(None, 1))
else:
client.run(creds.strip())
| 1 | #!/usr/bin/env python3 |
| 2 | import discord |
| 3 | import asyncio |
| 4 | import aiohttp |
| 5 | from urllib.parse import quote_plus, urlencode |
| 6 | import os.path |
| 7 | from os import getcwd |
| 8 | from urllib.parse import unquote |
| 9 | from mutagen import File |
| 10 | client = discord.Client() |
| 11 | |
| 12 | if not discord.opus.is_loaded(): |
| 13 | discord.opus.load_opus("libopus.so.0") |
| 14 | |
| 15 | def is_safe_path(basedir, path, follow_symlinks=True): |
| 16 | # resolves symbolic links |
| 17 | if follow_symlinks: |
| 18 | return os.path.realpath(path).startswith(basedir) |
| 19 | return os.path.abspath(path).startswith(basedir) |
| 20 | |
| 21 | lastresults = [] |
| 22 | player = None |
| 23 | |
| 24 | @client.event |
| 25 | async def on_message(message): |
| 26 | def after(): |
| 27 | coro = client.voice_client_in(message.server).disconnect() |
| 28 | fut = asyncio.run_coroutine_threadsafe(coro, client.loop) |
| 29 | try: |
| 30 | fut.result() |
| 31 | except: |
| 32 | pass |
| 33 | global lastresults |
| 34 | global player |
| 35 | if message.content.startswith("!search "): |
| 36 | with aiohttp.ClientSession() as session: |
| 37 | async with session.get("http://tallyall.club/search.php", params={"q": message.content[8:], "json": ""}) as resp: |
| 38 | data = await resp.json() |
| 39 | if len(data["results"]) < 1: |
| 40 | await client.send_message(message.channel, ":sos: No results") |
| 41 | return |
| 42 | out = ["Search results:"] |
| 43 | lastresults = data["results"] |
| 44 | numbers = [":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:"] |
| 45 | for n,r in enumerate(data["results"][:8]): |
| 46 | out.append("{} {}".format(numbers[n], os.path.basename(r))) |
| 47 | out.append(":signal_strength: !play <number>") |
| 48 | if len(data["results"]) > 8: |
| 49 | out.append(":arrow_right: Full results at http://tallyall.club/search.php?" + urlencode({"q": message.content[8:]})) |
| 50 | await client.send_message(message.channel, "\n".join(out)) |
| 51 | if message.content.startswith("!play "): |
| 52 | if player: |
| 53 | player.stop() |
| 54 | msg = message.content[6:] |
| 55 | if msg.isdigit(): |
| 56 | if int(msg) <= len(lastresults) and int(msg) > 0: |
| 57 | msg = lastresults[int(msg)-1] |
| 58 | else: |
| 59 | await client.send_message(message.channel, "Invalid result number") |
| 60 | return |
| 61 | path = "/var/www/html/" + unquote(msg.strip().replace("http://tallyall.club/", "")) |
| 62 | if is_safe_path("/var/www/html", path) and os.path.isfile(path) and File(path): |
| 63 | voice = client.voice_client_in(message.server) |
| 64 | if voice: |
| 65 | await voice.disconnect() |
| 66 | voice = await client.join_voice_channel(client.get_channel("386339345914920980")) |
| 67 | player = voice.create_ffmpeg_player(path, after=after) |
| 68 | player.start() |
| 69 | else: |
| 70 | await client.send_message(message.channel, "Invalid path") |
| 71 | if message.content.startswith("!stop "): |
| 72 | if player: |
| 73 | player.stop() |
| 74 | voice = client.voice_client_in(message.server) |
| 75 | await voice.disconnect() |
| 76 | |
| 77 | @client.event |
| 78 | async def on_ready(): |
| 79 | print("Logged in as {} ({})".format(client.user.name, client.user.id)) |
| 80 | print(discord.utils.oauth_url(client.user.id, permissions=discord.Permissions(3072))) |
| 81 | |
| 82 | with open(".bottoken") as f: |
| 83 | creds = f.read() |
| 84 | if " " in creds: |
| 85 | client.run(*creds.split(None, 1)) |
| 86 | else: |
| 87 | client.run(creds.strip()) |