Skip to content

Commit

Permalink
Get twitch subs from new twitch_subs table instead of getting in from…
Browse files Browse the repository at this point in the history
… the api for the discord role sync
  • Loading branch information
thomaserlang committed Feb 27, 2024
1 parent cb1eb25 commit 4599429
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions tbot/discord_bot/tasks/twitch_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ async def sync(self):

async def set_sub_roles(self, returninfo):
subs = await self.get_subscribers()
await self.count_subs(subs)
subs = {d['user_id']: d for d in subs}
cached_badges = await self.get_cached_badges_months()
for member in self.server.members:
Expand All @@ -71,7 +70,7 @@ async def set_sub_roles(self, returninfo):
months = cached_badges[twitch_id]['sub'] or 0
sub_streak_role = None
for role in self.roles:
if role['type'] == 'sub_tier' and role['value'] == subinfo['sub_plan']:
if role['type'] == 'sub_tier' and role['value'] == subinfo['tier']:
self.give_roles[member].append(role['role'])
elif role['type'] == 'sub_streak' and months >= int(role['value']):
sub_streak_role = role['role']
Expand Down Expand Up @@ -130,38 +129,11 @@ async def sync_roles(self, returninfo):
returninfo['removed_users'] += 1

async def get_subscribers(self):
'''
return [
{
"broadcaster_id": "123",
"broadcaster_name": "test_user",
"is_gift": True,
"tier": "1000",
"plan_name": "Channel Subscription (erleperle)",
"user_id": "36981191",
"user_name": "erleperle",
}
]
'''

subs = []
url = 'https://api.twitch.tv/helix/subscriptions'
after = ''
while True:
d = await twitch_channel_token_request(self.bot, self.info['channel_id'], url, params={
'broadcaster_id': self.info['channel_id'],
'after': after,
})
if d['data']:
subs.extend(d['data'])
else:
break
if not 'pagination' in d or not d['pagination']:
break
after = d['pagination']['cursor']
return subs


rows = await self.bot.db.fetchall(
'SELECT user_id, tier FROM twitch_subs WHERE channel_id=%s',
(self.info['channel_id']),
)
return rows

async def get_cached_badges_months(self):
rows = await self.bot.db.fetchall(
Expand Down

0 comments on commit 4599429

Please sign in to comment.