#importing text as code

1 messages · Page 1 of 1 (latest)

white shoal
#

Depends on how your code looks and how you want to "use it as a command"

raw prism
#

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

compact comet
#

ok

#

lemme test

raw prism
#

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.

compact comet
#

Would that work

raw prism
white shoal
#

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

raw prism
#

you need to load the string from the file btw

compact comet
#

Whats in the file is underneath #file

compact comet
white shoal
#

I don't see why you can't just use cogs for it though, seems like you're just overcomplicating stuff

compact comet
#

Because i dont like cogs

white shoal
#

Why not?

raw prism
#

You make it 1000 times harder for urself

compact comet
#

I like that

white shoal
#

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

raw prism
#

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