📅  最后修改于: 2023-12-03 15:00:25.318000             🧑  作者: Mango
Discord Py is a Python library that provides an easy way to interact with the Discord API. With this library, you can develop bots that can perform a wide range of functions, from moderating chats to playing games with your friends.
This guide will show you how to create a Discord Py bot that will display its status on your server. The bot will provide useful information such as the server name, the number of connected users, and more.
Before getting started, you will need:
mkdir discord-bot
cd discord-bot
python -m venv bot-env
source bot-env/bin/activate
pip install discord.py
Create a new Python file in your project folder called 'bot.py'
Open the file in a text editor and add the following code:
import discord
import os
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
if message.content.startswith('!status'):
guild = client.guilds[0]
await message.channel.send(f'Server Name: {guild.name}\nConnected Users: {guild.member_count}')
client.run(os.environ['DISCORD_TOKEN'])
Replace 'DISCORD_TOKEN' with your own Discord API key
Save the file and exit the text editor
Run the bot using the following command:
python bot.py
The bot is now up and running, and will respond to any text messages sent to it with the '!status' command.
In this guide, you learned how to create a Discord Py bot that displays its status on your server. You also learned how to install Discord Py, set up a virtual environment, and run the bot using your own Discord API key.
With this knowledge, you can now start exploring the endless possibilities of creating your own customized bots for your Discord server. Good luck and happy coding!