2
votes

As the title asks, is there a way for the bot to not work in PM's and only work when in used in channels?

What I have done at the moment is have this statement in front of my @client.command

@client.command(pass_context=True)
async def profile(ctx):
    if ctx.message.server == None:
        pass
    else:
        # Code

Is there an easier way to go about doing this? I've read that I could use a global check for it, but I'm not sure how to implement that.

EDIT: I'm coding this using Commands Extension

3
That looks good to me; why do you think there would be a different way?Adam Barnes
@AdamBarnes Since I have to add that for every command I create, I feel that repeating that line each time will make the code unnecessarily long.haruyuki

3 Answers

4
votes

There is nothing in the documentation that I could see which said anything about globally disabling events for private messages.

The way you've done it is exactly what I'd do. You say you don't want to repeat the line every time, but you can only have one on_message(), so that line will only need to appear once; every command that needs to obey those rules will just be in that if block:

@client.event
async def on_message(message):
    if message.server is not None:
        if message.content.startswith('!dice'):
            ...

        if message.content.startswith('!roulette'):
            ...

EDIT: It appears you're using the commands extension, which is pretty much undocumented.

Reading the docstring for Command, though, I came across this section:

no_pm : bool
    If ``True``\, then the command is not allowed to be executed in
    private messages. Defaults to ``False``. Note that if it is executed
    in private messages, then :func:`on_command_error` and local error handlers
    are called with the :exc:`NoPrivateMessage` error.

So if you define your command as such:

@client.command(pass_context=True, no_pm=True)
async def profile(ctx):
    ...

It will be disabled in PMs.

4
votes

no_pm = true doesnt seem to work now. so add "@commands.guild_only()" below @client.command()

example:

@client.command()
@commands.guild_only()
async def commandname():
1
votes

If you want to code it with @bot.event you have to do it like so:

@bot.event
async def on_message(message):
    if message.content.startswith('!dm')
        await message.author.send('Hi. This message is send in DM!')