We use asynchronous functions here by using the async def syntax Bot React to Commands.We use a decorator before the functions.the channel id that the message was sent to def on_ready():.the user (author)’s name who sent the message. The second function on_message() will print out the following three pieces of information: The first function on_ready() will print ‘bot ready’ in console when we start the bot. Now we have a basic bot, let’s add two functions to it. In the Bot constructor (see the last line of code), by setting command_prefix = ‘.’, we are telling the bot that whenever a user’s message starts with “.” (dot), treat that as a command, we’ll talk about commands in the next section. The bot will only work if we (admin) give it certain permissions. You can think of intents similar to permissions. After 2022, your bot must have an intent in order to work with Discord. This is in compliance with the new Discord API. The below code creates a bare-bone Discord bot: import discordĬlient = commands.Bot(command_prefix='.', intents=intents) We’ll start off by building a very simple bot that will print out (in terminal/console) messages whenever a user sends a chat in a discord channel. We can install the library using pip: pip install discord.py Code Our First Python Discord Bot We’ll build the discord bot using the excellent discord.py library. Indeed, the bot’s engine will be the Python code! Install Discord.py Library We notice that the bot’s status is showing as offline. Give the bot access, then your bot will be added to your discord server! Once you have set up the bot successfully, copy the generated URL and paste it into a web-browser, then hit enter. See the video walkthrough for the details about registering your bot.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |