Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.7k stars 3.74k forks source link

Getting server's name from message #1358

Closed burgerhex closed 6 years ago

burgerhex commented 6 years ago

I'm currently writing a Discord bot in Python, but I am having some trouble getting the server's name from the user's sent message. This is the code I have written:

if message.content.startswith("!whereami"):
    await message.channel.send(f"You are in {message.server.name} " + \
                               f"in the {message.channel.mention} channel!")

Python throws this error back at me: AttributeError: 'Message' object has no attribute 'server'

I tried reading the documentation for discord.Message, but it says that the message.server attribute is "optional." What does that mean and/or how can I go around that?

Thanks in advance.

Harmon758 commented 6 years ago

It seems like you're using the Rewrite branch, where servers are now referred to as guilds. See https://discordpy.readthedocs.io/en/rewrite/api.html#discord.Message.guild As for the optional part, if a message is from a DM, message.guild will return None.

For future questions like this, you should join either the official discord.py server or the Discord API server for help, as the README recommends.

Gmanicus commented 4 years ago

This page no longer exists. Also, docs that I find say to use "guilds", yet the version I have works with "servers". I installed discord.py via pip, so I'm confused as to why it appears I do not have the rewrite version.

Harmon758 commented 4 years ago

Please don't necrobump issues.

The page no longer exists because the rewrite branch was released as v1 and has since been deleted. The latest version of that documentation would be at https://discordpy.readthedocs.io/en/latest/api.html#discord.Message.guild. For help migrating to v1, see https://discordpy.readthedocs.io/en/latest/migrating.html.

For instructions for installing the library, see https://github.com/Rapptz/discord.py#installing. Make sure you're installing discord.py and not a mirror package on PyPI like discord. Also make sure you're installing it for the Python version that you're using.

For further help, you should join either the official discord.py server or the Discord API server, as the README also links.