#๐Ÿ”’ Please Help

36 messages ยท Page 1 of 1 (latest)

cedar flint
#

I made a discord bot to setup servers but it spams channels

late wigeonBOT
#

@cedar flint

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mortal edge
#

Are you storing the discord channel ids in a database?

mortal edge
#

How else will the bot know the channels were already created?

cedar flint
#

ummmmmm

#

how do i store them?

mortal edge
#

you can use either sqlite3 or a json file

cedar flint
#

how would i use a json file?

mortal edge
#

with the json module.

#

!d json.load

late wigeonBOT
#

json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)```
Deserialize *fp* (a `.read()`\-supporting [text file](https://docs.python.org/3/glossary.html#term-text-file) or [binary file](https://docs.python.org/3/glossary.html#term-binary-file) containing a JSON document) to a Python object using this [conversion table](https://docs.python.org/3/library/json.html#json-to-py-table).

*object\_hook* is an optional function that will be called with the result of any object literal decoded (a [`dict`](https://docs.python.org/3/library/stdtypes.html#dict)). The return value of *object\_hook* will be used instead of the [`dict`](https://docs.python.org/3/library/stdtypes.html#dict). This feature can be used to implement custom decoders (e.g. [JSON\-RPC](https://www.jsonrpc.org) class hinting).

*object\_pairs\_hook* is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of *object\_pairs\_hook* will be used instead of the [`dict`](https://docs.python.org/3/library/stdtypes.html#dict). This feature can be used to implement custom decoders. If *object\_hook* is also defined, the *object\_pairs\_hook* takes priority.
mortal edge
#
with open("channels.json") as f:
  channels = json.load(f)
#

!d json.dump to save data

late wigeonBOT
#

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)```
Serialize *obj* as a JSON formatted stream to *fp* (a `.write()`\-supporting [file\-like object](https://docs.python.org/3/glossary.html#term-file-like-object)) using this [conversion table](https://docs.python.org/3/library/json.html#py-to-json-table).

If *skipkeys* is true (default: `False`), then dict keys that are not of a basic type ([`str`](https://docs.python.org/3/library/stdtypes.html#str), [`int`](https://docs.python.org/3/library/functions.html#int), [`float`](https://docs.python.org/3/library/functions.html#float), [`bool`](https://docs.python.org/3/library/functions.html#bool), `None`) will be skipped instead of raising a [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError).

The [`json`](https://docs.python.org/3/library/json.html#module-json) module always produces [`str`](https://docs.python.org/3/library/stdtypes.html#str) objects, not [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) objects. Therefore, `fp.write()` must support [`str`](https://docs.python.org/3/library/stdtypes.html#str) input.

If *ensure\_ascii* is true (the default), the output is guaranteed to have all incoming non\-ASCII characters escaped. If *ensure\_ascii* is false, these characters will be output as\-is.
cedar flint
#

ok

#

thx

mortal edge
#

If the file doesn't exist, open will raise an OSError

cedar flint
#

can u help me one more time

#

for a while now ive been using replit to run my python code

#

can u teach me how to run in the IDE

mortal edge
#

Open the file in vscode and press F5

#

(after activating your venv)

#

!venv

late wigeonBOT
#
Virtual environments

Virtual environments are isolated Python environments, which make it easier to keep your system clean and manage dependencies. By default, when activated, only libraries and scripts installed in the virtual environment are accessible, preventing cross-project dependency conflicts, and allowing easy isolation of requirements.

To create a new virtual environment, you can use the standard library venv module: python3 -m venv .venv (replace python3 with python or py on Windows)

Then, to activate the new virtual environment:

Windows (PowerShell): .venv\Scripts\Activate.ps1
or (Command Prompt): .venv\Scripts\activate.bat
MacOS / Linux (Bash): source .venv/bin/activate

Packages can then be installed to the virtual environment using pip, as normal.

For more information, take a read of the documentation. If you run code through your editor, check its documentation on how to make it use your virtual environment. For example, see the VSCode or PyCharm docs.

Tools such as poetry and pipenv can manage the creation of virtual environments as well as project dependencies, making packaging and installing your project easier.

Note: When using PowerShell in Windows, you may need to change the execution policy first. This is only required once per user:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
cedar flint
#

thank you so much ๐Ÿ™‚

cedar flint
#

the other two dont work

#

using python3

#

same thing happens for python

cedar flint
mortal edge
#

you need to activate the venv before running python

late wigeonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.