Rapptz / discord.py

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

Problem with discord.AppInfo.owner #1018

Closed ghost closed 6 years ago

ghost commented 6 years ago

Code:

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    if message.channel.is_private:
        embed = discord.Embed(color=0x7daeff)
        embed.add_field(name='By', value=str(message.author), inline=True)
        embed.add_field(name='Time', value=str(datetime.now().strftime('%Y.%m.%d, %H:%M:%S')), inline=True)
        embed.add_field(name='Message content', value=message.content, inline=False)
        await bot.send_message(discord.AppInfo.owner, embed=embed)
    await bot.process_commands(message)

Console error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "D:\Program Files\Python 3.6.2\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "D:\Coding\Python\UniBot\main.py", line 27, in on_message
    await bot.send_message(discord.AppInfo.owner, embed=embed)
  File "D:\Program Files\Python 3.6.2\lib\site-packages\discord\client.py", line 1145, in send_message
    channel_id, guild_id = yield from self._resolve_destination(destination)
  File "D:\Program Files\Python 3.6.2\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 property

I do not know what I'm doing wrong. Can any one help me?

EvieePy commented 6 years ago

You do not call AppInfo directly.

You will need to retrieve and build AppInfo with application_info(): http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.application_info

For a quick example, you could do this in on_ready() and assign it to your bot:

@bot.event
async def on_ready():
    if not hasattr(bot, 'appinfo'):
        bot.appinfo = await bot.application_info()

In your code, you could then do:

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    if message.channel.is_private:
        embed = discord.Embed(color=0x7daeff)
        embed.add_field(name='By', value=str(message.author), inline=True)
        embed.add_field(name='Time', value=str(datetime.now().strftime('%Y.%m.%d, %H:%M:%S')), inline=True)
        embed.add_field(name='Message content', value=message.content, inline=False)
        await bot.send_message(bot.appinfo.owner, embed=embed)
    await bot.process_commands(message)
ghost commented 6 years ago

Thank you so much. I'll try to be more attentive next time.

scragly commented 6 years ago

Don't forget to close the Issue.