Перейти к содержимому

Как отправить сообщение discord py

  • автор:

Introduction¶

This is the documentation for discord.py, a library for Python to aid in creating applications that utilise the Discord API.

Prerequisites¶

discord.py works with Python 3.8 or higher. Support for earlier versions of Python is not provided. Python 2.7 or lower is not supported. Python 3.7 or lower is not supported.

Installing¶

You can get the library directly from PyPI:

python3 -m pip install -U discord.py 

If you are using Windows, then the following should be used instead:

py -3 -m pip install -U discord.py 

To get voice support, you should use discord.py[voice] instead of discord.py , e.g.

python3 -m pip install -U discord.py[voice] 

On Linux environments, installing voice requires getting the following dependencies:

For a Debian-based system, the following command will get these dependencies:

$ apt install libffi-dev libnacl-dev python3-dev

Remember to check your permissions!

Virtual Environments¶

Sometimes you want to keep libraries from polluting system installs or use a different version of libraries than the ones installed on the system. You might also not have permissions to install libraries system-wide. For this purpose, the standard library as of Python 3.3 comes with a concept called “Virtual Environment”s to help maintain these separate versions.

A more in-depth tutorial is found on Virtual Environments and Packages .

However, for the quick and dirty:

    Go to your project’s working directory:

$ cd your-bot-source $ python3 -m venv bot-env
$ source bot-env/bin/activate

On Windows you activate it with:

$ bot-env\Scripts\activate.bat
$ pip install -U discord.py

Congratulations. You now have a virtual environment all set up.

Scripts executed with py -3 will ignore any currently active virtual environment, as the -3 specifies a global scope.

Basic Concepts¶

discord.py revolves around the concept of events . An event is something you listen to and then respond to. For example, when a message happens, you will receive an event about it that you can respond to.

A quick example to showcase how events work:

# This example requires the 'message_content' intent. import discord class MyClient(discord.Client): async def on_ready(self): print(f'Logged on as self.user>!') async def on_message(self, message): print(f'Message from message.author>: message.content>') intents = discord.Intents.default() intents.message_content = True client = MyClient(intents=intents) client.run('my token goes here') 

Как отправить сообщение пользователю по ID?

Я слышал о discord.User, но как его правильно применять?

  • Вопрос задан более трёх лет назад
  • 4380 просмотров

Комментировать
Решения вопроса 1

Это такой распространённый вопрос, что на него есть ответ прямо в документации: https://discordpy.readthedocs.io/en/latest/faq.htm.

Ответ написан более трёх лет назад
Нравится 1 1 комментарий
Guerro69 @Guerro69 Автор вопроса
спасибо, я знал о get_channel но почему то не задумался про get_user )
Ответы на вопрос 0
Ваш ответ на вопрос

Войдите, чтобы написать ответ

discord-py

  • discord.py

Почему Не запускается dropdown на disnake, и как исправить?

  • 1 подписчик
  • 17 нояб.
  • 22 просмотра

Send python message from another function using discord.py

Hello. I wrote some code that constantly checks for streams from a youtube channel and prints all the info to the console. I want to write another program that’s basically the same, but it sends that info to discord instead. I was thinking I could replace all the print functions with one that sends the same thing to discord but it doesn’t look like it’s that simple. Here’s how the code roughly looks:

# code that check() uses def check(channel_url): # while True loop that’s constantly running intents = discord.Intents.all() bot = commands.Bot(command_prefix=’/’, intents= intents) @bot.tree.command(name=’check_handle’) @app_commands.describe(handle = ‘@’) async def check_handle(interaction: discord.Interaction, handle: str): channel_url = f’https://www.youtube.com/»>/streams’ if valid_url(channel_url): check(channel_url) else: await interaction.response.send_message(f’Invalid handle!!’) # two more functions that do the same thing as check_handle but take a channel id and url instead bot.run(os.environ.get(‘YT_NOTIFY’))

the check() code is a bit long, but basically I want to replace every print function with a send_message(status, message) one. I’m a total noob when it comes to async so I don’t know where to place the function definition and how to call it without half my code becoming unreachable. This is the function:

async def send_message(status, message): global notify_status channel = bot.get_channel(1141122899365855235) if notify_status == ‘scheduled’: await channel.send(message) elif notify_status == ‘live’ and status == ‘live’: await channel.send(message)

Thanks a lot for the help!

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *