generated from kkrypt0nn/Python-Discord-Bot-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.py
853 lines (742 loc) · 37.1 KB
/
streamer.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
""""
Copyright © Krypton 2022 - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
This is a template to create your own discord bot in python.
Version: 5.4
"""
import asyncio
from collections import defaultdict
import datetime
import json
import random
import re
import pytz
import requests
from discord import Color, Webhook, SyncWebhook
import aiohttp
import discord
from discord import Embed, app_commands
from discord.ext import commands, tasks
from discord.ext.commands import Context, has_permissions
from helpers import battle, checks, db_manager, hunt, mine, search, bank, beg
from typing import List, Tuple
from discord.ext.commands.errors import CommandInvokeError
from num2words import num2words
import os
import sys
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
with open("config.json") as file:
config = json.load(file)
twitch_client_id = config["CLIENT_ID"]
twitch_client_secret = config["CLIENT_SECRET"]
def checkUser(user_id, oauth_token):
url = "https://api.twitch.tv/helix/streams"
headers = {
"Authorization": f"Bearer {oauth_token}",
"Client-ID": f"{twitch_client_id}"
}
params = {
"user_id": user_id
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
if len(data["data"]) > 0: # If the 'data' array is not empty, the user is streaming
# Get the stream's thumbnail
thumbnail_url_template = data["data"][0]["thumbnail_url"]
thumbnail_url = thumbnail_url_template.format(width="1920", height="1080") # Replace the width and height in the template URL
return True, data["data"][0]["title"], thumbnail_url # Return True, the stream title, and the thumbnail URL
else:
return False, None, None # If the user is not streaming, return False, None, and None
else:
print(f"Error checking user: {response.json()}")
return False, None, None
def refresh_token_func(refresh_token):
url = "https://id.twitch.tv/oauth2/token"
payload = {
"grant_type": "refresh_token",
"refresh_token": refresh_token,
"client_id": twitch_client_id,
"client_secret": twitch_client_secret
}
response = requests.post(url, data=payload)
if response.status_code == 200:
new_tokens = response.json()
return new_tokens["access_token"], new_tokens["refresh_token"]
else:
print(f"Error refreshing token: {response.json()}")
return None, None
global i
i = 0
cash = "⌬"
replycont = "<:replycontinued:1245851813022793778>"
reply = "<:reply:1245851809730396210>"
rarity_colors = {
"Common": 0x808080, # Grey
"Uncommon": 0x319236, # Green
"Rare": 0x4c51f7, # Blue
"Epic": 0x9d4dbb, # Purple
"Legendary": 0xf3af19, # Gold
# Add more rarities and colors as needed
}
# Here we name the cog and create a new class for the cog.
class Streamer(commands.Cog, name="streamer"):
def __init__(self, bot):
self.bot = bot
try:
self.live_streams = set()
self.check_streams.start()
self.refresh_tokens.start()
except Exception as e:
print(e)
pass
#check every 5 minutes if any streamers are live
@tasks.loop(minutes=5)
async def check_streams(self):
# Get all streamers from the database
streamers = await db_manager.view_streamers()
#check if the streamer is live
for i in streamers:
user_id = i[2]
twitch_id_of_streamer = i[3]
oauth = await db_manager.get_twitch_oauth_token(user_id)
if oauth == None or oauth == False or oauth == [] or oauth == "None" or oauth == 0:
continue
is_live, title, thumbnail_url = checkUser(twitch_id_of_streamer, oauth) # Get the thumbnail URL
if is_live:
# Check if the streamer is already in the set
if any(twitch_channel == i[1] for twitch_channel, _, _, _ in self.live_streams):
continue # Skip to the next iteration of the loop without sending the announcement
self.live_streams.add((i[1], i[2], title, thumbnail_url)) # Include the title and thumbnail URL in the tuple
else:
self.live_streams.discard((i[1], i[2]))
#send a message to the discord channel if a streamer is live
if len(self.live_streams) > 0:
for twitch_channel, user_id, title, thumbnail_url in self.live_streams: # Unpack the tuple to get the title and thumbnail URL
channelID = await db_manager.get_discord_channel_id_live_announce(user_id)
if channelID == None or channelID == False or channelID == [] or channelID == "None" or channelID == 0:
continue
roleID = await db_manager.get_discord_role_id_live_announce(user_id)
if roleID == None or roleID == False or roleID == [] or roleID == "None" or roleID == 0:
continue
role = await self.bot.get_role(roleID)
channel = await self.bot.get_channel(channelID)
print(f"{twitch_channel} is live!")
#get the message
message = await db_manager.get_discord_announce_message(user_id)
# Create an embed message
embed = discord.Embed(
title=title,
description=f"[{title}](https://twitch.tv/{twitch_channel})",
color=discord.Color.from_rgb(100, 65, 165),
)
embed.set_author(name=f"{twitch_channel} is live", icon_url=thumbnail_url) # Set the author to the streamer
embed.set_thumbnail(url=thumbnail_url) # Set the thumbnail to the stream's thumbnail
#get the unix timestamp of it and format it like so <t:{unix_timestamp}:R>
unix_timestamp = datetime.datetime.now().timestamp()
unix_timestamp = int(unix_timestamp)
#replace the {twitch_channel} with the twitch channel name
embed.set_footer(text=f"nyanstreamer.lol" ) # Add a footer
embed.timestamp = datetime.datetime.utcnow() # Add a timestamp
await channel.send(content=f"{role.mention} {message}", embed=embed) # Include the title in the message
else:
print("No streamers are live")
@check_streams.before_loop
async def before_check_streams(self):
await self.bot.wait_until_ready()
#every second, check if any giveaways are over (check the chatgpt page behind this to get started, kk love you -past Truen )
@tasks.loop(hours=2) # Adjust the interval as needed
async def refresh_tokens(self):
print("Refreshing tokens...")
# Get all streamers from the database
streamers = await db_manager.view_streamers()
for i in streamers:
user_id = i[2]
refresh_token = await db_manager.get_twitch_refresh_token(user_id) # Get the refresh token
if not refresh_token:
print(f"No refresh token found for {i[1]}")
continue
# Refresh the token
new_access_token, new_refresh_token = refresh_token_func(refresh_token)
if new_access_token and new_refresh_token:
# Store the new tokens
await db_manager.set_twitch_oauth_token(user_id, new_access_token)
await db_manager.set_twitch_refresh_token(user_id, new_refresh_token)
print(f"Refreshed tokens for {i[1]}")
@refresh_tokens.before_loop
async def before_refresh_tokens(self):
await self.bot.wait_until_ready()
@commands.hybrid_group(
name="streamer",
description="The base command for all streamer commands.",
aliases=["s"],
)
async def streamer(self, ctx: Context):
"""
The base command for all streamer commands.
:param ctx: The context in which the command was called.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help("streamer")
#command to create a new item in the database item table, using the create_streamer_item function from helpers\db_manager.py
#`streamer_prefix` varchar(20) NOT NULL,
#`item_id` varchar(20) NOT NULL,
#`item_name` varchar NOT NULL,
#`item_emoji` varchar(255) NOT NULL,
#`item_rarity` varchar(255) NOT NULL,
@streamer.command(
name="createitem",
description="make a new item for your viewers to collect!",
aliases=["ci", "create_item"],
)
@checks.is_streamer()
async def create_item(self, ctx: Context, channel: str, name: str, emoji: discord.Emoji):
checkUser = await db_manager.check_user(ctx.author.id)
if checkUser == None or checkUser == False or checkUser == [] or checkUser == "None" or checkUser == 0:
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
"""
This command will create a new streamer item in the database.
:param ctx: The context in which the command was called.
:param item_id: The id of the item that should be created.
:param item_name: The name of the item that should be created.
:param item_emoji: The emoji of the item that should be created.
:param item_rarity: The rarity of the item that should be created.
"""
#get the user id from the twitch channel name
user_id = await db_manager.get_user_id_from_streamer_channel(channel)
#get the streamer prefix
streamer_prefix = await db_manager.get_streamerPrefix_with_user_id(user_id)
#get streamer broadcast type
broadcast_type = await db_manager.get_broadcaster_type_from_user_id(user_id)
print(broadcast_type)
#check if the item exists in the database
items = await db_manager.view_streamer_items(channel)
for i in items:
if name in i:
await ctx.send(f"An Item named {name} already exists for your channel.")
return
#create the item
#if the streamer has more than 5 items and is an affilate, the item will not be created
if broadcast_type == "affiliate" and len(items) >= 5:
await ctx.send("You have reached the maximum amount of custom items for an affiliate.")
return
#if the streamer has more than 10 items and is a partner, the item will not be created
elif broadcast_type == "partner" and len(items) >= 10:
await ctx.send("You have reached the maximum amount of custom items for a partner.")
return
else:
#do stuff with the emoji to get into this format <a:AspenRave:1064715986852393030> or <:AspenRave:1064715986852393030>
emojistr = str(emoji)
await db_manager.create_streamer_item(streamer_prefix, channel, name, emojistr)
#give the user the item
item_id = str(streamer_prefix) + "_" + name
#convert all spaces in the item name to underscores
item_id = item_id.replace(" ", "_")
#send more info
embed = discord.Embed(title="Item Creation", description=f"Created item {emojistr} **{name}** for the channel {channel}",)
embed.set_footer(text="ID: " + item_id)
await ctx.send(embed=embed)
#autocommplete for the createitem command
@create_item.autocomplete("channel")
async def create_item_channel_autocomplete(self, ctx: discord.Interaction, argument: str):
"""
This function provides autocomplete choices for the create_item command.
:param ctx: The context in which the command was called.
:param argument: The user's current input for the item name.
"""
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
#command to remove an item from the database item table, using the remove_item function from helpers\db_manager.py, make sure only streamers can remove their own items
@streamer.command(
name="removeitem",
description="delete an item from your channel! (only works for items you or your mods created)",
aliases=["ri", "remove_item"],
)
@checks.is_streamer()
async def removeitem(self, ctx: Context, channel: str, item: str):
checkUser = await db_manager.check_user(ctx.author.id)
if checkUser == None or checkUser == False or checkUser == [] or checkUser == "None" or checkUser == 0:
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
"""
This command will remove an item from the database.
:param ctx: The context in which the command was called.
:param item_id: The id of the item that should be removed.
"""
items = await db_manager.view_streamer_items(channel)
for i in items:
if item in i:
emoji = await db_manager.get_streamer_item_emote(item)
name = await db_manager.get_streamer_item_name(item)
embed = discord.Embed(title="Item Removal", description=f"Removed the item {emoji} **{name}** from the channel {channel}",)
embed.set_footer(text="ID: " + item)
await ctx.send(embed=embed)
await db_manager.remove_item(item)
return
await ctx.send(f"Item with the ID `{item}` does not exist in the database or you are not the streamer that owns this item.")
#giveaway command
@commands.hybrid_group(
name="giveaway",
description="The base command for all giveaway commands.",
aliases=["ga"],
)
async def giveaway(self, ctx: Context):
"""
The base command for all giveaway commands.
:param ctx: The context in which the command was called.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help("giveaway")
#command to create a new giveaway in the database giveaway table, using the create_giveaway function from helpers\db_manager.py
#autocommplete for the removeitem command
@removeitem.autocomplete("channel")
async def remove_item_channel_autocomplete(self, ctx: discord.Interaction, argument: str):
"""
This function provides autocomplete choices for the remove_item command.
:param ctx: The context in which the command was called.
:param argument: The user's current input for the item name.
"""
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
@removeitem.autocomplete("item")
async def remove_item_autocomplete(self, ctx: discord.Interaction, argument):
"""
This function provides autocomplete choices for the remove_item command.
:param ctx: The context in which the command was called.
:param argument: The user's current input for the item name.
"""
channel_value = ctx.data["options"][0]["options"][0]["value"]
print(channel_value)
streamer_items = await db_manager.view_user_streamer_made_items(channel_value)
choices = []
for item in streamer_items:
if argument.lower() in item[3].lower(): # Assuming item[1] is the item's name
choices.append(app_commands.Choice(name=item[3], value=item[2])) # Assuming item[0] is the item's ID
return choices[:25]
#command to view all the streamer items owned by the user from a specific streamer, if they dont have the item, display ??? for the emoji
@streamer.command(
name="case",
description="see all the items you have from streamers! :)",
)
async def case(self, ctx: Context, streamer: str = None):
checkUser = await db_manager.check_user(ctx.author.id)
if checkUser == None or checkUser == False or checkUser == [] or checkUser == "None" or checkUser == 0:
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
if streamer == None:
streamers = await db_manager.view_streamers()
else:
streamers = await db_manager.view_streamers()
for i in streamers:
if streamer in i:
streamers = []
streamers.append(i)
break
user_id = ctx.message.author.id
user_name = ctx.message.author.name
# Get all streamers from the database
# Create a list of embeds with 2 streamers per embed
embeds = []
for i in range(0, len(streamers), 2):
description = ""
total_items = 0
total_owned_items = 0
for streamer in streamers[i:i+2]:
streamer = streamer[1].lower()
# Get the items from the database
items = await db_manager.view_streamer_items(streamer)
# Get the user's items from the database
user_items = await db_manager.view_streamer_item_inventory(user_id)
# Initialize an empty string to hold all the item information
item_info = ""
user_item_count = 0
# Add the items to the string
for index, item in enumerate(items): # Initialize counter for user items
total_items += 1
if any(item[2] in user_item for user_item in user_items):
user_item_count += 1 # Increment counter if user owns the item
total_owned_items += 1
if index == len(items) - 1: # Check if this is the last item
item_info += f"{reply} **{item[4]}{item[3]}**\n"
else:
item_info += f"{replycont} **{item[4]}{item[3]}**\n"
else:
if index == len(items) - 1: # Check if this is the last item
item_info += f"{reply} **???**\n"
else:
item_info += f"{replycont} **???**\n"
# Add the streamer information to the description
description += f"**[{streamer}](https://twitch.tv/{streamer})** ({user_item_count}/{len(items)})\n{item_info}\n\n"
# Create the embed with the description
embed = discord.Embed(title=f"{user_name}'s Streamer Items", description="Here are your Streamer Items, If it has ???, it means you don't own one, think of this as a trophy case for streamer items you collect by watching their streams :) \n \n" + description)
embed.set_footer(text=f"Total items owned: {total_owned_items}/{total_items}")
embeds.append(embed)
class StreamerItemsButton(discord.ui.View):
def __init__(self, current_page, embeds, **kwargs):
super().__init__(**kwargs)
self.current_page = current_page
self.embeds = embeds
@discord.ui.button(label="<<", style=discord.ButtonStyle.green)
async def on_first_page(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current_page = 0
await interaction.response.edit_message(embed=self.embeds[self.current_page])
@discord.ui.button(label="<", style=discord.ButtonStyle.green)
async def on_previous_page(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.current_page > 0:
self.current_page -= 1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
@discord.ui.button(label=">", style=discord.ButtonStyle.green)
async def on_next_page(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.current_page < len(self.embeds) - 1:
self.current_page += 1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
@discord.ui.button(label=">>", style=discord.ButtonStyle.green)
async def on_last_page(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current_page = len(self.embeds) - 1
await interaction.response.edit_message(embed=self.embeds[self.current_page])
# If there are multiple pages, add the buttons
if len(embeds) > 1:
view = StreamerItemsButton(current_page=0, embeds=embeds)
await ctx.send(embed=embeds[0], view=view)
else:
await ctx.send(embed=embeds[0])
@case.autocomplete("streamer")
async def streamercase_autocomplete(self, ctx: Context, argument):
# Get all streamers from the database
streamers = await db_manager.view_streamers()
# Filter the streamers based on the user's input
choices = [app_commands.Choice(name=streamer[1], value=streamer[1]) for streamer in streamers if argument.lower() in streamer[1].lower()]
# Return the first 25 matches
return choices[:25]
#add mod command to add a mod for a streamer
@streamer.command(
name="addmod",
description="add a mod for your channel!",
aliases=["am", "add_mod"],
)
@checks.is_streamer()
async def add_mod(self, ctx: Context, user: discord.Member):
checkUser = await db_manager.check_user(ctx.author.id)
if checkUser == None or checkUser == False or checkUser == [] or checkUser == "None" or checkUser == 0:
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
"""
This command will add a mod for a streamer in the database.
:param ctx: The context in which the command was called.
:param user_id: The id of the user that should be added as a mod.
"""
user_id = ctx.message.author.id
#get the streamer prefix
streamer_prefix = await db_manager.get_streamerPrefix_with_user_id(user_id)
#get streamer broadcast type
channel = await db_manager.get_streamer_channel_from_user_id(user_id)
#prevent the streamer from adding themselves as a mod
if user.id == user_id:
await ctx.send("You cannot add yourself as a mod!")
return
#get the streamer's mods
mods = await db_manager.get_channel_mods(channel)
#check if the user is already a mod
for i in mods:
if user.id in i:
await ctx.send(f"{user.mention} is already a mod for your channel!")
return
#add the mod
#get the mods twitch name and id
twitch_name = await db_manager.get_twitch_name(user.id)
twitch_id = await db_manager.get_twitch_id(user.id)
if twitch_name == None or twitch_name == False or twitch_name == [] or twitch_name == "None" or twitch_name == 0:
await ctx.send(f"{user.mention} is not connected to a twitch account!")
return
await db_manager.add_mod_to_channel(channel, user.id, twitch_id, twitch_name)
await ctx.send(f"Added {user.mention} as a mod for your channel!")
#remove mod command to remove a mod for a streamer
@streamer.command(
name="removemod",
description="remove a mod for your channel!",
aliases=["rm", "remove_mod"],
)
@checks.is_streamer()
async def remove_mod(self, ctx: Context, user: discord.Member):
checkUser = await db_manager.check_user(ctx.author.id)
if checkUser == None or checkUser == False or checkUser == [] or checkUser == "None" or checkUser == 0:
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
"""
This command will remove a mod for a streamer in the database.
:param ctx: The context in which the command was called.
:param user_id: The id of the user that should be removed as a mod.
"""
user_id = ctx.message.author.id
#get the streamer prefix
streamer_prefix = await db_manager.get_streamerPrefix_with_user_id(user_id)
#get streamer broadcast type
channel = await db_manager.get_streamer_channel_from_user_id(user_id)
#get the streamer's mods
mods = await db_manager.get_channel_mods(channel)
#check if the user is already a mod
for i in mods:
if user.id in i:
#remove the mod
await db_manager.remove_mod_from_channel(channel, user.id)
await ctx.send(f"Removed {user.mention} as a mod for your channel!")
return
await ctx.send(f"{user.mention} is not a mod for your channel!")
#command to view all the mods for a streamer
@streamer.command(
name="mods",
description="see all the mods for your channel!",
)
@checks.is_streamer()
async def mods(self, ctx: Context):
"""
This command will view all the mods for a streamer in the database.
:param ctx: The context in which the command was called.
"""
user_id = ctx.message.author.id
#get the streamer prefix
streamer_prefix = await db_manager.get_streamerPrefix_with_user_id(user_id)
#get streamer broadcast type
channel = await db_manager.get_streamer_channel_from_user_id(user_id)
#get the streamer's mods
mods = await db_manager.get_channel_mods(channel)
#create a string with all the mods
mod_string = ""
for i in range(len(mods)):
if i == len(mods) - 1: # If it's the last mod
mod_string += f"{reply} <@{mods[i][0]}>\n"
else:
mod_string += f"{replycont} <@{mods[i][0]}>\n"
else:
if mod_string == "":
mod_string = "No mods for this channel, Yet!"
#create an embed with all the mods
embed = discord.Embed(title=f"{channel}'s Mods", description=mod_string)
await ctx.send(embed=embed)
#chat setup command
@streamer.group(
name="chat",
description="chat commands for streamers!",
)
async def chat(self, ctx: Context):
"""
The base command for all chat commands.
:param ctx: The context in which the command was called.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help("chat")
#command to setup the chat for a streamer
@chat.command(
name="setup",
description="setup the chat for your channel!",
)
@checks.is_streamer()
async def chatsetup(self, ctx: Context, streamer: str, channel: discord.TextChannel):
"""
This command will setup the chat for a streamer in the database.
:param ctx: The context in which the command was called.
"""
mods = await db_manager.get_channel_mods(streamer)
await db_manager.set_discord_channel_id_chat(streamer, channel.id)
await ctx.send(f"Twitch to Discord Chat setup for **{streamer}** in {channel.mention}! (It will take a few minutes for the chat to start working)")
#auto complete for the chatsetup command for the streamer
@chatsetup.autocomplete("streamer")
async def chatsetup_streamer_autocomplete(self, ctx: Context, argument):
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
#remove the chat setup for a streamer
@chat.command(
name="remove",
description="remove the chat setup for your channel!",
)
@checks.is_streamer()
async def chatremove(self, ctx: Context, streamer: str):
"""
This command will remove the chat setup for a streamer in the database.
:param ctx: The context in which the command was called.
"""
mods = await db_manager.get_channel_mods(streamer)
await db_manager.remove_discord_channel_id_chat(streamer)
await ctx.send(f"Twitch to Discord Chat removed for **{streamer}**! (It will take a few minutes for the chat to stop working)")
#auto complete for the chatremove command for the streamer
@chatremove.autocomplete("streamer")
async def chatremove_streamer_autocomplete(self, ctx: Context, argument):
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
#annouce stuff
@streamer.group(
name="announce",
description="announce stuff for streamers!",
)
async def announce(self, ctx: Context):
"""
The base command for all announce commands.
:param ctx: The context in which the command was called.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help("announce")
#command to setup the live announce for a streamer
@announce.command(
name="setup",
description="setup the live announce for your channel!",
)
@checks.is_streamer()
async def announcesetup(self, ctx: Context, streamer: str, message: str, channel: discord.TextChannel, role: discord.Role):
"""
This command will setup the live announce for a streamer in the database.
:param ctx: The context in which the command was called.
:param role: The role that should be mentioned when the streamer goes live.
"""
mods = await db_manager.get_channel_mods(streamer)
await db_manager.set_discord_channel_id_live_announce(streamer, channel.id)
await db_manager.set_discord_role_id_live_announce(streamer, role.id)
await db_manager.set_discord_announce_message(streamer, message)
await ctx.send(f"Gotcha :), I'll announce when **{streamer}** goes live in {channel.mention} with the message: {role.name} {message}!")
#auto complete for the announcesetup command for the streamer
@announcesetup.autocomplete("streamer")
async def announcesetup_streamer_autocomplete(self, ctx: Context, argument):
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
#remove the live announce for a streamer
@announce.command(
name="remove",
description="remove the live announce for your channel!",
)
@checks.is_streamer()
async def announceremove(self, ctx: Context, streamer: str):
"""
This command will remove the live announce for a streamer in the database.
:param ctx: The context in which the command was called.
"""
mods = await db_manager.get_channel_mods(streamer)
await db_manager.remove_discord_channel_id_live_announce(streamer)
await db_manager.remove_discord_role_id_live_announce(streamer)
await db_manager.remove_discord_announce_message(streamer)
await ctx.send(f"Live Announce removed for **{streamer}**!")
#auto complete for the announceremove command for the streamer
@announceremove.autocomplete("streamer")
async def announceremove_streamer_autocomplete(self, ctx: Context, argument):
streamers = await db_manager.get_user_mod_channels(ctx.user.id)
user_channel = await db_manager.get_streamer_channel_from_user_id(ctx.user.id)
# Add the user's channel to the list
if user_channel is not None:
streamers.append((user_channel,))
choices = []
for streamer in streamers:
streamer = streamer[0]
#make it a string
streamer = str(streamer)
if argument.lower() in streamer.lower():
choices.append(app_commands.Choice(name=streamer, value=streamer))
return choices[:25]
@commands.hybrid_command(
name="connect",
description="Connect your twitch account to your discord account!",
)
async def connect(self, ctx: Context):
#check if th user exists in the database
user_exists = await db_manager.check_user(ctx.author.id)
if user_exists == None or user_exists == [] or user_exists == False or user_exists == 0 or user_exists == "None":
await ctx.send("You are not in the database yet, please use the `s.start or /start` command to start your adventure!")
return
#create an embed to send to the user, then add a button to connect their twitch account
embed = discord.Embed(
title="Connect your twitch account to your discord account!",
description="Click the button below to connect your twitch account to your discord account!",
color=discord.Color.blurple()
)
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/881056455321487390/881056516333762580/unknown.png")
embed.set_footer(text="NyanStreamer")
#make a discord.py button interaction
class MyView(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
def __init__(self, url: str):
super().__init__()
self.url = url
self.add_item(discord.ui.Button(label="Register With Twitch!", url=self.url))
#send the embed to the user in DMs
await ctx.send("Check your DMs :)")
try:
await ctx.author.send(embed=embed, view=MyView(f"https://nyanstreamer.lol/webhook?discord_id={ctx.author.id}")) # Send a message with our View class that contains the button
except(CommandInvokeError):
await ctx.send("I couldn't DM you! Make sure your DMs are open!")
return
#disconnect command
@commands.hybrid_command(
name="disconnect",
description="Disconnect your twitch account from your discord account!",
)
async def disconnect(self, ctx: Context):
#check if the user is connected
isConnected = await db_manager.is_connected(ctx.author.id)
if isConnected == False:
await ctx.send("You are not connected to a twitch account!")
return
#disconnect the user
await db_manager.disconnect_twitch_id(ctx.author.id)
await db_manager.disconnect_twitch_name(ctx.author.id)
await db_manager.update_is_not_streamer(ctx.author.id)
await db_manager.remove_streamer_items(ctx.author.id)
await db_manager.remove_streamer(ctx.author.id)
await ctx.send("Your twitch account has been disconnected from your discord account!")
#every 5 minutes check if the streamers are live
# And then we finally add the cog to the bot so that it can load, unload, reload and use it's content.
async def setup(bot):
await bot.add_cog(Streamer(bot))