-
I'm trying to lay the foundation of a music system inside my Discord bot that I will be developing on for quite some time in the future. I finished up most of the basic work, except that one error popped in: Command 'join' raised an exception: 'Interaction' object has no attribute 'voice_state' If you want more context, here's the code inside the class Music(commands.Cog):
def __init__(
self,
bot: core.IgKnite
) -> None:
self.bot = bot
self.voice_states = {}
def get_voice_state(
self,
inter: discord.Interaction
) -> VoiceState:
state = self.voice_states.get(inter.guild.id)
if not state or not state.exists:
state = VoiceState(self.bot, inter)
self.voice_states[inter.guild.id] = state
return state
def cog_unload(self) -> None:
for state in self.voice_states.values():
self.bot.loop.create_task(state.stop())
def ensure_voice_state(func):
@functools.wraps(func)
async def callback(
self,
inter: discord.Interaction,
*args,
**kwargs
) -> None:
inter.voice_state = self.get_voice_state(inter)
await func(self, inter, *args, **kwargs)
return callback
@app_commands.command(
name='join',
description='Joins the voice channel that you\'re in.',
)
@app_commands.guild_only()
@ensure_voice_state
async def _join(
self,
inter: discord.Interaction
) -> None:
destination = inter.user.voice.channel
if inter.voice_state.voice:
await inter.voice_state.voice.move_to(destination)
else:
inter.voice_state.voice = await destination.connect() If you have a look at the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can not add arbitrary attributes to most of the libraries' models, including Since this is a common request when it comes to interactions there is - inter.voice_state = self.get_voice_state(inter)
+ inter.extras['voice_state'] = self.get_voice_state(inter) |
Beta Was this translation helpful? Give feedback.
You can not add arbitrary attributes to most of the libraries' models, including
Interaction
.Since this is a common request when it comes to interactions there is
Interaction.extras
, a dictionary that you may store any of your own state in, meaning you can simply replace your assignment and uses: