-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discord User Check
65 lines (56 loc) · 2.06 KB
/
Discord User Check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import discord
from woocommerce import API
import asyncio
# Replace with your own Discord bot token
TOKEN = 'your_token_here'
# Replace with your own WooCommerce API credentials
wc_api = API(
url='https://yourstore.com',
consumer_key='ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumer_secret='cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
version='wc/v3'
)
# Replace with the Discord role ID for the subscriber role
SUBSCRIBER_ROLE_ID = 1234567890
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as', client.user)
await check_subscriptions()
@client.event
async def on_member_join(member):
# Check if the user is a subscriber
customer_id = get_customer_id(member.id)
if customer_id is None:
return
subscriptions = wc_api.get(f'customers/{customer_id}/subscriptions').json()
for subscription in subscriptions:
if subscription['status'] == 'active':
role = discord.utils.get(member.guild.roles, id=SUBSCRIBER_ROLE_ID)
await member.add_roles(role)
return
async def check_subscriptions():
while True:
await asyncio.sleep(3600) # check every 1 hour
members = client.get_all_members()
for member in members:
customer_id = get_customer_id(member.id)
if customer_id is None:
continue
subscriptions = wc_api.get(f'customers/{customer_id}/subscriptions').json()
active = False
for subscription in subscriptions:
if subscription['status'] == 'active':
active = True
break
if not active:
role = discord.utils.get(member.guild.roles, id=SUBSCRIBER_ROLE_ID)
await member.remove_roles(role)
# function to get customer id from discord user id
def get_customer_id(user_id):
customers = wc_api.get('customers').json()
for customer in customers:
if customer['meta_data'][0]['value'] == user_id:
return customer['id']
return None
client.run(TOKEN)