A story about reflecting Discord activity in Slack Status

Overview

Discord activity image.png To Reflected in Slack Status. image.png

Who's the deal? This is ...

discord.py This time, we will use ** discord.py **, which is the Python library of Discord API.

-Official Reference

reference

Great pioneers have already written articles about the acquisition of Token, so please refer to that.

-Practical Discord Bot in Python (discordpy explanation)

Implementation

Basically, you put a bot on a discord server you built yourself, and that bot gets the activity of the server members.

The process will be described in each event handler of discord, This time

--When the bot starts --When updating member profile

Let's get the activity.

Each event handler is as follows.

#When the bot starts
@client.event
async def on_ready():
     ...
#When updating a member's profile
@client.event
async def on_member_update(before, after):
     ...

Get activity

As far as I read the official reference, discord.Member Has user activity.

In other words, it suffices if each event handler can get Member. So I decided to use Client.get_all_members ().

However, since all the members participating in the server will be acquired. Get only the Member of the member specified by using the discord.util function.

member = discord.utils.get(client.get_all_members(), name='Name on Discord')
if member.activity != None:
    print(member.activity.name)

If you have KovaaK running image.png

Execution result


KovaaK 2.0: The Meta

Will be.

Points to note when retrieving activity

discord.Member.activity has various activities, For games, the above works, but activities that aren't registered with Discord, such as playing Spotify, may not work.

See below for details.

For the time being, the members of BaseActivity and Spotify all have name, so is it okay ... (maybe)

Rewrite Slack status

You can do this by sending a post request to ʻusers.profile.set` in the Slack API.

Basically, it can be realized by passing the profile name you want to update to name and the contents of the profile to value.

response = requests.post('https://slack.com/api/users.profile.set', 
                       data={'token': 'Slack token',
                       'name': 'status_text',
                       'value': 'test',
                       'user': 'Slack user ID', 
                       'pretty': '1'})

If you throw this, image.png It looks like this.

Putting these together, it looks like this

import discord
import requests

DISCORD_TOKEN = '******'
SLACK_TOKEN = '******'

game = None

#Create an object to connect to Discord
client = discord.Client()

#Get the name of the activity from member and set it in a variable
def set_game_name(member):
    global game
    if member.activity != None:
        game = member.activity.name + 'Playing'
    else:
        game = 'Not Playing game.'

#Update Slack status
def post_slack_status():
    global game
    response = request.post('https://slack.com/api/users.profile.set',
                            data={
                                 'token': SLACK_TOKEN,
                                 'profile': '{ "status_text":"' + game + '",
                                               "status_emoji": ":nick:" }',
                                 'user': '*****',
                                 'pretty': '1'})

#When the bot starts
@client.event
async def on_ready():
    member = discord.utils.get(client.get_all_members(), name='*****')
    set_game_name(member)
    post_slack_status()

#When updating a member's profile
@client.event
async def on_member_update(befor, after):
    set_game_name(after)
    post_slack_status()

#bot launch
client.run(DISCORD_TOKEN)

Right? Isn't it easy?

Friend A "I hope I can get n minutes passed"

I will describe the detailed implementation method later if I feel like it ... Simply put

--Throw a request for post_slack_status () in a loop that goes around every minute. --Thread post_slack_status () and execute it as a separate thread. --When ʻon_member_update (befor, after)runs, set the loop flag toFalse, terminate the thread, reacquire the activity, and then run the thread of post_slack_status () `again.

It's like this. (I thought I wrote it, but what if ʻon_member_update () runs again while waiting for the thread to end in ʻon_member_update () .... Event handler is troublesome ......... )

Postscript (2020/05/07)

There was a lack of understanding of the specifications of ʻon_member_update ()`. on_member_update()

Because it runs when the following of the members are updated Let's run only when the activity is updated!

Recommended Posts

A story about reflecting Discord activity in Slack Status
A story about operating a GCP instance from Discord
A story about a Linux beginner passing LPIC101 in a week
A story about how to specify a relative path in python.
A story about competing with a friend in Othello AI Preparation
A story about trying to implement a private variable in Python.
A story about a tragedy happening by exchanging commands in chat
A refreshing story about Python's Slice
A sloppy story about Python's Slice
A story about a build error in a shared library that references libusb
A story about using Python's reduce
[Google Photo & Slack Photo Bot] A story about making a bot that acquires photos in Google Photos and sends them to Slack.
A story about a beginner making a VTuber notification bot from scratch in Python
[Memorandum] A story about trying OpenCV tutorial (face recognition) in a Windows environment
A story about creating an anonymous channel on Slack from zero knowledge
A story about remodeling Lubuntu into a Chromebook
A story connecting Slack and google spreadsheets
A story about machine learning with Kyasuket
A story about Python pop and append
A story about a 503 error on Heroku open
Escape from Python's virtual environment ~ A story about being trapped in a virtual environment I created ~
A story about simple machine learning using TensorFlow
A story about Go's global variables and scope
A memo about writing merge sort in Python
A story about displaying article-linked ads on Jubatus
Data analysis in Python: A note about line_profiler
A story about implementing a login screen with django
A story about running Python on PHP on Heroku
Think about building a Python 3 environment in a Mac environment
How to notify a Discord channel in Python
A story packed with absolute values in numpy.ndarray
A story about modifying Python and adding functions
A story about data analysis by machine learning
A story of trial and error trying to create a dynamic user group in Slack
A story about trying to introduce Linter in the middle of a Python (Flask) project