I get this error when i try to send a txt file to a discord webhook. (python3)
import os
import subprocess
import requests
import discord
import dhooks
from dhooks import Webhook, Embed
hook = Webhook("hook")
Discord_txt = open("data.txt", "r+")
hook.send(file=Discord_txt)
According to the docs, you should be using the File class from dhooks:
from dhooks import Webhook, File
hook = Webhook("https://discord.com/api/webhooks/...")
Discord_txt = File("data.txt")
hook.send(file=Discord_txt)
Related
I'm try to import my main python file but it's not working.
import discord
from discord.ext import commands
from ..bot import bot
class help(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command(name="help", aliases=["Help"])
async def help(self,context, message = None):
embed = discord.Embed(title="Help command")
cmds = ""
if message == None:
bot.listcom(cmds)
embed.add_field(name="Commands", value=cmds)
if not message == None:
pass
await context.send(embed=embed)
def setup(client):
client.add_cog(help(client))
Terminal Output:
ImportError: attempted relative import with no known parent package
I tried using import ..bot but it gave a syntax error and when I tried using just import bot it said it didn't exist. Can somebody help me?
https://stackoverflow.com/a/24868877/16237426
found that..
in your case that should work:
import sys, os
sys.path.append(os.path.abspath(os.path.join('..')))
from bot import bot
I'm attempting to make a Custom Search command using the Custom Search API.
Naturally I have no idea how to implement it. I'm hoping someone can take a look at what I have and help me figure out how to make it work in this style?
import discord
from discord.ext import commands
import discord.utils
import time
import os
import dotenv
from dotenv import load_dotenv
load_dotenv()
SEARCH = os.getenv("CUSTOM_SEARCH_API_KEY")
class Util(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command(name="Search", aliases=["search"])
#commands.has_permissions(embed_links=True, add_reactions=True)
async def _search(self, ctx, *, message):
if not ctx.author.Bot:
guild = ctx.guild
msg = ctx.message
cli = self.client.user
gold = discord.Color.dark_gold()
embed = discord.Embed(color=gold, name=f"{image.name}", description=f"{image.desc}", timestamp=msg.created_at)
embed.set_image(url=f"{image.url}")
embed.set_thumbnail(url=cli.avatar_url)
embed.set_footer(text=guild.name, icon_url=guild.icon_url)
await ctx.send(embed=embed)
return
def setup(client):
client.add_cog(Util(client))
I'm using dotenv to store my API key
Basically, I'm wanting it to be a somewhat detailed command.
If searching for posts it will find the exact post. and if searching for images it will go by tag and find a random image matching the selected tag(s).
I would like for it to respond via rich embed if possible.
I'm fully aware my above code won't work at all. I simply put it there as a base for part of the embed that I want to try to implement.
Some help would be much appreciated.
I'm using discord.py rewrite and I have my commands set up through cogs.
This is my bot.py file that contains the handler for my cogs.
import discord
from discord.ext import commands
import os
import json
import dotenv
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
PREFIX = os.getenv("COMMAND_PREFIX")
client = commands.Bot(command_prefix=PREFIX)
client.remove_command('help')
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
client.load_extension(f"cogs.{filename[:-3]}")
client.run(TOKEN)
Is there any way to upload a virtual PDF file onto Discord (I do not want files to be created on my computer). I already know I can send virtual txt files with io.StringIO like this:
import discord, asyncio
from discord.ext import commands
from io import StringIO
bot = commands.Bot()
#bot.command()
async def send(ctx, *, string):
await ctx.send(file=discord.File(
fp=StringIO("This is a test"),
filename="Test.txt"
)
But this doesn't work with PDF files. I tried using io.BytesIO instead but I got no result. Do anyone please know how to solve this problem?
Here's an example using FPDF:
import discord
from discord.ext import commands
from io import BytesIO
from fpdf import FPDF
bot = commands.Bot("!")
#bot.command()
async def pdf(ctx, *, text):
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, text)
bstring = pdf.output(dest='S').encode('latin-1')
await ctx.send(file=discord.File(BytesIO(bstring), filename='pdf.pdf'))
bot.run("token")
I am having a server.py file which is written in Falcon. Which looks like this.
try:
import falcon, logging
from os.path import dirname, realpath, join
from wsgiref import simple_server
from .config.config import server_config
from .middlewares.SQLAlchemySessionManager import SQLAlchemySessionManager
from .middlewares.GlobalInternalServerErrorManager import InternalServerErrorManager
from .lib.dbConnector import Session
from .routes import router
except ImportError as err:
falcon = None
raise err
serv_conf = server_config()
salescoachbot = falcon.API(middleware= [
SQLAlchemySessionManager(Session),
InternalServerErrorManager()
])
But when I am trying to import "salescoachbot" to other folder and files
like:
from ..server import salescoachbot
This gives me an error saying that the
from ..server import salescoachbot
ImportError: cannot import name 'salescoachbot'
The server.py is in the root of the project and has an init.py as well as the file which is trying to import the name.
What am I doing wrong here?
I get an Import error while importing the thermos.py module in my models.py module.
C:\Users\sys\Thermos\thermos\thermos.py
C:\Users\sys\Thermos\thermos\models.py
Here is the relevant part of the thermos.py module.
import os
from datetime import datetime
from flask import Flask, render_template, url_for, request, redirect, flash
from flask_sqlalchemy import SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'thermos.db')
db = SQLAlchemy(app)
And, here is the relevant part of the models.py module.
from datetime import datetime
from thermos import db
Here is the image of the error I receive in CMD:
Kindly let me know what needs to be done to fix this issue.
Python can't decide if you're trying to import from the folder, or the file named thermos
You can rename
C:\Users\sys\Thermos\thermos\thermos.py
To
C:\Users\sys\Thermos\thermos\__init__.py
This makes the thermos directory a package with the db variable that can be used with an import
from . import db