2
votes

I am trying to learn how to make a discord bot through discord.py and wanted to add a feature where a message would be sent from the bot whenever another user joined the voice channel that the bot is currently in. I do not know how to use the event handler itself and didn't understand their documentation enough to utilize it.

from discord.ext.commands import Bot 
client = Bot(command_prefix="!")

@client.event
async def on_voice_state_update(before, after):
    await client.say("Howdy")

From my limited understanding of the documentation, the event handler should be used whenever a user is muted, deafened, leaves, or joins a channel.

However, even when I tried to get it to recognize those actions it gave me an error message of:

Ignoring exception in on_voice_state_update
Traceback (most recent call last):
  File "C:\Users\Sam\PycharmProjects\FunBotProject\venv\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/Sam/PycharmProjects/FunBotProject/my_bot2.py", line 63, in on_voice_state_update
    await client.say("Xd ")
  File "C:\Users\Sam\PycharmProjects\FunBotProject\venv\lib\site-packages\discord\ext\commands\bot.py", line 309, in _augmented_msg
    msg = yield from coro
  File "C:\Users\Sam\PycharmProjects\FunBotProject\venv\lib\site-packages\discord\client.py", line 1145, in send_message
    channel_id, guild_id = yield from self._resolve_destination(destination)
  File "C:\Users\Sam\PycharmProjects\FunBotProject\venv\lib\site-packages\discord\client.py", line 289, in _resolve_destination
    raise InvalidArgument(fmt.format(destination))
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
2

2 Answers

2
votes

A version working in 2020. Please not that my code behaves different in terms of the sent message.

from discord.ext.commands import Bot 
bot = commands.Bot(command_prefix='!')

@bot.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None:
        if after.channel.id == [YOUR_CHANNEL_ID]:
            await member.guild.system_channel.send("Alarm!")
0
votes

client.say can only be used inside of commands, not events. See documentation here.

Can I use bot.say in other places aside from commands?

No. They only work inside commands due to the way the magic involved works.

The makes sense since a command will always be called from a text channel, meaning that the response from the bot can be sent to the same channel.

In your case, when a user joins a voice channel, the bot does not know to which text channel to send "Howdy".

To fix this, use client.send_message instead of client.say. In the example code below, "Howdy" will be sent to the "general" text channel every time the on_voice_state_update event is triggered.

from discord.ext.commands import Bot 
client = Bot(command_prefix="!")

@client.event
async def on_voice_state_update(before, after):
    if before.voice.voice_channel is None and after.voice.voice_channel is not None:
        for channel in before.server.channels:
            if channel.name == 'general':
                await client.send_message(channel, "Howdy")