#importing text as code
1 messages · Page 1 of 1 (latest)
In pythen there is a build in fuction called exec
Which takes a string or a bytecode object and executes the code based on globals and locales
If the code is a valid module import it with importlib.import_module
However if the code is not from a trusted source dont do that.
Only use eval, exec or other stuff when you 10000000% sure that the code is save.
import py-cord
import os
all the needed things here
file = load.fileorsmth
#file
@client.command()
async def test(ctx):
await ctx.send("B")
#end of file
exec(file)#?
bot.run("")
Would that work
depends on what is in the file and you want to achieve
First of all it's not import py-cord, it's import discord
And again, it depends on the code in the file and what it's supposed to do
you need to load the string from the file btw
Whats in the file is underneath #file
you can see it in the code
i know
I don't see why you can't just use cogs for it though, seems like you're just overcomplicating stuff
ye
Because i dont like cogs
Why not?
You make it 1000 times harder for urself
I like that
Well go ahead, but you won't get much help here
it's not the way the library is intended to be used, so expect to run into unsolvable issues
This should work.
But im not 100 sure
A better way would be use cogs.
But if you dont want that user import lib to import the file
und dir() to get all methods and do client.add_command manualy
Which is what cogs also do in the end but a little fancier