Discord Member Engagement Bot

By
 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

@bot.event
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.

Category
Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.

Streamlit
Python

A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .

Python
Flask

WhatsApp ChatBot (Twilio, InstantChat)

Build a WhatApp chatbot using Twilio, Discord and LazyAI.

WhatsApp
Twilio
Discord
Home
/
Discord Member Engagement Bot