Discord Modal - trouble accessing attributes - python

I've been building a discord bot with pycord, and so far it's been a very well documented affair. However, when I got to the newer function of Modals, I ran into some trouble. Below is the code for my Modal and the slash command that calls it in the discord server. The modal sends fine and the callback runs fine.
When I try to get the user that filled in the modal, however, I run into trouble.
class MyModal(discord.ui.Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.add_item(discord.ui.InputText(label="Enter your prompt here and click submit.", style = discord.InputTextStyle.long))
async def callback(self, interaction: discord.Interaction): # DO NOT alter the callback name
embed = discord.Embed(title="Modal Results", color=5763719)
embed.add_field(name="Suggested prompt:", value = self.children[0].value)
print(discord.Interaction.user)
# returns "<member 'user' of 'Interaction' objects>" instead of the user attribute that I expected.
# embed.set_author(name = discord.Interaction.user.name) <- error: 'member_descriptor' object has no attribute 'name'
await interaction.response.send_message(embeds=[embed])
#bot.command(description = "Call forth a test Modal.")
async def trymodal(ctx):
modal = MyModal(title = "Modal via Slash Command")
await ctx.send_modal(modal)
I tried calling on discord.Interaction.user, which, according to the API doc should have the attribute 'name', among other things. When I debug and halt the code right after the print, I can indeed see a local variable 'user' in the object discord.interactions.Interaction.
If I try to address the attribute, I get the error 'member_descriptor' object has no attribute 'name'.
I haven't had formal training (yet), but if I had to guess I'd say it's seeing discord.Interaction.user as a member, and not as the class that it is supposed to be? I read this in the API documentation under the 'user' attribute of the class Interaction: https://docs.pycord.dev/en/stable/api/models.html#discord.Interaction.user
It says Type: Union[User, Member]. I have no clue what that means? I know User and Member are both classes with a lot of overlapping data in them. Does my problem have anything to do with this? Or am I missing a basic class thing?
Bottom line: how can I access the attributes that I know User has?

You're accessing the wrong thing.
discord.Interaction.user is getting the user property on the Interaction class. Your interaction variable is called interaction - you need to access the user attribute on that variable.
async def callback(self, interaction: discord.Interaction):
print(interaction.user)
discord.Interaction is the Object type and is a Class. You even use it as a type hint for the variable in the function definition. Just make sure to access the variable attributes.

Related

Discord button stop working if the bot is inactive

i have a discord bot which is working properly. But if i leave the bot alone for at least 5 min the button what was posted below a message, stop working even if the bot is online.
I run the script from my computer but i tried online services.
What could cause this error?
i tried online services, my computer.
but after 5 min the button stop working.
Heres my code: pastebin.com/zShRaYig
You need to increase the timeout when you create your View class. So assuming that you have your own view that's subclassed from discord.ui.View then:
class MyView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
# rest of your view class
Or, alternatively, you can override the on_timeout function and edit the message accordingly when it does timeout.
class MyView(discord.ui.View):
def __init__(self):
super().__init__()
async def on_timeout(self):
await self.message.edit(content="This has timed out. Please create another.", view=None)
# rest of your view class

Detect when an (IRC) thread ends which otherwise is silent

I'm attempting to use the python Twitch-Python library for a simple Twitch bot to connect to chat and respond to messages. The bot works as expected, but about 1/6 times the bot fails to connect and the program simply ends. As explained on this error page, it seems to be because the thread ends and doesn't return anything at all https://github.com/PetterKraabol/Twitch-Python/issues/45
I have moved the Chat class into my own code, and have the following:
def __init__(self, channel: str, nickname: str, oauth: str, helix: Optional['twitch.Helix'] = None):
super().__init__()
self.helix: Optional['twitch.Helix'] = helix
self.irc = tc.IRC(nickname, password=oauth)
self.irc.incoming.subscribe(self._message_handler)
self.irc.start()
self.channel = channel.lstrip('#')
self.joined: bool = False
print(self.irc.is_alive())
The class is called with two different functions elsewhere, like so:
def handle_message(self, message : twitch.chat.Message) -> None:
print(message.sender, message.text)
def main(self):
ircConn = TCH(channel='#CHANNEL NAME", nickname="BOT NAME", oauth="SAMPLEOAUTH")
ircConn.subscribe(self.handle_message)
The issue is that about 1 in 6 times, the subscribe() doesn't actually do anything and the program ends. I'd like to find a way to detect when it fails, so that I may attempt a retry, but nothing I've found works. The function/class doesn't return anything, adding an on_error or on_completed argument to the subscribe() doesn't call anything, and using sys.excepthook also doesn't catch it failing.
Additionally, the IRC thread always prints True for irc.is_alive(), even when it fails afterwards.
How can I catch the thread failing? Thank you.

PyCharm doesn't identify 'config', Pycharm 2020.3.3 Python 3.9

I found this help command from GitHub and put it in my code. I checked all errors and under those errors was the error that config was not defined. How do I fix it?
Link to GitHub:
https://gist.github.com/nonchris/1c7060a14a9d94e7929aa2ef14c41bc2
Code (it's a long code, I know):
import discord
from discord.ext import commands
from discord.errors import Forbidden
"""This custom help command is a perfect replacement for the default one on any Discord Bot written in Discord.py!
However, you must put "bot.remove_command('help')" in your bot, and the command must be in a cog for it to work.
Original concept by Jared Newsom (AKA Jared M.F.)
[Deleted] https://gist.github.com/StudioMFTechnologies/ad41bfd32b2379ccffe90b0e34128b8b
Rewritten and optimized by github.com/nonchris
https://gist.github.com/nonchris/1c7060a14a9d94e7929aa2ef14c41bc2
You need to set three variables to make that cog run.
Have a look at line 51 to 56
"""
async def send_embed(ctx, embed):
"""
Function that handles the sending of embeds
-> Takes context and embed to send
- tries to send embed in channel
- tries to send normal message when that fails
- tries to send embed private with information abot missing permissions
If this all fails: https://youtu.be/dQw4w9WgXcQ
"""
try:
await ctx.send(embed=embed)
except Forbidden:
try:
await ctx.send("Hey, seems like I can't send embeds. Please check my permissions :)")
except Forbidden:
await ctx.author.send(
f"Hey, seems like I can't send any message in {ctx.channel.name} on {ctx.guild.name}\n"
f"May you inform the server team about this issue? :slight_smile: ", embed=embed)
class Help(commands.Cog):
"""
Sends this help message
"""
def __init__(self, bot):
self.bot = bot
#commands.command()
# #commands.bot_has_permissions(add_reactions=True,embed_links=True)
async def help(self, ctx, *input):
"""Shows all modules of that bot"""
# !SET THOSE VARIABLES TO MAKE THE COG FUNCTIONAL!
prefix = # ENTER YOUR PREFIX - loaded from config, as string or how ever you want!
# setting owner name - if you don't wanna be mentioned remove line 49-60 and adjust help text (line 88)
owner = # ENTER YOU DISCORD-ID
owner_name = # ENTER YOUR USERNAME#1234
# checks if cog parameter was given
# if not: sending all modules and commands not associated with a cog
if not input:
# checks if owner is on this server - used to 'tag' owner
try:
owner = ctx.guild.get_member(config.OWNER).mention
except AttributeError as e:
owner = config.OWNER_NAME
# starting to build embed
emb = discord.Embed(title='Commands and modules', color=discord.Color.blue(),
description=f'Use `{prefix}help <module>` to gain more information about that module '
f':smiley:\n')
# iterating trough cogs, gathering descriptions
cogs_desc = ''
for cog in self.bot.cogs:
cogs_desc += f'`{cog}` {self.bot.cogs[cog].__doc__}\n'
# adding 'list' of cogs to embed
emb.add_field(name='Modules', value=cogs_desc, inline=False)
# integrating trough uncategorized commands
commands_desc = ''
for command in self.bot.walk_commands():
# if cog not in a cog
# listing command if cog name is None and command isn't hidden
if not command.cog_name and not command.hidden:
commands_desc += f'{command.name} - {command.help}\n'
# adding those commands to embed
if commands_desc:
emb.add_field(name='Not belonging to a module', value=commands_desc, inline=False)
# setting information about author
emb.add_field(name="About", value=f"The Bots is developed by Chriѕ#0001, based on discord.py.\n\
This version of it is maintained by {owner}\n\
Please visit https://github.com/nonchris/discord-fury to submit ideas or bugs.")
emb.set_footer(text=f"Bot is running {config.VERSION}")
# block called when one cog-name is given
# trying to find matching cog and it's commands
elif len(input) == 1:
# iterating trough cogs
for cog in self.bot.cogs:
# check if cog is the matching one
if cog.lower() == input[0].lower():
# making title - getting description from doc-string below class
emb = discord.Embed(title=f'{cog} - Commands', description=self.bot.cogs[cog].__doc__,
color=discord.Color.green())
# getting commands from cog
for command in self.bot.get_cog(cog).get_commands():
# if cog is not hidden
if not command.hidden:
emb.add_field(name=f"`{config.PREFIX}{command.name}`", value=command.help, inline=False)
# found cog - breaking loop
break
# if input not found
# yes, for-loops have an else statement, it's called when no 'break' was issued
else:
emb = discord.Embed(title="What's that?!",
description=f"I've never heard from a module called `{input[0]}` before :scream:",
color=discord.Color.orange())
# too many cogs requested - only one at a time allowed
elif len(input) > 1:
emb = discord.Embed(title="That's too much.",
description="Please request only one module at once :sweat_smile:",
color=discord.Color.orange())
else:
emb = discord.Embed(title="It's a magical place.",
description="I don't know how you got here. But I didn't see this coming at all.\n"
"Would you please be so kind to report that issue to me on github?\n"
"https://github.com/nonchris/discord-fury/issues\n"
"Thank you! ~Chris",
color=discord.Color.red())
# sending reply embed using our own function defined above
await send_embed(ctx, emb)
def setup(bot):
bot.add_cog(Help(bot))
A config.py file is a file you use to set configurations to your code easily. On the code you sent, it's used to identify a couple of key attributes.
If the original creator hasn't posted the config.py on the github, you can probably do it yourself. First, you will create a .py fle named config, and then you will write on there the attributes that this code calls using config. For example, in one part of the code, we have this:
except AttributeError as e:
owner = config.OWNER_NAME
That means the code is going after the config file to look for the attribute "OWNER_NAME", so, when you create your config.py file on your own folder, you will have to define this attribute, with something like:
OWNER_NAME = "your name or something"
The same goes for every other attribute called through the config.
After you finish defining all of this, make sure the config.py file goes inside the same folder that your code is located, and put on your code this simple line:
import config
And then your code should run.

How can i subscribe a consumer and notify him of any changes in django channels

I'm currently building an application that allows users to collaborate together and create things, as I require a sort of discord like group chatfeed. I need to be able to subscribe logged in users to a project for notifications.
I have a method open_project that retrieves details from a project that has been selected by the user, which I use to subscribe him to any updates for that project.
So I can think of 2 ways of doing this. I have created a instance variable in my connect function, like this:
def connect(self):
print("connected to projectconsumer...")
self.accept()
self.projectSessions = {}
And here is the open_project method:
def open_project(self, message):
p = Project.objects.values("projectname").get(id=message)
if len(self.projectSessions) == 0:
self.projectSessions[message] = []
pass
self.projectSessions[message] = self.projectSessions[message].append(self)
print(self.projectSessions[message])
self.userjoinedMessage(self.projectSessions[message])
message = {}
message["command"] = "STC-openproject"
message["message"] = p
self.send_message(json.dumps(message))
Then when the user opens a project, he is added to the projectSessions list, this however doesn't work (I think) whenever a new user connects to the websocket, he gets his own projectconsumer.
The second way I thought of doing this is to create a managing class that only has 1 instance and keeps track of all the users connected to a project. I have not tried this yet as I would like some feedback on if I'm even swinging in the right ball park. Any and all feedback is appreciated.
EDIT 1:
I forgot to add the userjoinedMessage method to the question, this method is simply there to mimic future mechanics and for feedback to see if my solution actually works, but here it is:
def userjoinedMessage(self, pointer):
message = {}
message["command"] = "STC-userjoinedtest"
message["message"] = ""
pointer.send_message(json.dumps(message))
note that i attempt to reference the instance of the consumer.
I will also attempt to implement a consumer manager that has the role of keeping track of what consumers are browsing what projects and sending updates to the relevant channels.
From the question, the issue is how to save projectSessions and have it accessible across multiple instances of the consumer. Instead of trying to save it in memory, you can save in a database. It is a dictionary with project as key. You can make it a table with ForeignKey to the Project model.
In that way, it is persisted and there would be no issue retrieving it even across multiple channels server instances if you ever decide to scale your channels across multiple servers.
Also, if you feel that a traditional database will slow down the retrieval of the sessions, then you can use faster storage systems like redis
Right, this is probably a horrible way of doing things and i should be taken out back and shot for doing it but i have a fix for my problem. I have made a ProjectManager class that handles subscriptions and updates to the users of a project:
import json
class ProjectManager():
def __init__(self):
if(hasattr(self, 'projectSessions')):
pass
else:
self.projectSessions = {}
def subscribe(self, projectid, consumer):
print(projectid not in self.projectSessions)
if(projectid not in self.projectSessions):
self.projectSessions[projectid] = []
self.projectSessions[projectid].append(consumer)
self.update(projectid)
def unsubscribe(self, projectid, consumer):
pass
def update(self, projectid):
if projectid in self.projectSessions:
print(self.projectSessions[projectid])
for consumer in self.projectSessions[projectid]:
message = {}
message["command"] = "STC-userjoinedtest"
message["message"] = ""
consumer.send_message(json.dumps(message))
pass
in my apps.py file i initialize the above ProjectManager class and assign it to a variable.
from django.apps import AppConfig
from .manager import ProjectManager
class ProjectConfig(AppConfig):
name = 'project'
manager = ProjectManager()
Which i then use in my consumers.py file. I import the manager from the projectconfig class and assign it to a instance variable inside the created consumer whenever its connected:
def connect(self):
print("connected to projectconsumer...")
self.accept()
self.manager = ProjectConfig.manager
and whenever i call open_project i subscribe to that project with the given project id recieved from the front-end:
def open_project(self, message):
p = Project.objects.values("projectname").get(id=message)
self.manager.subscribe(message, self)
message = {}
message["command"] = "STC-openproject"
message["message"] = p
self.send_message(json.dumps(message))
as i said i in no way claim that this is the correct way of doing it and i am also aware that channel_layers supposedly does this for you in a neat way. i however don't really have the time to get into channel_layers and will therefore be using this.
I am still open to suggestions ofcourse and am always happy to learn more.

Extending the bot in python's IRC library

I am attempting to extend the example bot that comes with this IRC library here. I've repasted the code of said bot here.
My problem is that I don't quite see what needs to be modified in order to enable the bot to respond to events, like getting a message - there's no event dispatcher that I can see.
What I can do is
bot = irc.bot.SingleServerIRCBot(server_list = [('irc.whatever.net.', 6667)],realname = 'irclibbot',nickname = 'irclibbot',)
bot.start()
and it runs fine - connects to the network and all that, but it doesn't do anything. Doesn't even respond to the basic CTCP events like VERSION and PING.
How does this work?
Check out this example of what you need to do.
class TestBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
self.do_command(e, e.arguments[0])
Define your own class that inherits from the actual irc.bot.SingleServerIRCBot class. Then, the events will automatically be bound to the methods named on_'event' like on_privmsg, on_part, etc.
Here you can find the reference of the supported events.

Categories