You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.6 KiB

import os
import discord
import json
import logging
import sys
from discord.ext import commands, tasks
from discord.ext.commands import has_permissions, MissingPermissions
4 years ago
import aiohttp
import mimetypes
import requests
3 years ago
from decouple import config
bot = commands.Bot(command_prefix=".", intents=discord.Intents.default(),
case_insensitive=True)
"""
To add your token create a .env file in the top directory and add the following contents:
BOT_TOKEN={Paste your token here}
"""
3 years ago
token = config("BOT_TOKEN")
logging.basicConfig(filename='console.log',
level=logging.INFO,
format='[%(asctime)s %(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
@bot.event
async def on_ready():
# Marks bot as running
logging.info('I am running.')
@bot.event
async def on_message(message):
# Binflop
if len(message.attachments) > 0:
if message.author.bot:
return
perms = message.channel.permissions_for(message.guild.me)
if not perms.send_messages:
return
if not message.attachments[0].url.lower().endswith(('.html')):
file_type = mimetypes.guess_type(message.attachments[0].url)
if not file_type[0] == None:
try:
file_type = file_type[0].split('/')[0]
except:
logging.info(file_type + " failed while being parsed")
if message.attachments[0].url.lower().endswith(('.log', '.txt', '.json', '.yml', '.yaml', '.css', '.py', '.js', '.sh', '.config', '.conf')) or file_type == 'text':
text = await discord.Attachment.read(message.attachments[0], use_cached=False)
text = text.decode('Latin-1')
text = "\n".join(text.splitlines())
truncated = False
if len(text) > 100000:
text = text[:99999]
truncated = True
async with aiohttp.ClientSession() as session:
async with session.post('https://bin.birdflop.com/documents', data=text) as req:
key = json.loads(await req.read())['key']
response = ""
response = response + "https://bin.birdflop.com/" + key
response = response + "\nRequested by " + message.author.mention
if truncated:
response = response + "\n(file was truncated because it was too long.)"
embed_var = discord.Embed(title="Converted to an online paste file: ", color=0x1D83D4)
embed_var.description = response
try:
await message.channel.send(embed=embed_var)
except:
print("Permission error")
logging.info(f'File uploaded by {message.author} ({message.author.id}): https://bin.birdflop.com/{key}')
# Pastebin is blocked in some countries
words = message.content.replace("\n", " ").split(" ")
for word in words:
if word.startswith("https://pastebin.com/") and len(word) == 29:
print("hello3")
pastebinkey = word[len(word) - 8:]
r = requests.get(f'https://pastebin.com/raw/{pastebinkey}')
text = r.text
truncated = False
if len(text) > 100000:
text = text[:99999]
truncated = True
async with aiohttp.ClientSession() as session:
async with session.post('https://bin.birdflop.com/documents', data=text) as req:
key = json.loads(await req.read())['key']
response = ""
response = response + "https://bin.birdflop.com/" + key
response = response + "\nUploaded by " + message.author.mention
if truncated:
response = response + "\n(file was truncated because it was too long.)"
embed_var = discord.Embed(title="Pastebin is blocked in some countries", color=0x1D83D4)
embed_var.description = response
try:
await message.channel.send(embed=embed_var)
except:
print("Permission error")
timings = bot.get_cog('Timings')
await timings.analyze_timings(message)
await bot.process_commands(message)
@bot.command()
async def ping(ctx):
3 years ago
await ctx.send(f'Bot ping is {round(bot.latency * 1000)}ms')
for file_name in os.listdir('./cogs'):
if file_name.endswith('.py'):
bot.load_extension(f'cogs.{file_name[:-3]}')
4 years ago
bot.run(token)