-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy-twitch.txt
127 lines (116 loc) · 4.65 KB
/
copy-twitch.txt
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
import discord
from discord import app_commands
from typing import Optional
import logging
import datetime
logging.basicConfig(
filename='twitch_commands.log',
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
@app_commands.command(
name="twitch",
description="Manage Twitch channel monitoring"
)
@app_commands.choices(action=[
app_commands.Choice(name="add", value="add"),
app_commands.Choice(name="remove", value="remove"),
app_commands.Choice(name="list", value="list")
])
async def twitch(
interaction: discord.Interaction,
action: str,
channel_name: Optional[str] = None
):
logging.info(f"Command executed by {interaction.user.name}#{interaction.user.discriminator} (ID: {interaction.user.id})")
logging.info(f"Action: {action}, Channel: {channel_name}")
# Role check
streamer_role = discord.utils.get(interaction.guild.roles, name="Streamer")
if streamer_role not in interaction.user.roles:
logging.warning(f"Access denied: User lacks Streamer role")
await interaction.response.send_message(
"You need the 'Streamer' role to use this command!",
ephemeral=True
)
return
# Channel check
if interaction.channel.name != "streamer_submission":
logging.warning(f"Wrong channel: Command used in #{interaction.channel.name}")
await interaction.response.send_message(
"This command can only be used in the #streamer_submission channel!",
ephemeral=True
)
return
# Get live_now channel
live_now_channel = discord.utils.get(interaction.guild.channels, name="live_now")
if not live_now_channel:
logging.error("live_now channel not found")
await interaction.response.send_message(
"The #live_now channel doesn't exist! Please create it first.",
ephemeral=True
)
return
# LIST command - no channel_name needed
if action == "list":
channels = list(interaction.client.twitch_monitor.monitored_channels.keys())
if channels:
logging.info(f"Listed channels: {', '.join(channels)}")
await interaction.response.send_message(
f"📺 Monitored channels that will notify in {live_now_channel.mention}:\n" +
"\n".join([f"• {channel}" for channel in channels]),
ephemeral=True
)
else:
logging.info("Listed channels: None found")
await interaction.response.send_message(
"📝 No channels are currently being monitored.",
ephemeral=True
)
return
# Require channel_name for add/remove
if not channel_name and action in ["add", "remove"]:
await interaction.response.send_message(
"Please provide a channel name!",
ephemeral=True
)
return
# ADD command
if action == "add":
try:
headers = {
'Client-ID': interaction.client.twitch_monitor.client_id,
'Authorization': f'Bearer {interaction.client.twitch_monitor.access_token}'
}
await interaction.client.twitch_monitor.monitor_channel(
channel_name,
live_now_channel.id
)
logging.info(f"Successfully added Twitch channel: {channel_name}")
await interaction.response.send_message(
f"✅ Now monitoring Twitch channel: {channel_name}\nLive notifications will be sent to {live_now_channel.mention}",
ephemeral=True
)
except Exception as e:
logging.error(f"Error adding channel {channel_name}: {str(e)}")
await interaction.response.send_message(
f"❌ Error adding the channel. Please try again later.",
ephemeral=True
)
# REMOVE command
elif action == "remove":
if channel_name in interaction.client.twitch_monitor.monitored_channels:
del interaction.client.twitch_monitor.monitored_channels[channel_name]
logging.info(f"Successfully removed Twitch channel: {channel_name}")
await interaction.response.send_message(
f"✅ Successfully removed channel: {channel_name}",
ephemeral=True
)
else:
logging.info(f"Attempted to remove non-monitored channel: {channel_name}")
await interaction.response.send_message(
f"❌ Channel '{channel_name}' is not being monitored.",
ephemeral=True
)
async def setup(bot):
bot.tree.add_command(twitch)