Discord Member Engagement Bot

 import discord
from discord.ext import commands
import asyncio
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import pandas as pd

intents = discord.Intents.all()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)

# Slack client
slack_client = WebClient(token=os.environ["SLACK_API_TOKEN"])

# Metrics
engaged_users = set()
new_members = 0
members_left = 0

async def on_message(message):
    global engaged_users

About this template

This bot will be designed to perform a variety of tasks on Discord, including counting metrics, tracking unique engaged members, tracking new members, and tracking members that have left. These metrics will also be sent as messages to a Slack channel every 7 days in the form of a data frame table converted to text. To make it work, it requires the following environment variables: DISCORD_TOKEN (your Discord bot token), SLACK_API_TOKEN (your Slack API token), and SLACK_CHANNEL (the ID of the Slack channel where the bot will send messages).

Introduction to the Discord Member Engagement Bot Template

Welcome to the Discord Member Engagement Bot Template! This bot is designed to help you track and measure user engagement within your Discord server. It counts unique engaged users, new members, and members who have left, and sends these metrics to a designated Slack channel every week. This article will guide you through the process of setting up and using this template on the Lazy platform.

Clicking Start with this Template

To begin using the Discord Member Engagement Bot Template, click on the "Start with this Template" button. This will initialize the template within the Lazy Builder interface, pre-populating the code necessary for the bot's functionality.

Initial Setup

Before you can use the bot, you'll need to set up a few environment secrets that the bot requires to function properly. These are the DISCORD_TOKEN, SLACK_API_TOKEN, and SLACK_CHANNEL. Here's how to acquire these values:

  • DISCORD_TOKEN: This is the token for your Discord bot. You can obtain it by creating a bot application in the Discord Developer Portal and copying the token provided.
  • SLACK_API_TOKEN: This token allows your bot to communicate with Slack. You can get it by creating a Slack app and installing it to your workspace, then navigating to the 'OAuth & Permissions' page to find your 'Bot User OAuth Token'.
  • SLACK_CHANNEL: This is the ID of the Slack channel where the bot will send messages. You can find this by navigating to your Slack workspace, selecting the desired channel, and looking at the URL. The channel ID is the last segment of the URL, typically starting with a 'C'.

Once you have these values, enter them into the Environment Secrets tab within the Lazy Builder.

Test: Pressing the Test Button

After setting up the environment secrets, press the "Test" button to deploy the app. The Lazy CLI will handle the deployment, and you won't need to install any libraries or set up your environment.

Entering Input

Analyze the code to see if the bot requires user input through the CLI. In this case, the bot does not require any additional user input, so you can skip this step.

Using the App

There is no frontend experience for this app, as its primary function is to operate in the background of your Discord server and send messages to Slack. Therefore, there is no interface to interact with directly within the app.

Integrating the App

After testing and confirming that the bot is running correctly, you'll need to invite the bot to your Discord server using the OAuth2 URL generated in the Discord Developer Portal. Ensure that you grant the bot the necessary permissions to read messages and track member events.

For Slack integration, make sure that your Slack app has the necessary permissions to post messages to the channel you've specified. You can adjust these permissions in the Slack app settings under 'OAuth & Permissions'.

Once the bot is added to your Discord server and configured in Slack, it will begin tracking engagement metrics and sending weekly updates to your Slack channel automatically.

If you encounter any issues or need further assistance, refer to the documentation provided by Discord and Slack for creating bots and apps, respectively.

Discord Bot Documentation: https://discord.com/developers/docs/intro

Slack API Documentation: https://api.slack.com/

By following these steps, you should now have a fully functional Discord Member Engagement Bot running on the Lazy platform, providing valuable insights into your server's engagement directly to Slack.

Last published
July 20, 2024

More templates like this

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.


AlphaBot Financial Summary

AlphaBot Financial Summary: A Discord bot that utilizes the Alpha Vantage API to provide AI-generated summaries of income statements, balance sheets, and cash flow for renowned US companies.


Discord Moderation Bot

The Discord bot monitors all messages in the server. If a message contains profanity, the bot deletes it and sends a warning to the channel. The bot also notifies the host about the deletion via a direct message. Additionally, the bot outputs a helpful error message to a channel if there is a permissions error, guiding the server admin to enable the required permission in the Discord Developer portal.

Discord Member Engagement Bot