question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add storage to Bot, similar to Flask.config

See original GitHub issue

Summary

Current way of adding custom vars to Bot instances suck

What is the feature request for?

discord.ext.commands

The Problem

Currently the recommended way to add custom variables/instances/etc to bot is to attach them directly to Bot’s instance. E.g:

import discordpy
from discord.ext import commands

bot = commands.Bot(command_prefix="!")
bot.var1 = "amogus"
bot.var2 = "sus"

Which is kinda bad, coz there is a very high chance to accidentally pick some variable name internally used by Bot, overwrite it and then break things. Plus, even if someone will carefully browse tru docs to avoid it - its not really future-proof (coz var with conflicting name may be randomly introduced in new version of library)

The Ideal Solution

Simply attach one or two SimpleNamespace storages to Bot(). Like, dunno - Bot.config and Bot.data. Or just one empty dict to be used exclusively by whoever writes a bot. Then boom - everyone will be happy

The Current Solution

Currently you have to manually browse documentation for possible name collisions, manually attach vars to Bot’s instance, then pray for nothing to break on library’s update

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PythonCoderAScommented, Aug 16, 2021

Use name mangling. Make a subclass and call your variable __config, so that it will never conflict unless discord.py devs have a personal vendetta against you and find out your class name.

0reactions
Rapptzcommented, Aug 16, 2021

I have no plans on changing this. The current approach is pretty bog standard Python.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Your Flask App - Hackers and Slackers
Best practices on configuring Flask. Use different configs for different environments and keep sensitive values out of your source code.
Read more >
Preserving global state in a flask application - Stack Overflow
It's simple key->value storage that is easy to configure and is shared among all threads and processes. Sometimes however (like in my case)...
Read more >
Uploading Files — Flask Documentation (2.2.x)
The basic idea of file uploads is actually quite simple. It basically works like this: A <form> tag is marked with enctype=multipart/form-data ...
Read more >
Flask-Limiter {2.8.1}
The different configuration options for each storage can be found in the storage backend documentation for limits as that is delegated to the...
Read more >
Build a Photos Web Application with Python Flask and Azure ...
I decided to build my own Photos Web App using Python Flask and Azure Storage. In this video, you'll see how to configure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found