Discord activity To Reflected in Slack Status.
Who's the deal? This is ...
discord.py This time, we will use ** discord.py **, which is the Python library of Discord API.
Great pioneers have already written articles about the acquisition of Token, so please refer to that.
-Practical Discord Bot in Python (discordpy explanation)
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):
...
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
Execution result
KovaaK 2.0: The Meta
Will be.
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)
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, 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?
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 to
False, 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 ......... )
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