Discord¶
Integrate your code with Discord to send messages, files and more.
Installation¶
Setting up the WebHook¶
To use this plugin you need a Discord WebHook.
Follow the instructions at this article to learn how to obtain a WebHook.
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
Sending a Simple Message¶
Here is the expected output:
Sending a Complex Message¶
You can compose and send complex messages with the EmbeddedMessage
class.
from botcity.plugins.discord import BotDiscordPlugin, EmbeddedMessage, Author, Footer, Field, Color
# Instantiating the Embedded Message
message = EmbeddedMessage(
title = 'Title Embedded',
description = 'Long Description.',
color = Color.ORANGE
)
# Set the author
message.author = Author(
name = 'Botcity',
url = 'https://github.com/botcity-dev',
icon_url = 'https://avatars.githubusercontent.com/u/72993825?s=200&v=4'
)
# Set the footer
message.footer = Footer(
text = 'Footer text example',
icon_url = 'https://avatars.githubusercontent.com/u/1525981?s=200&v=4'
)
# Add extra fields
message.fields = [
Field(name = 'Field 1', value = 'Value 1'),
Field(name = 'Field 2', value = 'Value 2')
]
# Set the thumbnail
message.thumbnail = 'https://i.imgur.com/0QGx79x.png'
# Add an image
message.image = 'https://avatars.githubusercontent.com/u/1965106?s=200&v=4'
# Setup the plugin
url = 'https://discord.com/api/webhooks/95811659/Y6uu4DOUH'
discord = BotDiscordPlugin(urls = url, username = "Bot Discord Name")
# Send the message
response = discord.send_embedded_message(message)
Here is the expected output:
Editting a Message¶
from botcity.plugins.discord import BotDiscordPlugin
url = 'https://discord.com/api/webhooks/95811659/Y6uu4DOUH'
discord = BotDiscordPlugin(urls = url, username = "Bot Discord Name")
first_message_response = discord.send_message(content = 'Hello!')
updated_message_response = discord.edit_message(first_message_response, 'New content.')
Here is the expected output:
Uploading a File¶
Here is the expected output:
Deleting a Message/File¶
Info
This method takes the response from an action.
import time
from botcity.plugins.discord import BotDiscordPlugin
url = 'https://discord.com/api/webhooks/95811659/Y6uu4DOUH'
discord = BotDiscordPlugin(urls = url, username = "Bot Discord Name")
# Send a message
response = discord.send_message(content='Hello!')
# Wait for the message to be sent
time.sleep(5)
# Delete the message
discord.delete_message(response)