Getting python file outside of folder - python-3.x

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

Related

How to import functions from different file and use in discord.py

A straight Forward question - How to import a function from a different file and use that in discord.py cuz i tried that and failed for example there is file a and b which looks like
a.py:
async def joke(ctx):
joke = 'Your mama so fat boy'
await ctx.send(joke)
and i want to use the joke function from file a to file b and the code i write was:
from a import joke
from discord.ext import commands
#some discord code
TOKEN = 'My_Secret_Token'
GUILD = 'My_Guild'
client = commands.Bot(command_prefix='!')
#client.command(name='joke', help='This will return a joke')
joke()
client.run(TOKEN)
And the line joke() is returning me error
File "main.py", line 31
joke()
^
SyntaxError: invalid syntax
And i am here confused that why it is returning me error and how can i pass the argument ctx. So please come up with a solution for me.
Edit: After debugging and scratching my head for hours i came up with a solution which is also not working that i modified my b.py a little bit :
from a import joke
from discord.ext import commands
#some discord code
TOKEN = 'My_Secret_Token'
GUILD = 'My_Guild'
client = commands.Bot(command_prefix='!')
#client.command(name='joke', help='This will return a joke')
async def my_function(ctx):
joke(ctx)
client.run(TOKEN)
You can use cogs for this. Docs: https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
Here is an Example:
Your main.py
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix='!')
for file in os.listdir("cogs"):
if file.endswith(".py"):
name = file[:-3]
client.load_extension(f"cogs.{name}")
client.run(TOKEN)
Make a secound /cogs/joke.py
import discord
from discord.ext import commands
import random
class joke(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def joke(self, ctx):
jokes = [
'Your mama so fat boy',
'joke2',
'joke3',
'joke4',
'joke5'
]
answer = random.choice(jokes)
await ctx.send(answer)
def setup(bot):
bot.add_cog(joke(bot))

AttributeError: 'Moderation' object has no attribute 'channel'

i tried to make cog for organize my bot but i have an error that i dont find how to fix. The bot successful find command in cog but when i write the command i have this error:
Could you help me pls ? Here is my code:
import discord
import asyncio
import re
import os
import random
from discord.ext import commands
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
#Purge
#commands.command()
async def purge(ctx, amount=10):
await ctx.channel.purge(limit=amount)
Your first parameter must be self.
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
#Purge
#commands.command()
async def purge(self, ctx, amount=10):
await ctx.channel.purge(limit=amount)
Add self as parameter in purge function.
async def purge(self,ctx,amount=10):
await ctx.channel.purge(limit=amount)

Custom Search Command Discord.py

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)

discord.py - Sending virtual PDF file

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")

Failed to import quarry.auth

I am trying to create a bot for Minecraft in python to integrate with Discord. I have this code from the documentation
import discord
from twisted.internet import defer, reactor
from quarry.net.client import ClientFactory, ClientProtocol
from quarry.auth import Profile
class kek:
def __init__(self, client):
self.client = client
class ExampleClientProtocol(ClientProtocol):
pass
class ExampleClientFactory(ClientFactory):
protocol = ExampleClientProtocol
#defer.inlineCallbacks
def main():
print("logging in...")
profile = yield Profile.from_credentials(
"MOJANG EMAIL", "MOJANG PASSWORD")
factory = ExampleClientFactory(profile)
print("connecting...")
factory = yield factory.connect("play.minevibe.net", 25565)
print("connected!")
if __name__ == "__main__":
main()
reactor.run()
def setup(client):
client.add_cog(kek(client))
However, I get the error "MCBot.kek was not loaded. [No module named 'quarry.auth']" when I run it. The rest of the bot runs fine however it does not login to the server.
According to the github repo for quarry auth is inside the net directory so try replacing
from quarry.auth import Profile
with
from quarry.net.auth import Profile

Resources