Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This Discord bot was created by 'sinning' based off the following tutorial by Indently:
- # https://www.youtube.com/watch?v=UYJDKSah-Ww
- # The majority of code in main.py, and some part of responses.py, is directly copied from the tutorial.
- # The original author's GitHub repository can be found here:
- # https://github.com/indently/discord_tutorial_2024
- # Note that this bot will not work without all the steps included in the tutorial at all
- # And I cannot guarantee it will work even if those are followed perfectly
- # See also: https://pastebin.com/uSUFpdkf , for responses.py
- # And https://danbooru.donmai.us/forum_topics/28338 for related topic
- from typing import Final
- import os
- from dotenv import load_dotenv
- from discord import Intents, Client, Message
- from responses import get_response
- # STEP 0: LOAD OUR TOKEN FROM SOMEWHERE SAFE
- load_dotenv()
- TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
- # STEP 1: BOT SETUP
- intents: Intents = Intents.default()
- intents.message_content = True # NOQA
- client: Client = Client(intents=intents)
- # STEP 2: MESSAGE FUNCTIONALITY
- async def send_message(message: Message, user_message: str) -> None:
- if not user_message:
- print('(Message was empty because intents were not enabled probably)')
- return
- if is_private := user_message[0] == '?':
- user_message = user_message[1:]
- try:
- potential_embed = ''
- if len(message.embeds) > 0:
- print("The message has embeds")
- potential_embed=[]
- for _embed in message.embeds:
- print(f"_embed.thumbnail.url={_embed.thumbnail.url} _embed.image.url={_embed.image.url}")
- if isinstance(_embed.image.url, str):
- potential_embed.append(_embed.image.url)
- if isinstance(_embed.thumbnail.url, str):
- potential_embed.append(_embed.thumbnail.url)
- response: str = get_response(user_message, potential_embed)
- await message.author.send(response) if is_private else await message.channel.send(response)
- except Exception as e:
- print(e)
- # STEP 3: HANDLING THE STARTUP FOR OUR BOT
- @client.event
- async def on_ready() -> None:
- print(f'{client.user} is now running!')
- # STEP 4: HANDLING INCOMING MESSAGES
- @client.event
- async def on_message(message: Message) -> None:
- if message.author == client.user:
- return
- username: str = str(message.author)
- user_message: str = message.content
- channel: str = str(message.channel)
- print(f'[{channel}] {username}: "{user_message}"')
- await send_message(message, user_message)
- # STEP 5: MAIN ENTRY POINT
- def main() -> None:
- client.run(token=TOKEN)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment