Discord is a social media platform where large numbers of people can interact in the form of a community. Currently, it’s the most harmonious platform for NFT projects as they’re community-oriented. Furthermore, the platforms facilitate NFTs trading or brand NFT marketing through their freedom communication features and integration space with Crypto wallets, such as Collabland, Metamask. Last but not least, it also facilitates NFT gating in private communities.
In this article, I would walk through how to build a discord bot to further increase communication and navigation efficiency using Python, MongoDB, Hikari, and Lightbulb.
Table of Contents: Build a Discord Bot Using Python, Hikari, Lightbulb, MongoDB
- Discord Bot Account: Discord Application, Token, Permission Options, Authentication Link
- User Message Receive and Response using Hikari, MongoDB
- Create a list of Slash Keywords & Navigators
- Full Python Script of Discord Bot using Hikari, Lightbulb, MongoDB, and Python
- FAQ
Discord Bot Account: Discord Application, Token, Permission Options, Authentication Link
First thing first, we need to create a discord bot account to get the access token and set up some permission in the discord communication.
Step 1: Go to the discord developer portal and log in using your discord account
Here is to create a new application
Step 2: Add a new bot, Select Permission Options and Authorise the. Integration with your Discord Account using the generated URL after having confirmed the permission options
Options for reference:
Scopes: Bot, applications.commands
Permission: Administration
User Message Receive and Response using Hikari
Secondly, it’s to create an auto-reply based on the messages from the users. Auto-reply 1to1 message strategy also matters, so I will further share in another article. In this piece, I would share the core framework of building this automatic conversation.
Step 1: Install and import Hikari, Lightbulb Module (pip3 install Hikari)
import hikari\nimport lightbulb
Step 2: Connect with Discord Bot Using LightBulb
In the integration, the bot token is the unique key to identify which bot you are using in the discord account. Additionally, please be sure to set up the intents which tell the system all messages can be handled by the bot
bot = lightbulb.BotApp(token="your bot token generated in the bot account", intents=hikari.Intents.ALL_MESSAGES, prefix="!")
If it’s connected, the live bot account would show up on the right side as well with other users and yourself.
Step 3: Set up the auto-reply by events
@bot.listen()
is the method to capture messages from users. Basically, there are two places . to capture messages. One is the public conversion guild, such as messages from channels, and the community and the other is the private message, which is the DM.
Here are the code samples for building the auto-reply in the DM with MongoDB and Hikari.
@bot.listen(hikari.DMMessageCreateEvent)\nasync def nftMessage(event):\nprint(event)\nif event.is_human:\nif myCollection.find_one({'messageA': 'id123'})['greetingMessage'] in event.content.strip().lower():\nreturn await event.message.respond(f'Hello buddy! please tell me more about your question?')\nelif myCollection.find_one({'messageA': 'id1234'})['greetingMessage'] in event.content.strip().lower():\nreturn await event.message.respond(f'Hello buddy! Whats up?')\nelse:\nreturn await event.message.respond(f'Tied up with something at the moment, I would come back to you asap')
Create a list of Slash Keywords & Navigators
Slash keywords basically are those specific words for users to get specific information without asking questions. It looks like the navigators. For example, if you are running an NFTs trade community, you can list a series of NFT name keywords, and users can get all information regarding these NFTs automatically in the conversion box.
In this section, we need to use lightbulb commands. Here are the code samples for creating one slash keyword FYI
@bot.command\n@lightbulb.command("nft", "Return the latest NFTs Collection")\n@lightbulb.implements(lightbulb.PrefixCommand, lightbulb.SlashCommand)\nasync def nftS(ctx: lightbulb.Context):\nreturn await ctx.respond(f'This is our latest NFT Collection, please check it out: https://testnets.opensea.io/assets/goerli/0x8eeb73f75e6d80a6a21302d1ec36d0ead0ceb295/0')\nbot.run()
In each slash command, it can include the name and description. Also, you can customiz. e the responding message as well. Here is a sample of Easy2Digital NFTs.
Full Python Script of Discord Bot using Hikari, Lightbulb, MongoDB, and Python
If you are interested in Python Tutorial 70 – Build a Discord Bot Using Python, Hikari, Lightbulb, MongoDB, please subscribe to our newsletter by adding the message “Chapter 70”. We would send you the script immediately to your mailbox.
I hope you enjoy reading Python Tutorial 70 – Build a Discord Bot Using Python, Hikari, Lightbulb, MongoDB. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support and Donate to our channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article on your social network with the hashtag #easy2digital
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount codes
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)