God mod logo

Message automation on Discord with Selenium

By PyDevDiaries | PyDevDiaries | 22 Oct 2023


Following our diving into the world of game automation, wouldn’t it be enthralling to delve into its core – like sending messages? Imagine, for a moment, the might of your fingers being replaced by code, dispatching messages with precision, all while you sip your coffee. Intrigued? Ready to dive in?

⚠️ Ethical Considerations and Adherence to Terms of Service : It is imperative to consider the ethical implications of automation in games and other platforms. Using automation might mess with other people's experience, coming off as unfair or even causing a ruckus. You gotta be smart about using automation, making sure it doesn't step on the toes or ruin the vibe of other users. Also, folks need to play by the rules and respect Discord's terms of service.  However, breaking the rules can get your account suspended or banned. Please review Discord’s TOS thoroughly before implementing any form of automation on the platform.

Remember, with great power comes great responsibility. However, we must use automation ethically and follow platform rules. Let’s strive to use automation ethically and in compliance with platform rules and regulations. Keep coding, keep exploring, but most importantly, keep respecting!

Let's quickly jog your memory: what exactly is Selenium, you ask?

Before diving into the crux, it's essential to reacquaint ourselves with Selenium. Selenium, in essence, is a boss-level tool that lets you control web browsers like a puppet master, making repetitive browser tasks feel like a walk in the park.  Selenium was originally made to automate testing web apps, but it's become really popular for automating repetitive browser tasks.  Selenium can be used in scripts written in multiple languages including Python, Java, and C#.

Starting up: Connecting to a browser and fetching blocks of code

To interact with any browser, we first need to establish a connection. Here's an example:

# Initialize WebDriver for Edge browser with specific options
options = webdriver.EdgeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9922")
options.use_chromium = True
driver = webdriver.Edge(options=options)

Code breakdown:

  • webdriver.EdgeOptions(): This creates a new instance of options for the Edge browser. Think of it as a set of rules or configurations you want to apply when launching the browser.
  • options.add_experimental_option("debuggerAddress", "127.0.0.1:9922"): Here, we're allowing the script the capability to connect to an already running browser instance. The address "127.0.0.1" is your computer's local address, and "9922" is a specific port.
  • options.use_chromium = True: This line indicates that you want to use the Chromium-based version of Edge. Microsoft Edge was recently rebuilt on the Chromium engine, hence this specification.
  • driver = webdriver.Edge(options=options): Here, we're launching the Edge browser with all the previously set options.
# Navigate and log into Discord
driver.get("")
driver.implicitly_wait(120)

Code breakdown:

  • driver.get(...): This is akin to manually entering the URL into the browser's address bar and hitting Enter.
  • driver.implicitly_wait(120): This line tells the script to wait for up to 120 seconds if an element is not immediately available. It's useful to ensure the page loads fully.
# Navigate to the desired Discord server
server_elem = driver.find_element("xpath", "//div[contains(@data-dnd-name, 'My Wonderful Discord server')]")
server_elem.click()
driver.implicitly_wait(20)

Code breakdown:

  • Locating the Discord server: This locates the server named 'My Wonderful Discord server'.
  • Server selection: This action simulates a mouse click on the identified server.

Crafting the message: A look at the code

Now, let’s explore the code snippet provided for sending messages on Discord:

msg_elem = driver.find_element("xpath","//span[contains(@class, 'emptyText-1o0WH_')]")
msg_elem.send_keys("Hello world")
msg_elem.send_keys(Keys.RETURN)

Code breakdown:

  • msg_elem = driver.find_element("xpath","//span[contains(@class, 'emptyText-1o0WH_')]"): This line is essentially instructing the program to search for a specific element within the webpage. This is achieved using "XPath", a query language for selecting nodes from an XML-like structure. In this context, it's searching for the Discord message input field. The specific details, like 'emptyText-1o0WH_', pertain to the unique characteristics of the message input field on Discord.

    In simpler terms: Think of it as using a map to find a specific location in a city. The XPath provides the detailed directions, and once the location (in this case, the input field) is found, we can interact with it.

  • msg_elem.send_keys("Hello world"): Once we've located the input field using the previous line, this line is simulating the action of typing "Hello world" into that field.

    In simpler terms: Now that we've reached our location (the input field), this line is like writing a note and leaving it there.

  • msg_elem.send_keys(Keys.RETURN): The last line here simulates the action of hitting the "Enter" or "Return" key. In the context of messaging platforms like Discord, hitting this key typically sends the message.

    In simpler terms: After writing our note, this line is like pressing a button to make sure the note is delivered or displayed for everyone to see.


Conclusion and looking forward

Embarking on a journey of self-learning in the realm of programming and automation brings its fair share of challenges. But, as we see with our ability to automate even the seemingly simple task of message dispatching, the rewards are limitless. Although automation enables self-reliance, we still need community.

Keep an eye out for our next deep dive, where we'll tackle game automation's nitty-gritty - focusing on how to read Discord cooldowns in UnderPunks and strategize like a pro. But first, get some rest - you've earned it.

How do you rate this article?

5



PyDevDiaries
PyDevDiaries

Welcome to PyDevDiaries, another portal to the captivating world of blockchain. Dive deep into the revolutionary landscape where technology meets entertainment. I predominantly focus on blockchain-based games, unraveling their mechanics, potential, and future. Join me on this journey to decode and discover the next frontier of digital gaming!

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.